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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
#!/usr/bin/env ruby
require 'test/unit'
require 'ruby-prof'
require 'test_helper'
# Need to use wall time for this test due to the sleep calls
RubyProf::measure_mode = RubyProf::WALL_TIME
def simple(n)
sleep(1)
n -= 1
return if n == 0
simple(n)
end
def cycle(n)
sub_cycle(n)
end
def sub_cycle(n)
sleep(1)
n -= 1
return if n == 0
cycle(n)
end
def factorial(n)
if n < 2 then
n
else
n * factorial(n-1)
end
end
# -- Tests ----
class RecursiveTest < Test::Unit::TestCase
def test_recursive
result = RubyProf.profile do
simple(2)
end
result.threads.values.each do |methods|
methods.each do |method|
check_parent_times(method)
check_parent_calls(method)
check_child_times(method)
end
end
methods = result.threads.values.first.sort.reverse
assert_equal(6, methods.length)
method = methods[0]
assert_equal('RecursiveTest#test_recursive', method.full_name)
assert_in_delta(2, method.total_time, 0.02)
assert_in_delta(0, method.self_time, 0.02)
assert_in_delta(0, method.wait_time, 0.02)
assert_in_delta(2, method.children_time, 0.02)
assert_equal(0, method.called)
assert_equal(0, method.parents.length)
assert_equal(1, method.children.length)
method = methods[1]
assert_equal('Object#simple', method.full_name)
assert_in_delta(2, method.total_time, 0.02)
assert_in_delta(0, method.self_time, 0.02)
assert_in_delta(0, method.wait_time, 0.02)
assert_in_delta(2, method.children_time, 0.02)
assert_equal(1, method.called)
assert_equal(1, method.parents.length)
assert_equal(4, method.children.length)
method = methods[2]
assert_equal('Kernel#sleep', method.full_name)
assert_in_delta(2, method.total_time, 0.02)
assert_in_delta(2, method.self_time, 0.02)
assert_in_delta(0, method.wait_time, 0.02)
assert_in_delta(0, method.children_time, 0.02)
assert_equal(2, method.called)
assert_equal(2, method.parents.length)
assert_equal(0, method.children.length)
method = methods[3]
assert_equal('Object#simple-1', method.full_name)
assert_in_delta(1, method.total_time, 0.02)
assert_in_delta(0, method.self_time, 0.02)
assert_in_delta(0, method.wait_time, 0.02)
assert_in_delta(1, method.children_time, 0.02)
assert_equal(1, method.called)
assert_equal(1, method.parents.length)
assert_equal(3, method.children.length)
method = methods[4]
assert_equal('Fixnum#==', method.full_name)
assert_in_delta(0, method.total_time, 0.02)
assert_in_delta(0, method.self_time, 0.02)
assert_in_delta(0, method.wait_time, 0.02)
assert_in_delta(0, method.children_time, 0.02)
assert_equal(2, method.called)
assert_equal(2, method.parents.length)
assert_equal(0, method.children.length)
method = methods[5]
assert_equal('Fixnum#-', method.full_name)
assert_in_delta(0, method.total_time, 0.02)
assert_in_delta(0, method.self_time, 0.02)
assert_in_delta(0, method.wait_time, 0.02)
assert_in_delta(0, method.children_time, 0.02)
assert_equal(2, method.called)
assert_equal(2, method.parents.length)
assert_equal(0, method.children.length)
end
def test_cycle
result = RubyProf.profile do
cycle(2)
end
result.threads.values.each do |methods|
methods.each do |method|
check_parent_times(method)
check_parent_calls(method)
check_child_times(method)
end
end
end
def test_factorial
result = RubyProf.profile do
# Around 700 on windows causes "stack level too deep" error
factorial(650)
end
result.threads.values.each do |methods|
methods.each do |method|
check_parent_times(method)
check_parent_calls(method)
check_child_times(method)
end
end
end
end
|