File: block_test.rb

package info (click to toggle)
ruby-prof 0.17.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,656 kB
  • sloc: ruby: 5,043; ansic: 2,175; makefile: 6
file content (74 lines) | stat: -rwxr-xr-x 2,625 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env ruby
# encoding: UTF-8

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

class BlockMethodTest < TestCase
  def setup
    # Need to use wall time for this test due to the sleep calls
    RubyProf::measure_mode = RubyProf::WALL_TIME
  end

  def test_block
    result = RubyProf.profile do
      1.times { RubyProf::C1.new.hello }
    end

    # Methods called
    #  Kernel#sleep
    #  <Class::BasicObject>#allocate
    #  #{RubyProf.parent_object}#inizialize
    #  RubyProf::C1#hello
    #  Class#new
    #  Integer#times
    #  BlockMethodTest#test_block

    methods = result.threads.first.methods.sort.reverse
    assert_equal(RubyProf.ruby_2? ? 6 : 7, methods.length)

    # Check times
    assert_equal("BlockMethodTest#test_block", methods[0].full_name)
    assert_in_delta(0.2, methods[0].total_time, 0.02)
    assert_in_delta(0.0, methods[0].wait_time, 0.02)
    assert_in_delta(0.0, methods[0].self_time, 0.02)

    assert_equal("Integer#times", methods[1].full_name)
    assert_in_delta(0.2, methods[1].total_time, 0.02)
    assert_in_delta(0.0, methods[1].wait_time, 0.02)
    assert_in_delta(0.0, methods[1].self_time, 0.02)

    assert_equal("RubyProf::C1#hello", methods[2].full_name)
    assert_in_delta(0.2, methods[2].total_time, 0.02)
    assert_in_delta(0.0, methods[2].wait_time, 0.02)
    assert_in_delta(0.0, methods[2].self_time, 0.02)

    assert_equal("Kernel#sleep", methods[3].full_name)
    assert_in_delta(0.2, methods[3].total_time, 0.01)
    assert_in_delta(0.0, methods[3].wait_time, 0.01)
    assert_in_delta(0.2, methods[3].self_time, 0.01)

    assert_equal("Class#new", methods[4].full_name)
    assert_in_delta(0.0, methods[4].total_time, 0.01)
    assert_in_delta(0.0, methods[4].wait_time, 0.01)
    assert_in_delta(0.0, methods[4].self_time, 0.01)

    # the timing difference between #initialize and #allocate is so small
    # that we cannot rely on #initialize appearing first.
    # so switch them, if necessary
    if methods[5].full_name =~ /#allocate/
      methods[5], methods[6] = methods[6], methods[5]
    end

    assert_equal("#{RubyProf.parent_object}#initialize", methods[5].full_name)
    assert_in_delta(0.0, methods[5].total_time, 0.01)
    assert_in_delta(0.0, methods[5].wait_time, 0.01)
    assert_in_delta(0.0, methods[5].self_time, 0.01)

    unless RubyProf.ruby_2?
      assert_equal("<Class::#{RubyProf.parent_object}>#allocate", methods[6].full_name)
      assert_in_delta(0.0, methods[6].total_time, 0.01)
      assert_in_delta(0.0, methods[6].wait_time, 0.01)
      assert_in_delta(0.0, methods[6].self_time, 0.01)
    end
  end
end