Permalink
Browse files

Added support for sorting results by total, self, wait and child times.

  • Loading branch information...
1 parent 5bab36c commit 32ce287f050e4e01f5f0515fa0fae0f3d8857559 @jsuchal jsuchal committed Sep 15, 2011
View
@@ -104,7 +104,26 @@ opts = OptionParser.new do |opts|
options.measure_mode = RubyProf::GC_TIME
end
end
-
+
+ opts.on('-s sort_mode', '--sort=sort_mode', [:total, :self, :wait, :child],
+ 'Select how ruby-prof results should be sorted:',
+ ' total - Total time (default)',
+ ' self - Self time',
+ ' wait - Wait time',
+ ' child - Child time') do |sort_mode|
+
+ options.sort_method = case sort_mode
+ when :total
+ :total_time
+ when :self
+ :self_time
+ when :wait
+ :wait_time
+ when :child
+ :children_time
+ end
+ end
+
opts.on("--replace-progname", "Replace $0 when loading the .rb files.") do
options.replace_prog_name = true
end
@@ -183,19 +202,20 @@ at_exit {
result = RubyProf.stop
# Create a printer
- printer = options.printer.new(result)
+ printer_options = {:min_percent => options.min_percent, :sort_method => options.sort_method}
+ printer = options.printer.new(result, printer_options)
# Get output
if options.file
# write it relative to the dir they *started* in, as it's a bit surprising to write it in the dir they end up in.
Dir.chdir(options.old_wd) do
File.open(options.file, 'w') do |file|
- printer.print(file, {:min_percent => options.min_percent})
+ printer.print(file, printer_options)
end
end
else
# Print out results
- printer.print(STDOUT, {:min_percent => options.min_percent})
+ printer.print(STDOUT, printer_options)
end
}
@@ -2,10 +2,10 @@
module RubyProf
class AbstractPrinter
- def initialize(result)
+ def initialize(result, options = {})
@result = result
@output = nil
- @options = {}
+ @options = options
end
# Specify print options.
@@ -20,6 +20,7 @@ def initialize(result)
# :print_file - True or false. Specifies if a method's source
# file should be printed. Default value if false.
#
+ # :sort_method - Specifies method used for sorting method infos
def setup_options(options = {})
@options = options
end
@@ -32,6 +33,10 @@ def print_file
@options[:print_file] || false
end
+ def sort_method
+ @options[:sort_method] || :total_time
+ end
+
def method_name(method)
name = method.full_name
if print_file
@@ -8,10 +8,6 @@ module RubyProf
class CallStackPrinter < AbstractPrinter
include ERB::Util
- def initialize(result)
- super(result)
- end
-
# Specify print options.
#
# options - Hash table
@@ -23,8 +23,8 @@ class DotPrinter < RubyProf::AbstractPrinter
EDGE_COLOR = '"#666666"'
# Creates the DotPrinter using a RubyProf::Result.
- def initialize(result)
- super(result)
+ def initialize(result, options = {})
+ super(result, options)
@seen_methods = Set.new
end
@@ -100,11 +100,9 @@ def dot_id(subject)
def print_methods(thread_id, methods)
total_time = thread_times[thread_id]
- # Print each method in total time order
- methods.reverse_each do |method|
+ methods.sort_by(&sort_method).reverse_each do |method|
total_percentage = (method.total_time/total_time) * 100
- self_percentage = (method.self_time/total_time) * 100
-
+
next if total_percentage < min_percent
name = method_name(method).split("#").last
puts "#{dot_id(method)} [label=\"#{name}\\n(#{total_percentage.round}%)\"];"
@@ -12,6 +12,14 @@ module RubyProf
# printer.print(STDOUT, {})
#
class FlatPrinter < AbstractPrinter
+
+ def initialize(results, options = {})
+ # Now sort methods by largest self time by default,
+ # not total time like in other printouts
+ options[:sort_method] ||= :self_time
+ super(results, options)
+ end
+
# Print a flat profile report to the provided output.
#
# output - Any IO oject, including STDOUT or a file.
@@ -43,11 +51,7 @@ def print_methods(thread_id, methods)
total_time = 0.01
end
- # 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
+ methods = methods.sort_by(&sort_method).reverse
@output << "Thread ID: %d\n" % thread_id
@output << "Total: %0.6f\n" % total_time
@@ -13,6 +13,13 @@ module RubyProf
#
class FlatPrinterWithLineNumbers < FlatPrinter
+ def initialize(results, options = {})
+ # Now sort methods by largest self time by default,
+ # not total time like in other printouts
+ options[:sort_method] ||= :self_time
+ super(results, options)
+ end
+
def print_methods(thread_id, methods)
# Get total time
toplevel = methods.max
@@ -21,11 +28,7 @@ def print_methods(thread_id, methods)
total_time = 0.01
end
- # 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
+ methods = methods.sort_by(&sort_method).reverse
@output << "Thread ID: %d\n" % thread_id
@output << "Total: %0.6f\n" % total_time
@@ -30,8 +30,8 @@ class GraphHtmlPrinter < AbstractPrinter
# Create a GraphPrinter. Result is a RubyProf::Result
# object generated from a profiling run.
- def initialize(result)
- super(result)
+ def initialize(result, options = {})
+ super(result, options)
@thread_times = Hash.new
calculate_thread_times
end
@@ -221,7 +221,7 @@ def template
</tr>
<% min_time = @options[:min_time] || (@options[:nonzero] ? 0.005 : nil)
- methods.sort.reverse_each do |method|
+ methods.sort_by(&sort_method).reverse_each do |method|
total_percentage = (method.total_time/total_time) * 100
next if total_percentage < min_percent
next if min_time && method.total_time < min_time
@@ -20,8 +20,8 @@ class GraphPrinter < AbstractPrinter
# Create a GraphPrinter. Result is a RubyProf::Result
# object generated from a profiling run.
- def initialize(result)
- super(result)
+ def initialize(result, options = {})
+ super(result, options)
@thread_times = Hash.new
calculate_thread_times
end
@@ -63,7 +63,7 @@ def print_threads
def print_methods(thread_id, methods)
# Sort methods from longest to shortest total time
- methods = methods.sort
+ methods = methods.sort_by(&sort_method)
toplevel = methods.last
total_time = toplevel.total_time
@@ -5,11 +5,11 @@ module RubyProf
# one profiling run. Currently prints a flat profile, a callgrind
# profile, a call stack profile and a graph profile.
class MultiPrinter
- def initialize(result)
- @stack_printer = CallStackPrinter.new(result)
- @graph_printer = GraphHtmlPrinter.new(result)
- @tree_printer = CallTreePrinter.new(result)
- @flat_printer = FlatPrinter.new(result)
+ def initialize(result, options = {})
+ @stack_printer = CallStackPrinter.new(result, options)
+ @graph_printer = GraphHtmlPrinter.new(result, options)
+ @tree_printer = CallTreePrinter.new(result, options)
+ @flat_printer = FlatPrinter.new(result, options)
end
# create profile files under options[:path] or the current

0 comments on commit 32ce287

Please sign in to comment.