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 145 146
|
#!/usr/bin/env ruby
# encoding: UTF-8
require File.expand_path('../test_helper', __FILE__)
module ExcludeMethodsModule
def c
1.times { |i| ExcludeMethodsModule.d }
end
def self.d
1.times { |i| ExcludeMethodsClass.e }
end
end
class ExcludeMethodsClass
include ExcludeMethodsModule
def a
1.times { |i| b }
end
def b
1.times { |i| c; self.class.e }
end
def self.e
1.times { |i| f }
end
def self.f
sleep 0.1
end
end
class ExcludeMethodsTest < 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_methods_can_be_profiled
obj = ExcludeMethodsClass.new
prf = RubyProf::Profile.new
result = prf.profile { 5.times {obj.a} }
methods = result.threads.first.methods.sort.reverse
assert_equal(10, methods.count)
assert_equal('ExcludeMethodsTest#test_methods_can_be_profiled', methods[0].full_name)
assert_equal('Integer#times', methods[1].full_name)
assert_equal('ExcludeMethodsClass#a', methods[2].full_name)
assert_equal('ExcludeMethodsClass#b', methods[3].full_name)
assert_equal('<Class::ExcludeMethodsClass>#e', methods[4].full_name)
assert_equal('<Class::ExcludeMethodsClass>#f', methods[5].full_name)
assert_equal('Kernel#sleep', methods[6].full_name)
assert_equal('ExcludeMethodsModule#c', methods[7].full_name)
assert_equal('<Module::ExcludeMethodsModule>#d', methods[8].full_name)
assert_equal('Kernel#class', methods[9].full_name)
end
def test_methods_can_be_hidden1
obj = ExcludeMethodsClass.new
prf = RubyProf::Profile.new
prf.exclude_methods!(Integer, :times)
result = prf.profile { 5.times {obj.a} }
methods = result.threads.first.methods.sort.reverse
assert_equal(9, methods.count)
assert_equal('ExcludeMethodsTest#test_methods_can_be_hidden1', methods[0].full_name)
assert_equal('ExcludeMethodsClass#a', methods[1].full_name)
assert_equal('ExcludeMethodsClass#b', methods[2].full_name)
assert_equal('<Class::ExcludeMethodsClass>#e', methods[3].full_name)
assert_equal('<Class::ExcludeMethodsClass>#f', methods[4].full_name)
assert_equal('Kernel#sleep', methods[5].full_name)
assert_equal('ExcludeMethodsModule#c', methods[6].full_name)
assert_equal('<Module::ExcludeMethodsModule>#d', methods[7].full_name)
assert_equal('Kernel#class', methods[8].full_name)
end
def test_methods_can_be_hidden2
obj = ExcludeMethodsClass.new
prf = RubyProf::Profile.new
prf.exclude_methods!(Integer, :times)
prf.exclude_methods!(ExcludeMethodsClass.singleton_class, :f)
prf.exclude_methods!(ExcludeMethodsModule.singleton_class, :d)
result = prf.profile { 5.times {obj.a} }
methods = result.threads.first.methods.sort.reverse
assert_equal(7, methods.count)
assert_equal('ExcludeMethodsTest#test_methods_can_be_hidden2', methods[0].full_name)
assert_equal('ExcludeMethodsClass#a', methods[1].full_name)
assert_equal('ExcludeMethodsClass#b', methods[2].full_name)
assert_equal('<Class::ExcludeMethodsClass>#e', methods[3].full_name)
assert_equal('Kernel#sleep', methods[4].full_name)
assert_equal('ExcludeMethodsModule#c', methods[5].full_name)
assert_equal('Kernel#class', methods[6].full_name)
end
def test_exclude_common_methods1
obj = ExcludeMethodsClass.new
prf = RubyProf::Profile.new
prf.exclude_common_methods!
result = prf.profile { 5.times {obj.a} }
methods = result.threads.first.methods.sort.reverse
assert_equal(9, methods.count)
assert_equal('ExcludeMethodsTest#test_exclude_common_methods1', methods[0].full_name)
assert_equal('ExcludeMethodsClass#a', methods[1].full_name)
assert_equal('ExcludeMethodsClass#b', methods[2].full_name)
end
def test_exclude_common_methods2
obj = ExcludeMethodsClass.new
result = RubyProf.profile(exclude_common: true) { 5.times {obj.a} }
methods = result.threads.first.methods.sort.reverse
assert_equal(9, methods.count)
assert_equal('ExcludeMethodsTest#test_exclude_common_methods2', methods[0].full_name)
assert_equal('ExcludeMethodsClass#a', methods[1].full_name)
assert_equal('ExcludeMethodsClass#b', methods[2].full_name)
end
private
def assert_method_has_been_eliminated(result, eliminated_method)
result.threads.each do |thread|
thread.methods.each do |method|
method.call_infos.each do |ci|
assert(ci.target != eliminated_method, "broken self")
assert(ci.parent.target != eliminated_method, "broken parent") if ci.parent
ci.children.each do |callee|
assert(callee.target != eliminated_method, "broken kid")
end
end
end
end
end
end
|