Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #58 from jsuchal/feature-sorting

Added support for sorting results by total, self, wait and child times.
  • Loading branch information...
commit 6bed1978d7aa0e2d8ab33c5e1f701834c425a9fc 2 parents d8cd228 + 0cbd252
@rdp rdp authored
View
26 bin/ruby-prof
@@ -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',
+ ' 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
@@ -184,18 +203,19 @@ at_exit {
# Create a printer
printer = options.printer.new(result)
+ printer_options = {:min_percent => options.min_percent, :sort_method => options.sort_method}
# 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
}
View
8 lib/ruby-prof/abstract_printer.rb
@@ -20,6 +20,10 @@ 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.
+ # Available values are :total_time, :self_time,
+ # :wait_time, :children_time
+ # Default value is :total_time
def setup_options(options = {})
@options = options
end
@@ -32,6 +36,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
View
4 lib/ruby-prof/call_stack_printer.rb
@@ -8,10 +8,6 @@ module RubyProf
class CallStackPrinter < AbstractPrinter
include ERB::Util
- def initialize(result)
- super(result)
- end
-
# Specify print options.
#
# options - Hash table
View
6 lib/ruby-prof/dot_printer.rb
@@ -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}%)\"];"
View
11 lib/ruby-prof/flat_printer.rb
@@ -14,7 +14,7 @@ module RubyProf
class FlatPrinter < AbstractPrinter
# Print a flat profile report to the provided output.
#
- # output - Any IO oject, including STDOUT or a file.
+ # output - Any IO object, including STDOUT or a file.
# The default value is STDOUT.
#
# options - Hash of print options. See #setup_options
@@ -22,6 +22,9 @@ class FlatPrinter < AbstractPrinter
#
def print(output = STDOUT, options = {})
@output = output
+ # Now sort methods by largest self time by default,
+ # not total time like in other printouts
+ options[:sort_method] ||= :self_time
setup_options(options)
print_threads
end
@@ -43,11 +46,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
View
6 lib/ruby-prof/flat_printer_with_line_numbers.rb
@@ -21,11 +21,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
View
2  lib/ruby-prof/graph_html_printer.rb
@@ -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
View
2  lib/ruby-prof/graph_printer.rb
@@ -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
View
104 test/printers_test.rb
@@ -45,12 +45,12 @@ def test_printers
printer = RubyProf::CallTreePrinter.new(@result)
printer.print(output)
output_dir = 'examples2'
-
+
if ENV['SAVE_NEW_PRINTER_EXAMPLES']
output_dir = 'examples'
end
FileUtils.mkdir_p output_dir
-
+
printer = RubyProf::DotPrinter.new(@result)
File.open("#{output_dir}/graph.dot", "w") {|f| printer.print(f)}
@@ -153,6 +153,106 @@ def test_all_with_small_percentiles
end
+ def test_flat_result_sorting_by_self_time_is_default
+ printer = RubyProf::FlatPrinter.new(@result)
+
+ printer.print(output = '')
+ self_times = flat_output_nth_column_values(output, 3)
+
+ assert_sorted self_times
+ end
+
+ def test_flat_result_sorting
+ printer = RubyProf::FlatPrinter.new(@result)
+ sort_method_with_column_number = {:total_time => 2, :self_time => 3, :wait_time => 4, :children_time => 5}
+ sort_method_with_column_number.each_pair do |sort_method, n|
+ printer.print(output = '', :sort_method => sort_method)
+ times = flat_output_nth_column_values(output, n)
+ assert_sorted times
+ end
+ end
+
+ def test_flat_result_with_line_numbers_sorting_by_self_time_is_default
+ printer = RubyProf::FlatPrinterWithLineNumbers.new(@result)
+
+ printer.print(output = '')
+ self_times = flat_output_nth_column_values(output, 3)
+
+ assert_sorted self_times
+ end
+
+ def test_flat_with_line_numbers_result_sorting
+ printer = RubyProf::FlatPrinterWithLineNumbers.new(@result)
+
+ sort_method_with_column_number = {:total_time => 2, :self_time => 3, :wait_time => 4, :children_time => 5}
+
+ sort_method_with_column_number.each_pair do |sort_method, n|
+ printer.print(output = '', :sort_method => sort_method)
+ times = flat_output_nth_column_values(output, n)
+ assert_sorted times
+ end
+ end
+
+ def test_graph_result_sorting_by_total_time_is_default
+ printer = RubyProf::GraphPrinter.new(@result)
+ printer.print(output = '')
+ total_times = graph_output_nth_column_values(output, 3)
+
+ assert_sorted total_times
+ end
+
+ def test_graph_results_sorting
+ printer = RubyProf::GraphPrinter.new(@result)
+
+ sort_method_with_column_number = {:total_time => 3, :self_time => 4, :wait_time => 5, :children_time => 6}
+
+ sort_method_with_column_number.each_pair do |sort_method, n|
+ printer.print(output = '', :sort_method => sort_method)
+ times = graph_output_nth_column_values(output, n)
+ assert_sorted times
+ end
+ end
+
+ def test_graph_html_result_sorting_by_total_time_is_default
+ printer = RubyProf::GraphHtmlPrinter.new(@result)
+ printer.print(output = '')
+ total_times = graph_html_output_nth_column_values(output, 3)
+
+ assert_sorted total_times
+ end
+
+ def test_graph_html_result_sorting
+ printer = RubyProf::GraphHtmlPrinter.new(@result)
+
+ sort_method_with_column_number = {:total_time => 3, :self_time => 4, :wait_time => 5, :children_time => 6}
+
+ sort_method_with_column_number.each_pair do |sort_method, n|
+ printer.print(output = '', :sort_method => sort_method)
+ times = graph_html_output_nth_column_values(output, n)
+ assert_sorted times
+ end
+ end
+
+ private
+ def flat_output_nth_column_values(output, n)
+ only_method_calls = output.split("\n").select { |line| line =~ /^ +\d+/ }
+ only_method_calls.collect { |line| line.split(/ +/)[n] }
+ end
+
+ def graph_output_nth_column_values(output, n)
+ only_root_calls = output.split("\n").select { |line| line =~ /^ +[\d\.]+%/ }
+ only_root_calls.collect { |line| line.split(/ +/)[n] }
+ end
+
+ def graph_html_output_nth_column_values(output, n)
+ only_root_calls = output.split('<tr class="method">')
+ only_root_calls.delete_at(0)
+ only_root_calls.collect {|line| line.scan(/[\d\.]+/)[n - 1] }
+ end
+
+ def assert_sorted array
+ assert_equal array, array.sort.reverse, "Array #{array.inspect} is not sorted"
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.