class PrintersTest

  1. test/printers_test.rb

-- Tests ----

Public Instance methods

do_nothing ()
[show source]
# File test/printers_test.rb, line 129
def do_nothing
  start = Time.now
  while(Time.now == start)
  end
end
go ()
[show source]
# File test/printers_test.rb, line 10
def go
  run_primes(1000)
end
helper_test_flat_string (klass)
[show source]
# File test/printers_test.rb, line 72
def helper_test_flat_string klass
  output = ''

  printer = klass.new(@result)
  printer.print(output)

  assert_match(%rThread ID: -?\d+/, output)
  assert_match(%rTotal: \d+\.\d+/, output)
  assert_match(%rObject#run_primes/, output)
  output
end
setup ()
[show source]
# File test/printers_test.rb, line 14
def setup
  RubyProf::measure_mode = RubyProf::WALL_TIME # WALL_TIME so we can use sleep in our test and get same measurements on linux and doze
  @result = RubyProf.profile do
    begin
      run_primes(1000)
      go
    rescue => e
      p e
    end
  end

end
test_all_with_small_percentiles ()
[show source]
# File test/printers_test.rb, line 135
def test_all_with_small_percentiles

  result = RubyProf.profile do
    sleep 2
    do_nothing
  end

  # RubyProf::CallTreePrinter doesn't "do" a min_percent
  # RubyProf::FlatPrinter only outputs if self time > percent...
  # RubyProf::FlatPrinterWithLineNumbers same
  for klass in [ RubyProf::GraphPrinter, RubyProf::GraphHtmlPrinter]
    printer = klass.new(result)
    out = ''
    output = printer.print(out, :min_percent => 0.00000001 )
    assert_match(%rdo_nothing/, out)
  end

end
test_call_tree_string ()
[show source]
# File test/printers_test.rb, line 120
def test_call_tree_string
  output = ''
  printer = RubyProf::CallTreePrinter.new(@result)
  printer.print(output)
  assert_match(%rfn=Object#find_primes/, output)
  assert_match(%revents: wall_time/, output)
  assert_no_match(%rd\d\d\d\d\d/, output) # old bug looked [in error] like Object::run_primes(d5833116)
end
test_flat_result_sorting ()
[show source]
# File test/printers_test.rb, line 163
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
test_flat_result_sorting_by_self_time_is_default ()
[show source]
# File test/printers_test.rb, line 154
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
test_flat_result_with_line_numbers_sorting_by_self_time_is_default ()
[show source]
# File test/printers_test.rb, line 175
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
test_flat_string ()
[show source]
# File test/printers_test.rb, line 67
def test_flat_string
  output = helper_test_flat_string RubyProf::FlatPrinter
  assert_no_match(%rprime.rb/, output)
end
test_flat_string_with_numbers ()
[show source]
# File test/printers_test.rb, line 84
def test_flat_string_with_numbers
  output = helper_test_flat_string RubyProf::FlatPrinterWithLineNumbers
  assert_match(%rprime.rb/, output)
  assert_no_match(%rruby_runtime:0/, output)
  assert_match(%rcalled from/, output)

  # should combine common parents
  if RUBY_VERSION < '1.9'
    assert_equal(3, output.scan(%rObject#is_prime/).length)
  else
    # 1.9 inlines it's  Fixnum#- so we don't see as many
    assert_equal(2, output.scan(%rObject#is_prime/).length)
  end
  assert_no_match(%r\.\/test\/prime.rb/, output) # don't use relative paths
end
test_flat_with_line_numbers_result_sorting ()
[show source]
# File test/printers_test.rb, line 184
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
test_graph_html_result_sorting ()
[show source]
# File test/printers_test.rb, line 224
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
test_graph_html_result_sorting_by_total_time_is_default ()
[show source]
# File test/printers_test.rb, line 216
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
test_graph_html_string ()
[show source]
# File test/printers_test.rb, line 100
def test_graph_html_string
  output = ''
  printer = RubyProf::GraphHtmlPrinter.new(@result)
  printer.print(output)

  assert_match( %rDTD HTML 4\.01/, output )
  assert_match( %r{<th>Total Time</th>}, output )
  assert_match( %rObject#run_primes/, output )
end
test_graph_result_sorting_by_total_time_is_default ()
[show source]
# File test/printers_test.rb, line 196
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
test_graph_results_sorting ()
[show source]
# File test/printers_test.rb, line 204
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
test_graph_string ()
[show source]
# File test/printers_test.rb, line 110
def test_graph_string
  output = ''
  printer = RubyProf::GraphPrinter.new(@result)
  printer.print(output)

  assert_match( %rThread ID: -?\d+/, output )
  assert_match( %rTotal Time: \d+\.\d+/, output )
  assert_match( %rObject#run_primes/, output )
end
test_printers ()
[show source]
# File test/printers_test.rb, line 27
def test_printers
  assert_nothing_raised do
    output = ENV['SHOW_RUBY_PROF_PRINTER_OUTPUT'] == "1" ? STDOUT : StringIO.new('')

    printer = RubyProf::FlatPrinter.new(@result)
    printer.print(output)

    printer = RubyProf::FlatPrinterWithLineNumbers.new(@result)
    printer.print(output)

    printer = RubyProf::GraphHtmlPrinter.new(@result)
    printer.print(output)

    printer = RubyProf::GraphPrinter.new(@result)
    printer.print(output)

    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)}

    printer = RubyProf::CallStackPrinter.new(@result)
    File.open("#{output_dir}/stack.html", "w") {|f| printer.print(f, :application => "primes")}

    printer = RubyProf::MultiPrinter.new(@result)
    printer.print(:path => "#{output_dir}", :profile => "multi", :application => "primes")
    for file in ['empty.png', 'graph.dot', 'minus.png', 'multi.flat.txt', 'multi.graph.html', 'multi.grind.dat', 'multi.stack.html', 'plus.png', 'stack.html']
      existant_file = output_dir + '/' + file
      assert File.size(existant_file) > 0
    end
  end
end