Sha256: bda8d5da9679c298158e78e14d7ae8a56d32c4dedd2daa99bd5efd3759ec9342

Contents?: true

Size: 1.53 KB

Versions: 12

Compression:

Stored size: 1.53 KB

Contents

#!/usr/bin/env ruby
# encoding: UTF-8

require File.expand_path('../test_helper', __FILE__)

# Test data
#     A
#    / \
#   B   C
#        \
#         B

class STPT
  def a
    100.times{b}
    300.times{c}
    c;c;c
  end

  def b
    sleep 0
  end

  def c
    5.times{b}
  end
end

class StackPrinterTest < TestCase
  def test_stack_can_be_printed
    start_time = Time.now
    result = RubyProf::Profile.profile(measure_mode: RubyProf::WALL_TIME) do
      5.times{STPT.new.a}
    end

    end_time = Time.now
    expected_time = end_time - start_time

    file_contents = nil
    file_contents = print(result)
    re = /Thread: (\d+)(, Fiber: (\d+))? \([\.0-9]+.[\.0-9]+% ~ ([\.0-9]+)\)/
    assert_match(re, file_contents)
    file_contents =~ re
    actual_time = $4.to_f
    assert_in_delta(expected_time, actual_time, 0.1)
  end

  private

  def print(result)
    test = caller.first =~ /in `(.*)'/ ? $1 : "test"
    testfile_name = "#{Dir.tmpdir}/ruby_prof_#{test}.html"
    # puts "printing to #{testfile_name}"
    printer = RubyProf::CallStackPrinter.new(result)
    File.open(testfile_name, "w") {|f| printer.print(f, :threshold => 0, :min_percent => 0, :title => "ruby_prof #{test}")}
    system("open '#{testfile_name}'") if RUBY_PLATFORM =~ /darwin/ && ENV['SHOW_RUBY_PROF_PRINTER_OUTPUT']=="1"
    assert File.exist?(testfile_name), "#{testfile_name} does not exist"
    assert File.readable?(testfile_name), "#{testfile_name} is no readable"
    File.read(testfile_name)
  end
end

Version data entries

12 entries across 12 versions & 2 rubygems

Version Path
ruby-prof-1.7.1-x64-mingw-ucrt test/stack_printer_test.rb
ruby-prof-1.7.1 test/stack_printer_test.rb
ruby-prof-1.7.0-x64-mingw-ucrt test/stack_printer_test.rb
ruby-prof-1.7.0 test/stack_printer_test.rb
honeybadger-5.4.0 vendor/bundle/ruby/3.2.0/gems/ruby-prof-1.6.3/test/stack_printer_test.rb
honeybadger-5.3.0 vendor/bundle/ruby/3.2.0/gems/ruby-prof-1.6.3/test/stack_printer_test.rb
ruby-prof-1.6.3-x64-mingw-ucrt test/stack_printer_test.rb
ruby-prof-1.6.3 test/stack_printer_test.rb
ruby-prof-1.6.2-x64-mingw-ucrt test/stack_printer_test.rb
ruby-prof-1.6.2 test/stack_printer_test.rb
ruby-prof-1.6.1 test/stack_printer_test.rb
ruby-prof-1.6.1-x64-mingw-ucrt test/stack_printer_test.rb