Permalink
Browse files

Added tests for sorting behaviour & removed options from constructor …

…to print method.
  • Loading branch information...
1 parent 0d18354 commit 0cbd2520a8de9cbc71bc50461c0d9f37712ff7b8 @jsuchal jsuchal committed Sep 16, 2011
View
@@ -107,7 +107,7 @@ opts = OptionParser.new do |opts|
opts.on('-s sort_mode', '--sort=sort_mode', [:total, :self, :wait, :child],
'Select how ruby-prof results should be sorted:',
- ' total - Total time (default)',
+ ' total - Total time',
' self - Self time',
' wait - Wait time',
' child - Child time') do |sort_mode|
@@ -202,8 +202,8 @@ 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
@@ -2,10 +2,10 @@
module RubyProf
class AbstractPrinter
- def initialize(result, options = {})
+ def initialize(result)
@result = result
@output = nil
- @options = options
+ @options = {}
end
# Specify print options.
@@ -23,8 +23,8 @@ class DotPrinter < RubyProf::AbstractPrinter
EDGE_COLOR = '"#666666"'
# Creates the DotPrinter using a RubyProf::Result.
- def initialize(result, options = {})
- super(result, options)
+ def initialize(result)
+ super(result)
@seen_methods = Set.new
end
@@ -12,24 +12,19 @@ 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.
+ # output - Any IO object, 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
+ # 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
@@ -13,13 +13,6 @@ 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
@@ -30,8 +30,8 @@ class GraphHtmlPrinter < AbstractPrinter
# Create a GraphPrinter. Result is a RubyProf::Result
# object generated from a profiling run.
- def initialize(result, options = {})
- super(result, options)
+ def initialize(result)
+ super(result)
@thread_times = Hash.new
calculate_thread_times
end
@@ -20,8 +20,8 @@ class GraphPrinter < AbstractPrinter
# Create a GraphPrinter. Result is a RubyProf::Result
# object generated from a profiling run.
- def initialize(result, options = {})
- super(result, options)
+ def initialize(result)
+ super(result)
@thread_times = Hash.new
calculate_thread_times
end
@@ -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, 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)
+ def initialize(result)
+ @stack_printer = CallStackPrinter.new(result)
+ @graph_printer = GraphHtmlPrinter.new(result)
+ @tree_printer = CallTreePrinter.new(result)
+ @flat_printer = FlatPrinter.new(result)
end
# create profile files under options[:path] or the current
View
@@ -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

0 comments on commit 0cbd252

Please sign in to comment.