ruby-prof 0.18.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (129) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGES +44 -1
  3. data/LICENSE +2 -2
  4. data/README.rdoc +1 -483
  5. data/Rakefile +3 -6
  6. data/bin/ruby-prof +111 -128
  7. data/ext/ruby_prof/extconf.rb +6 -38
  8. data/ext/ruby_prof/rp_aggregate_call_tree.c +41 -0
  9. data/ext/ruby_prof/rp_aggregate_call_tree.h +13 -0
  10. data/ext/ruby_prof/rp_allocation.c +259 -0
  11. data/ext/ruby_prof/rp_allocation.h +31 -0
  12. data/ext/ruby_prof/rp_call_tree.c +353 -0
  13. data/ext/ruby_prof/rp_call_tree.h +43 -0
  14. data/ext/ruby_prof/rp_call_trees.c +266 -0
  15. data/ext/ruby_prof/rp_call_trees.h +29 -0
  16. data/ext/ruby_prof/rp_measure_allocations.c +25 -51
  17. data/ext/ruby_prof/rp_measure_memory.c +21 -56
  18. data/ext/ruby_prof/rp_measure_process_time.c +37 -43
  19. data/ext/ruby_prof/rp_measure_wall_time.c +40 -21
  20. data/ext/ruby_prof/rp_measurement.c +221 -0
  21. data/ext/ruby_prof/rp_measurement.h +50 -0
  22. data/ext/ruby_prof/rp_method.c +279 -439
  23. data/ext/ruby_prof/rp_method.h +33 -45
  24. data/ext/ruby_prof/rp_profile.c +902 -0
  25. data/ext/ruby_prof/rp_profile.h +36 -0
  26. data/ext/ruby_prof/rp_stack.c +163 -132
  27. data/ext/ruby_prof/rp_stack.h +18 -28
  28. data/ext/ruby_prof/rp_thread.c +192 -124
  29. data/ext/ruby_prof/rp_thread.h +18 -8
  30. data/ext/ruby_prof/ruby_prof.c +36 -778
  31. data/ext/ruby_prof/ruby_prof.h +11 -45
  32. data/ext/ruby_prof/vc/ruby_prof.vcxproj +18 -12
  33. data/lib/ruby-prof.rb +4 -21
  34. data/lib/ruby-prof/assets/call_stack_printer.html.erb +710 -0
  35. data/lib/ruby-prof/assets/call_stack_printer.png +0 -0
  36. data/lib/ruby-prof/assets/graph_printer.html.erb +355 -0
  37. data/lib/ruby-prof/call_tree.rb +57 -0
  38. data/lib/ruby-prof/call_tree_visitor.rb +36 -0
  39. data/lib/ruby-prof/compatibility.rb +37 -107
  40. data/lib/ruby-prof/exclude_common_methods.rb +198 -0
  41. data/lib/ruby-prof/measurement.rb +17 -0
  42. data/lib/ruby-prof/method_info.rb +47 -90
  43. data/lib/ruby-prof/printers/abstract_printer.rb +73 -50
  44. data/lib/ruby-prof/printers/call_info_printer.rb +24 -12
  45. data/lib/ruby-prof/printers/call_stack_printer.rb +66 -152
  46. data/lib/ruby-prof/printers/call_tree_printer.rb +20 -12
  47. data/lib/ruby-prof/printers/dot_printer.rb +5 -5
  48. data/lib/ruby-prof/printers/flat_printer.rb +6 -24
  49. data/lib/ruby-prof/printers/graph_html_printer.rb +6 -192
  50. data/lib/ruby-prof/printers/graph_printer.rb +11 -14
  51. data/lib/ruby-prof/printers/multi_printer.rb +66 -23
  52. data/lib/ruby-prof/profile.rb +10 -3
  53. data/lib/ruby-prof/thread.rb +5 -20
  54. data/lib/ruby-prof/version.rb +1 -1
  55. data/ruby-prof.gemspec +9 -2
  56. data/test/abstract_printer_test.rb +0 -27
  57. data/test/alias_test.rb +126 -0
  58. data/test/basic_test.rb +1 -86
  59. data/test/call_tree_visitor_test.rb +32 -0
  60. data/test/call_trees_test.rb +66 -0
  61. data/test/dynamic_method_test.rb +0 -2
  62. data/test/exclude_methods_test.rb +17 -12
  63. data/test/fiber_test.rb +214 -23
  64. data/test/gc_test.rb +105 -0
  65. data/test/inverse_call_tree_test.rb +175 -0
  66. data/test/line_number_test.rb +118 -40
  67. data/test/marshal_test.rb +115 -0
  68. data/test/measure_allocations.rb +30 -0
  69. data/test/measure_allocations_test.rb +361 -12
  70. data/test/measure_allocations_trace_test.rb +375 -0
  71. data/test/measure_memory_trace_test.rb +1101 -0
  72. data/test/measure_process_time_test.rb +757 -33
  73. data/test/measure_times.rb +56 -0
  74. data/test/measure_wall_time_test.rb +329 -149
  75. data/test/multi_printer_test.rb +1 -34
  76. data/test/pause_resume_test.rb +24 -15
  77. data/test/prime.rb +1 -1
  78. data/test/prime_script.rb +6 -0
  79. data/test/printer_call_stack_test.rb +28 -0
  80. data/test/printer_call_tree_test.rb +31 -0
  81. data/test/printer_flat_test.rb +68 -0
  82. data/test/printer_graph_html_test.rb +60 -0
  83. data/test/printer_graph_test.rb +41 -0
  84. data/test/printers_test.rb +32 -166
  85. data/test/printing_recursive_graph_test.rb +26 -72
  86. data/test/recursive_test.rb +68 -77
  87. data/test/stack_printer_test.rb +2 -15
  88. data/test/start_stop_test.rb +22 -25
  89. data/test/test_helper.rb +6 -261
  90. data/test/thread_test.rb +11 -54
  91. data/test/unique_call_path_test.rb +25 -107
  92. data/test/yarv_test.rb +1 -0
  93. metadata +43 -41
  94. data/examples/flat.txt +0 -50
  95. data/examples/graph.dot +0 -84
  96. data/examples/graph.html +0 -823
  97. data/examples/graph.txt +0 -139
  98. data/examples/multi.flat.txt +0 -23
  99. data/examples/multi.graph.html +0 -760
  100. data/examples/multi.grind.dat +0 -114
  101. data/examples/multi.stack.html +0 -547
  102. data/examples/stack.html +0 -547
  103. data/ext/ruby_prof/rp_call_info.c +0 -425
  104. data/ext/ruby_prof/rp_call_info.h +0 -53
  105. data/ext/ruby_prof/rp_measure.c +0 -40
  106. data/ext/ruby_prof/rp_measure.h +0 -45
  107. data/ext/ruby_prof/rp_measure_cpu_time.c +0 -136
  108. data/ext/ruby_prof/rp_measure_gc_runs.c +0 -73
  109. data/ext/ruby_prof/rp_measure_gc_time.c +0 -60
  110. data/lib/ruby-prof/aggregate_call_info.rb +0 -76
  111. data/lib/ruby-prof/assets/call_stack_printer.css.html +0 -117
  112. data/lib/ruby-prof/assets/call_stack_printer.js.html +0 -385
  113. data/lib/ruby-prof/call_info.rb +0 -115
  114. data/lib/ruby-prof/call_info_visitor.rb +0 -40
  115. data/lib/ruby-prof/printers/flat_printer_with_line_numbers.rb +0 -83
  116. data/lib/ruby-prof/profile/exclude_common_methods.rb +0 -207
  117. data/lib/ruby-prof/profile/legacy_method_elimination.rb +0 -50
  118. data/test/aggregate_test.rb +0 -136
  119. data/test/block_test.rb +0 -74
  120. data/test/call_info_test.rb +0 -78
  121. data/test/call_info_visitor_test.rb +0 -31
  122. data/test/issue137_test.rb +0 -63
  123. data/test/measure_cpu_time_test.rb +0 -212
  124. data/test/measure_gc_runs_test.rb +0 -32
  125. data/test/measure_gc_time_test.rb +0 -36
  126. data/test/measure_memory_test.rb +0 -33
  127. data/test/method_elimination_test.rb +0 -84
  128. data/test/module_test.rb +0 -45
  129. data/test/stack_test.rb +0 -138
@@ -0,0 +1,56 @@
1
+ # Some classes used in measurement tests
2
+ require 'singleton'
3
+
4
+ module RubyProf
5
+ class C1
6
+ def C1.sleep_wait
7
+ sleep(0.1)
8
+ end
9
+
10
+ def C1.busy_wait
11
+ starting = Process.clock_gettime(Process::CLOCK_MONOTONIC)
12
+ while (Process.clock_gettime(Process::CLOCK_MONOTONIC) - starting) < 0.1
13
+ end
14
+ end
15
+
16
+ def sleep_wait
17
+ sleep(0.2)
18
+ end
19
+
20
+ def busy_wait
21
+ starting = Process.clock_gettime(Process::CLOCK_MONOTONIC)
22
+ while (Process.clock_gettime(Process::CLOCK_MONOTONIC) - starting) < 0.2
23
+ end
24
+ end
25
+ end
26
+
27
+ module M1
28
+ def sleep_wait
29
+ sleep(0.3)
30
+ end
31
+
32
+ def busy_wait
33
+ starting = Process.clock_gettime(Process::CLOCK_MONOTONIC)
34
+ while (Process.clock_gettime(Process::CLOCK_MONOTONIC) - starting) < 0.3
35
+ end
36
+ end
37
+ end
38
+
39
+ class C2
40
+ include M1
41
+ extend M1
42
+ end
43
+
44
+ class C3
45
+ include Singleton
46
+ def sleep_wait
47
+ sleep(0.3)
48
+ end
49
+
50
+ def busy_wait
51
+ starting = Process.clock_gettime(Process::CLOCK_MONOTONIC)
52
+ while (Process.clock_gettime(Process::CLOCK_MONOTONIC) - starting) < 0.2
53
+ end
54
+ end
55
+ end
56
+ end
@@ -2,6 +2,7 @@
2
2
  # encoding: UTF-8
3
3
 
4
4
  require File.expand_path('../test_helper', __FILE__)
5
+ require_relative './measure_times'
5
6
 
6
7
  class MeasureWallTimeTest < TestCase
7
8
  def setup
@@ -14,242 +15,421 @@ class MeasureWallTimeTest < TestCase
14
15
  assert_equal(RubyProf::WALL_TIME, RubyProf::measure_mode)
15
16
  end
16
17
 
17
- def test_wall_time_enabled_defined
18
- assert(defined?(RubyProf::WALL_TIME_ENABLED))
19
- end
20
-
21
18
  def test_class_methods
22
19
  result = RubyProf.profile do
23
- RubyProf::C1.hello
20
+ RubyProf::C1.sleep_wait
24
21
  end
25
22
 
26
23
  thread = result.threads.first
27
- assert_in_delta(0.1, thread.total_time, 0.01)
28
-
29
- top_methods = thread.top_methods
30
- assert_equal(1, top_methods.count)
31
- assert_equal("MeasureWallTimeTest#test_class_methods", top_methods[0].full_name)
32
-
33
- # Length should be 3:
34
- # MeasureWallTimeTest#test_class_methods
35
- # <Class::RubyProf::C1>#hello
36
- # Kernel#sleep
24
+ assert_in_delta(0.1, thread.total_time, 0.03)
37
25
 
38
26
  methods = result.threads.first.methods.sort.reverse
39
27
  assert_equal(3, methods.length)
40
28
 
41
29
  # Check the names
42
30
  assert_equal('MeasureWallTimeTest#test_class_methods', methods[0].full_name)
43
- assert_equal('<Class::RubyProf::C1>#hello', methods[1].full_name)
31
+ assert_equal('<Class::RubyProf::C1>#sleep_wait', methods[1].full_name)
44
32
  assert_equal('Kernel#sleep', methods[2].full_name)
45
33
 
46
34
  # Check times
47
- assert_in_delta(0.1, methods[0].total_time, 0.01)
48
- assert_in_delta(0, methods[0].wait_time, 0.01)
49
- assert_in_delta(0, methods[0].self_time, 0.01)
35
+ assert_in_delta(0.1, methods[0].total_time, 0.03)
36
+ assert_in_delta(0, methods[0].wait_time, 0.03)
37
+ assert_in_delta(0, methods[0].self_time, 0.03)
50
38
 
51
- assert_in_delta(0.1, methods[1].total_time, 0.01)
52
- assert_in_delta(0, methods[1].wait_time, 0.01)
53
- assert_in_delta(0, methods[1].self_time, 0.01)
39
+ assert_in_delta(0.1, methods[1].total_time, 0.03)
40
+ assert_in_delta(0, methods[1].wait_time, 0.03)
41
+ assert_in_delta(0, methods[1].self_time, 0.03)
54
42
 
55
- assert_in_delta(0.1, methods[2].total_time, 0.01)
56
- assert_in_delta(0, methods[2].wait_time, 0.01)
57
- assert_in_delta(0.1, methods[2].self_time, 0.01)
43
+ assert_in_delta(0.1, methods[2].total_time, 0.03)
44
+ assert_in_delta(0, methods[2].wait_time, 0.03)
45
+ assert_in_delta(0.1, methods[2].self_time, 0.03)
58
46
  end
59
47
 
60
- def test_instance_methods
48
+ def test_class_methods_threaded
61
49
  result = RubyProf.profile do
62
- RubyProf::C1.new.hello
50
+ background_thread = Thread.new do
51
+ RubyProf::C1.sleep_wait
52
+ end
53
+ background_thread.join
63
54
  end
64
55
 
56
+ assert_equal(2, result.threads.count)
57
+
65
58
  thread = result.threads.first
66
- assert_in_delta(0.2, thread.total_time, 0.01)
59
+ assert_in_delta(0.1, thread.total_time, 0.03)
67
60
 
68
- top_methods = thread.top_methods
69
- assert_equal(1, top_methods.count)
70
- assert_equal("MeasureWallTimeTest#test_instance_methods", top_methods[0].full_name)
61
+ methods = result.threads.first.methods.sort.reverse
62
+ assert_equal(4, methods.length)
71
63
 
72
- # Methods called
73
- # MeasureWallTimeTest#test_instance_methods
74
- # Class.new
75
- # Class:Object#allocate
76
- # for Object#initialize
77
- # C1#hello
78
- # Kernel#sleep
64
+ # Check times
65
+ assert_equal('MeasureWallTimeTest#test_class_methods_threaded', methods[0].full_name)
66
+ assert_in_delta(0.1, methods[0].total_time, 0.03)
67
+ assert_in_delta(0.0, methods[0].wait_time, 0.03)
68
+ assert_in_delta(0.0, methods[0].self_time, 0.03)
69
+ assert_in_delta(0.1, methods[0].children_time, 0.03)
70
+
71
+ assert_equal('Thread#join', methods[1].full_name)
72
+ assert_in_delta(0.1, methods[1].total_time, 0.03)
73
+ assert_in_delta(0.1, methods[1].wait_time, 0.03)
74
+ assert_in_delta(0.0, methods[1].self_time, 0.03)
75
+ assert_in_delta(0.0, methods[1].children_time, 0.03)
76
+
77
+ assert_equal('<Class::Thread>#new', methods[2].full_name)
78
+ assert_in_delta(0.0, methods[2].total_time, 0.03)
79
+ assert_in_delta(0.0, methods[2].wait_time, 0.03)
80
+ assert_in_delta(0.0, methods[2].self_time, 0.03)
81
+ assert_in_delta(0.0, methods[2].children_time, 0.03)
82
+
83
+ assert_equal('Thread#initialize', methods[3].full_name)
84
+ assert_in_delta(0.0, methods[3].total_time, 0.03)
85
+ assert_in_delta(0.0, methods[3].wait_time, 0.03)
86
+ assert_in_delta(0.0, methods[3].self_time, 0.03)
87
+ assert_in_delta(0.0, methods[3].children_time, 0.03)
88
+
89
+ thread = result.threads.last
90
+ assert_in_delta(0.1, thread.total_time, 0.03)
79
91
 
80
92
  methods = result.threads.first.methods.sort.reverse
81
- assert_equal(RubyProf.ruby_2? ? 5 : 6, methods.length)
93
+ assert_equal(4, methods.length)
94
+
95
+ methods = result.threads.last.methods.sort.reverse
96
+ assert_equal(3, methods.length)
97
+
98
+ # Check times
99
+ assert_equal('MeasureWallTimeTest#test_class_methods_threaded', methods[0].full_name)
100
+ assert_in_delta(0.1, methods[0].total_time, 0.03)
101
+ assert_in_delta(0.0, methods[0].wait_time, 0.03)
102
+ assert_in_delta(0.0, methods[0].self_time, 0.03)
103
+ assert_in_delta(0.1, methods[0].children_time, 0.03)
104
+
105
+ assert_equal('<Class::RubyProf::C1>#sleep_wait', methods[1].full_name)
106
+ assert_in_delta(0.1, methods[1].total_time, 0.03)
107
+ assert_in_delta(0.0, methods[1].wait_time, 0.03)
108
+ assert_in_delta(0.0, methods[1].self_time, 0.03)
109
+ assert_in_delta(0.1, methods[1].children_time, 0.03)
110
+
111
+ assert_equal('Kernel#sleep', methods[2].full_name)
112
+ assert_in_delta(0.1, methods[2].total_time, 0.03)
113
+ assert_in_delta(0.0, methods[2].wait_time, 0.03)
114
+ assert_in_delta(0.1, methods[2].self_time, 0.03)
115
+ assert_in_delta(0.0, methods[2].children_time, 0.03)
116
+ end
117
+
118
+ def test_instance_methods
119
+ result = RubyProf.profile do
120
+ RubyProf::C1.new.sleep_wait
121
+ end
122
+
123
+ thread = result.threads.first
124
+ assert_in_delta(0.2, thread.total_time, 0.03)
125
+
126
+ methods = result.threads.first.methods.sort.reverse
127
+ assert_equal(5, methods.length)
82
128
  names = methods.map(&:full_name)
83
- assert_equal('MeasureWallTimeTest#test_instance_methods', names[0])
84
- assert_equal('RubyProf::C1#hello', names[1])
85
- assert_equal('Kernel#sleep', names[2])
86
- assert_equal('Class#new', names[3])
87
129
 
88
130
  # order can differ
89
- assert(names.include?("#{RubyProf.parent_object}#initialize"))
90
- unless RubyProf.ruby_2?
91
- assert(names.include?("<Class::#{RubyProf.parent_object}>#allocate"))
92
- end
131
+ assert(names.include?("BasicObject#initialize"))
93
132
 
94
133
  # Check times
95
- assert_in_delta(0.2, methods[0].total_time, 0.02)
96
- assert_in_delta(0, methods[0].wait_time, 0.02)
97
- assert_in_delta(0, methods[0].self_time, 0.02)
98
-
99
- assert_in_delta(0.2, methods[1].total_time, 0.02)
100
- assert_in_delta(0, methods[1].wait_time, 0.02)
101
- assert_in_delta(0, methods[1].self_time, 0.02)
102
-
103
- assert_in_delta(0.2, methods[2].total_time, 0.02)
104
- assert_in_delta(0, methods[2].wait_time, 0.02)
105
- assert_in_delta(0.2, methods[2].self_time, 0.02)
106
-
107
- assert_in_delta(0, methods[3].total_time, 0.01)
108
- assert_in_delta(0, methods[3].wait_time, 0.01)
109
- assert_in_delta(0, methods[3].self_time, 0.01)
110
-
111
- assert_in_delta(0, methods[4].total_time, 0.01)
112
- assert_in_delta(0, methods[4].wait_time, 0.01)
113
- assert_in_delta(0, methods[4].self_time, 0.01)
114
-
115
- unless RubyProf.ruby_2?
116
- assert_in_delta(0, methods[5].total_time, 0.01)
117
- assert_in_delta(0, methods[5].wait_time, 0.01)
118
- assert_in_delta(0, methods[5].self_time, 0.01)
134
+ method = methods[0]
135
+ assert_equal('MeasureWallTimeTest#test_instance_methods', method.full_name)
136
+ assert_in_delta(0.2, method.total_time, 0.03)
137
+ assert_in_delta(0, method.wait_time, 0.03)
138
+ assert_in_delta(0, method.self_time, 0.03)
139
+
140
+ method = methods[1]
141
+ assert_equal('RubyProf::C1#sleep_wait', method.full_name)
142
+ assert_in_delta(0.2, method.total_time, 0.03)
143
+ assert_in_delta(0, method.wait_time, 0.03)
144
+ assert_in_delta(0, method.self_time, 0.03)
145
+
146
+ method = methods[2]
147
+ assert_equal('Kernel#sleep', method.full_name)
148
+ assert_in_delta(0.2, method.total_time, 0.03)
149
+ assert_in_delta(0, method.wait_time, 0.03)
150
+ assert_in_delta(0.2, method.self_time, 0.03)
151
+
152
+ method = methods[3]
153
+ assert_equal('Class#new', method.full_name)
154
+ assert_in_delta(0, method.total_time, 0.03)
155
+ assert_in_delta(0, method.wait_time, 0.03)
156
+ assert_in_delta(0, method.self_time, 0.03)
157
+
158
+ method = methods[4]
159
+ assert_equal('BasicObject#initialize', method.full_name)
160
+ assert_in_delta(0, method.total_time, 0.03)
161
+ assert_in_delta(0, method.wait_time, 0.03)
162
+ assert_in_delta(0, method.self_time, 0.03)
163
+ end
164
+
165
+ def test_instance_methods_block
166
+ result = RubyProf.profile do
167
+ 1.times { RubyProf::C1.new.sleep_wait }
119
168
  end
169
+
170
+ methods = result.threads.first.methods.sort.reverse
171
+ assert_equal(6, methods.length)
172
+
173
+ # Check times
174
+ method = methods[0]
175
+ assert_equal("MeasureWallTimeTest#test_instance_methods_block", method.full_name)
176
+ assert_in_delta(0.2, method.total_time, 0.03)
177
+ assert_in_delta(0.0, method.wait_time, 0.03)
178
+ assert_in_delta(0.0, method.self_time, 0.03)
179
+ assert_in_delta(0.2, method.children_time, 0.03)
180
+
181
+ method = methods[1]
182
+ assert_equal("Integer#times", method.full_name)
183
+ assert_in_delta(0.2, method.total_time, 0.03)
184
+ assert_in_delta(0.0, method.wait_time, 0.03)
185
+ assert_in_delta(0.0, method.self_time, 0.03)
186
+ assert_in_delta(0.2, method.children_time, 0.03)
187
+
188
+ method = methods[2]
189
+ assert_equal("RubyProf::C1#sleep_wait", method.full_name)
190
+ assert_in_delta(0.2, method.total_time, 0.03)
191
+ assert_in_delta(0.0, method.wait_time, 0.03)
192
+ assert_in_delta(0.0, method.self_time, 0.03)
193
+ assert_in_delta(0.2, method.children_time, 0.03)
194
+
195
+ method = methods[3]
196
+ assert_equal("Kernel#sleep", method.full_name)
197
+ assert_in_delta(0.2, method.total_time, 0.03)
198
+ assert_in_delta(0.0, method.wait_time, 0.03)
199
+ assert_in_delta(0.2, method.self_time, 0.03)
200
+ assert_in_delta(0.0, method.children_time, 0.03)
201
+
202
+ method = methods[4]
203
+ assert_equal("Class#new", method.full_name)
204
+ assert_in_delta(0.0, method.total_time, 0.03)
205
+ assert_in_delta(0.0, method.wait_time, 0.03)
206
+ assert_in_delta(0.0, method.self_time, 0.03)
207
+ assert_in_delta(0.0, method.children_time, 0.03)
208
+
209
+ method = methods[5]
210
+ assert_equal("BasicObject#initialize", method.full_name)
211
+ assert_in_delta(0.0, method.total_time, 0.03)
212
+ assert_in_delta(0.0, method.wait_time, 0.03)
213
+ assert_in_delta(0.0, method.self_time, 0.03)
214
+ assert_in_delta(0.0, method.children_time, 0.03)
120
215
  end
121
216
 
122
- def test_module_methods
217
+ def test_instance_methods_threaded
123
218
  result = RubyProf.profile do
124
- RubyProf::C2.hello
219
+ background_thread = Thread.new do
220
+ RubyProf::C1.new.sleep_wait
221
+ end
222
+ background_thread.join
125
223
  end
126
224
 
225
+ assert_equal(2, result.threads.count)
226
+
127
227
  thread = result.threads.first
128
- assert_in_delta(0.3, thread.total_time, 0.01)
228
+ assert_in_delta(0.2, thread.total_time, 0.03)
129
229
 
130
- top_methods = thread.top_methods
131
- assert_equal(1, top_methods.count)
132
- assert_equal("MeasureWallTimeTest#test_module_methods", top_methods[0].full_name)
230
+ methods = result.threads.first.methods.sort.reverse
231
+ assert_equal(4, methods.length)
133
232
 
134
- # Methods:
135
- # MeasureWallTimeTest#test_module_methods
136
- # M1#hello
137
- # Kernel#sleep
233
+ # Check times
234
+ assert_equal('MeasureWallTimeTest#test_instance_methods_threaded', methods[0].full_name)
235
+ assert_in_delta(0.2, methods[0].total_time, 0.03)
236
+ assert_in_delta(0.0, methods[0].wait_time, 0.03)
237
+ assert_in_delta(0.0, methods[0].self_time, 0.03)
238
+ assert_in_delta(0.2, methods[0].children_time, 0.03)
239
+
240
+ assert_equal('Thread#join', methods[1].full_name)
241
+ assert_in_delta(0.2, methods[1].total_time, 0.03)
242
+ assert_in_delta(0.2, methods[1].wait_time, 0.03)
243
+ assert_in_delta(0.0, methods[1].self_time, 0.03)
244
+ assert_in_delta(0.0, methods[1].children_time, 0.03)
245
+
246
+ assert_equal('<Class::Thread>#new', methods[2].full_name)
247
+ assert_in_delta(0.0, methods[2].total_time, 0.03)
248
+ assert_in_delta(0.0, methods[2].wait_time, 0.03)
249
+ assert_in_delta(0.0, methods[2].self_time, 0.03)
250
+ assert_in_delta(0.0, methods[2].children_time, 0.03)
251
+
252
+ assert_equal('Thread#initialize', methods[3].full_name)
253
+ assert_in_delta(0.0, methods[3].total_time, 0.03)
254
+ assert_in_delta(0.0, methods[3].wait_time, 0.03)
255
+ assert_in_delta(0.0, methods[3].self_time, 0.03)
256
+ assert_in_delta(0.0, methods[3].children_time, 0.03)
257
+
258
+ thread = result.threads.last
259
+ assert_in_delta(0.2, thread.total_time, 0.03)
260
+
261
+ methods = result.threads.first.methods.sort.reverse
262
+ assert_equal(4, methods.length)
263
+
264
+ methods = result.threads.last.methods.sort.reverse
265
+ assert_equal(5, methods.length)
266
+
267
+ # Check times
268
+ assert_equal('MeasureWallTimeTest#test_instance_methods_threaded', methods[0].full_name)
269
+ assert_in_delta(0.2, methods[0].total_time, 0.03)
270
+ assert_in_delta(0.0, methods[0].wait_time, 0.03)
271
+ assert_in_delta(0.0, methods[0].self_time, 0.03)
272
+ assert_in_delta(0.2, methods[0].children_time, 0.03)
273
+
274
+ assert_equal('RubyProf::C1#sleep_wait', methods[1].full_name)
275
+ assert_in_delta(0.2, methods[1].total_time, 0.03)
276
+ assert_in_delta(0.0, methods[1].wait_time, 0.03)
277
+ assert_in_delta(0.0, methods[1].self_time, 0.03)
278
+ assert_in_delta(0.2, methods[1].children_time, 0.03)
279
+
280
+ assert_equal('Kernel#sleep', methods[2].full_name)
281
+ assert_in_delta(0.2, methods[2].total_time, 0.03)
282
+ assert_in_delta(0.0, methods[2].wait_time, 0.03)
283
+ assert_in_delta(0.2, methods[2].self_time, 0.03)
284
+ assert_in_delta(0.0, methods[2].children_time, 0.03)
285
+
286
+ assert_equal('Class#new', methods[3].full_name)
287
+ assert_in_delta(0.0, methods[3].total_time, 0.03)
288
+ assert_in_delta(0.0, methods[3].wait_time, 0.03)
289
+ assert_in_delta(0.0, methods[3].self_time, 0.03)
290
+ assert_in_delta(0.0, methods[3].children_time, 0.03)
291
+
292
+ assert_equal('BasicObject#initialize', methods[4].full_name)
293
+ assert_in_delta(0.0, methods[4].total_time, 0.03)
294
+ assert_in_delta(0.0, methods[4].wait_time, 0.03)
295
+ assert_in_delta(0.0, methods[4].self_time, 0.03)
296
+ assert_in_delta(0.0, methods[4].children_time, 0.03)
297
+ end
298
+
299
+ def test_module_methods
300
+ result = RubyProf.profile do
301
+ RubyProf::C2.sleep_wait
302
+ end
303
+
304
+ thread = result.threads.first
305
+ assert_in_delta(0.3, thread.total_time, 0.03)
138
306
 
139
307
  methods = result.threads.first.methods.sort.reverse
140
308
  assert_equal(3, methods.length)
141
309
 
142
310
  assert_equal('MeasureWallTimeTest#test_module_methods', methods[0].full_name)
143
- assert_equal('RubyProf::M1#hello', methods[1].full_name)
311
+ assert_equal('RubyProf::M1#sleep_wait', methods[1].full_name)
144
312
  assert_equal('Kernel#sleep', methods[2].full_name)
145
313
 
146
314
  # Check times
147
315
  assert_in_delta(0.3, methods[0].total_time, 0.1)
148
- assert_in_delta(0, methods[0].wait_time, 0.02)
149
- assert_in_delta(0, methods[0].self_time, 0.02)
316
+ assert_in_delta(0, methods[0].wait_time, 0.03)
317
+ assert_in_delta(0, methods[0].self_time, 0.03)
150
318
 
151
319
  assert_in_delta(0.3, methods[1].total_time, 0.1)
152
- assert_in_delta(0, methods[1].wait_time, 0.02)
153
- assert_in_delta(0, methods[1].self_time, 0.02)
320
+ assert_in_delta(0, methods[1].wait_time, 0.03)
321
+ assert_in_delta(0, methods[1].self_time, 0.03)
154
322
 
155
323
  assert_in_delta(0.3, methods[2].total_time, 0.1)
156
- assert_in_delta(0, methods[2].wait_time, 0.02)
324
+ assert_in_delta(0, methods[2].wait_time, 0.03)
157
325
  assert_in_delta(0.3, methods[2].self_time, 0.1)
158
326
  end
159
327
 
160
328
  def test_module_instance_methods
161
329
  result = RubyProf.profile do
162
- RubyProf::C2.new.hello
330
+ RubyProf::C2.new.sleep_wait
163
331
  end
164
332
 
165
333
  thread = result.threads.first
166
- assert_in_delta(0.3, thread.total_time, 0.01)
167
-
168
- top_methods = thread.top_methods
169
- assert_equal(1, top_methods.count)
170
- assert_equal("MeasureWallTimeTest#test_module_instance_methods", top_methods[0].full_name)
171
-
172
- # Methods:
173
- # MeasureWallTimeTest#test_module_instance_methods
174
- # Class#new
175
- # <Class::Object>#allocate
176
- # Object#initialize
177
- # M1#hello
178
- # Kernel#sleep
334
+ assert_in_delta(0.3, thread.total_time, 0.03)
179
335
 
180
336
  methods = result.threads.first.methods.sort.reverse
181
- assert_equal(RubyProf.ruby_2? ? 5 : 6, methods.length)
337
+ assert_equal(5, methods.length)
182
338
  names = methods.map(&:full_name)
183
339
  assert_equal('MeasureWallTimeTest#test_module_instance_methods', names[0])
184
- assert_equal('RubyProf::M1#hello', names[1])
340
+ assert_equal('RubyProf::M1#sleep_wait', names[1])
185
341
  assert_equal('Kernel#sleep', names[2])
186
342
  assert_equal('Class#new', names[3])
187
343
 
188
344
  # order can differ
189
- assert(names.include?("#{RubyProf.parent_object}#initialize"))
190
- unless RubyProf.ruby_2?
191
- assert(names.include?("<Class::#{RubyProf.parent_object}>#allocate"))
192
- end
345
+ assert(names.include?("BasicObject#initialize"))
193
346
 
194
347
  # Check times
195
348
  assert_in_delta(0.3, methods[0].total_time, 0.1)
196
349
  assert_in_delta(0, methods[0].wait_time, 0.1)
197
350
  assert_in_delta(0, methods[0].self_time, 0.1)
198
351
 
199
- assert_in_delta(0.3, methods[1].total_time, 0.02)
200
- assert_in_delta(0, methods[1].wait_time, 0.01)
201
- assert_in_delta(0, methods[1].self_time, 0.01)
352
+ assert_in_delta(0.3, methods[1].total_time, 0.03)
353
+ assert_in_delta(0, methods[1].wait_time, 0.03)
354
+ assert_in_delta(0, methods[1].self_time, 0.03)
202
355
 
203
- assert_in_delta(0.3, methods[2].total_time, 0.02)
204
- assert_in_delta(0, methods[2].wait_time, 0.01)
205
- assert_in_delta(0.3, methods[2].self_time, 0.02)
356
+ assert_in_delta(0.3, methods[2].total_time, 0.03)
357
+ assert_in_delta(0, methods[2].wait_time, 0.03)
358
+ assert_in_delta(0.3, methods[2].self_time, 0.03)
206
359
 
207
- assert_in_delta(0, methods[3].total_time, 0.01)
208
- assert_in_delta(0, methods[3].wait_time, 0.01)
209
- assert_in_delta(0, methods[3].self_time, 0.01)
360
+ assert_in_delta(0, methods[3].total_time, 0.03)
361
+ assert_in_delta(0, methods[3].wait_time, 0.03)
362
+ assert_in_delta(0, methods[3].self_time, 0.03)
210
363
 
211
- assert_in_delta(0, methods[4].total_time, 0.01)
212
- assert_in_delta(0, methods[4].wait_time, 0.01)
213
- assert_in_delta(0, methods[4].self_time, 0.01)
214
-
215
- unless RubyProf.ruby_2?
216
- assert_in_delta(0, methods[5].total_time, 0.01)
217
- assert_in_delta(0, methods[5].wait_time, 0.01)
218
- assert_in_delta(0, methods[5].self_time, 0.01)
219
- end
364
+ assert_in_delta(0, methods[4].total_time, 0.03)
365
+ assert_in_delta(0, methods[4].wait_time, 0.03)
366
+ assert_in_delta(0, methods[4].self_time, 0.03)
220
367
  end
221
368
 
222
- def test_singleton
223
- c3 = RubyProf::C3.new
224
-
225
- class << c3
226
- def hello
227
- end
228
- end
229
-
369
+ def test_singleton_methods
230
370
  result = RubyProf.profile do
231
- c3.hello
371
+ RubyProf::C3.instance.sleep_wait
232
372
  end
233
373
 
234
374
  thread = result.threads.first
235
- assert_in_delta(0.0, thread.total_time, 0.01)
236
-
237
- top_methods = thread.top_methods
238
- assert_equal(1, top_methods.count)
239
- assert_equal("MeasureWallTimeTest#test_singleton", top_methods[0].full_name)
375
+ assert_in_delta(0.3, thread.total_time, 0.03)
240
376
 
241
377
  methods = result.threads.first.methods.sort.reverse
242
- assert_equal(2, methods.length)
378
+ assert_equal(7, methods.length)
379
+
380
+ assert_equal('MeasureWallTimeTest#test_singleton_methods', methods[0].full_name)
381
+ assert_in_delta(0.3, methods[0].total_time, 0.03)
382
+ assert_in_delta(0.0, methods[0].wait_time, 0.03)
383
+ assert_in_delta(0.0, methods[0].self_time, 0.03)
384
+ assert_in_delta(0.3, methods[0].children_time, 0.03)
243
385
 
244
- assert_equal('MeasureWallTimeTest#test_singleton', methods[0].full_name)
245
- assert_equal('<Object::RubyProf::C3>#hello', methods[1].full_name)
386
+ assert_equal('RubyProf::C3#sleep_wait', methods[1].full_name)
387
+ assert_in_delta(0.3, methods[1].total_time, 0.03)
388
+ assert_in_delta(0.0, methods[1].wait_time, 0.03)
389
+ assert_in_delta(0.0, methods[1].self_time, 0.03)
390
+ assert_in_delta(0.3, methods[1].children_time, 0.03)
246
391
 
247
- assert_in_delta(0, methods[0].total_time, 0.01)
248
- assert_in_delta(0, methods[0].wait_time, 0.01)
249
- assert_in_delta(0, methods[0].self_time, 0.01)
392
+ assert_equal('Kernel#sleep', methods[2].full_name)
393
+ assert_in_delta(0.3, methods[2].total_time, 0.03)
394
+ assert_in_delta(0.0, methods[2].wait_time, 0.03)
395
+ assert_in_delta(0.3, methods[2].self_time, 0.03)
396
+ assert_in_delta(0.0, methods[2].children_time, 0.03)
397
+
398
+ if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.7')
399
+ method = methods.detect {|method| method.full_name == '<Class::RubyProf::C3>#instance'}
400
+ assert_equal('<Class::RubyProf::C3>#instance', method.full_name)
401
+ assert_in_delta(0.0, method.total_time, 0.03)
402
+ assert_in_delta(0.0, method.wait_time, 0.03)
403
+ assert_in_delta(0.0, method.self_time, 0.03)
404
+ assert_in_delta(0.0, method.children_time, 0.03)
405
+ else
406
+ method = methods.detect {|method| method.full_name == 'Singleton::SingletonClassMethods#instance'}
407
+ assert_equal('Singleton::SingletonClassMethods#instance', method.full_name)
408
+ assert_in_delta(0.0, method.total_time, 0.03)
409
+ assert_in_delta(0.0, method.wait_time, 0.03)
410
+ assert_in_delta(0.0, method.self_time, 0.03)
411
+ assert_in_delta(0.0, method.children_time, 0.03)
412
+ end
250
413
 
251
- assert_in_delta(0, methods[1].total_time, 0.01)
252
- assert_in_delta(0, methods[1].wait_time, 0.01)
253
- assert_in_delta(0, methods[1].self_time, 0.01)
414
+ method = methods.detect {|method| method.full_name == 'Thread::Mutex#synchronize'}
415
+ assert_equal('Thread::Mutex#synchronize', method.full_name)
416
+ assert_in_delta(0.0, method.total_time, 0.03)
417
+ assert_in_delta(0.0, method.wait_time, 0.03)
418
+ assert_in_delta(0.0, method.self_time, 0.03)
419
+ assert_in_delta(0.0, method.children_time, 0.03)
420
+
421
+ method = methods.detect {|method| method.full_name == 'Class#new'}
422
+ assert_equal('Class#new', method.full_name)
423
+ assert_in_delta(0.0, method.total_time, 0.03)
424
+ assert_in_delta(0.0, method.wait_time, 0.03)
425
+ assert_in_delta(0.0, method.self_time, 0.03)
426
+ assert_in_delta(0.0, method.children_time, 0.03)
427
+
428
+ method = methods.detect {|method| method.full_name == 'BasicObject#initialize'}
429
+ assert_equal('BasicObject#initialize', method.full_name)
430
+ assert_in_delta(0.0, method.total_time, 0.03)
431
+ assert_in_delta(0.0, method.wait_time, 0.03)
432
+ assert_in_delta(0.0, method.self_time, 0.03)
433
+ assert_in_delta(0.0, method.children_time, 0.03)
254
434
  end
255
- end
435
+ end