Created
October 26, 2009 17:31
-
-
Save eric/218840 to your computer and use it in GitHub Desktop.
A ruby-prof printer to use with RubyProf::MEMORY
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class MemoryPrinter < RubyProf::AbstractPrinter | |
| # Print a flat profile report to the provided output. | |
| # | |
| # output - Any IO oject, including STDOUT or a file. | |
| # The default value is STDOUT. | |
| # | |
| # options - Hash of print options. See #setup_options | |
| # for more information. | |
| # | |
| def print(output = STDOUT, options = {}) | |
| @output = output | |
| setup_options(options) | |
| print_threads | |
| end | |
| private | |
| def print_threads | |
| @result.threads.each do |thread_id, methods| | |
| print_methods(thread_id, methods) | |
| end | |
| end | |
| def print_methods(thread_id, methods) | |
| # Get total time | |
| total_time = methods.inject(0) { |i, m| m.self_time + i } | |
| # Ignore threads that didn't use any memory | |
| return unless total_time > 2 | |
| # Now sort methods by largest self time, | |
| # not total time like in other printouts | |
| methods = methods.sort do |m1, m2| | |
| m1.self_time <=> m2.self_time | |
| end.reverse | |
| @output << "Thread ID: %d\n" % thread_id | |
| @output << "Total: %d Kb\n" % total_time | |
| @output << "\n" | |
| @output << "%8s %8s %s\n" % %w(self calls name) | |
| methods.each do |method| | |
| @output << "%8d Kb %8d %s\n" % [ | |
| method.self_time, # self | |
| method.called, # calls | |
| method_name(method) # name | |
| ] | |
| if method.self_time > 2_000 | |
| method.aggregate_parents.each do |caller| | |
| next unless caller.parent | |
| @output << ' ' * 23 | |
| @output << "via %s\n" % caller.parent.target.full_name | |
| end | |
| end | |
| end | |
| @output << "\n" * 2 | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment