ruby-prof 0.13.1 → 1.4.2

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 (209) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGES +579 -371
  3. data/LICENSE +24 -23
  4. data/README.rdoc +5 -433
  5. data/Rakefile +98 -110
  6. data/bin/ruby-prof +328 -329
  7. data/bin/ruby-prof-check-trace +45 -0
  8. data/ext/ruby_prof/extconf.rb +16 -59
  9. data/ext/ruby_prof/rp_aggregate_call_tree.c +59 -0
  10. data/ext/ruby_prof/rp_aggregate_call_tree.h +13 -0
  11. data/ext/ruby_prof/rp_allocation.c +287 -0
  12. data/ext/ruby_prof/rp_allocation.h +31 -0
  13. data/ext/ruby_prof/rp_call_tree.c +369 -0
  14. data/ext/ruby_prof/rp_call_tree.h +43 -0
  15. data/ext/ruby_prof/rp_call_trees.c +288 -0
  16. data/ext/ruby_prof/rp_call_trees.h +28 -0
  17. data/ext/ruby_prof/rp_measure_allocations.c +50 -65
  18. data/ext/ruby_prof/rp_measure_memory.c +42 -73
  19. data/ext/ruby_prof/rp_measure_process_time.c +65 -71
  20. data/ext/ruby_prof/rp_measure_wall_time.c +64 -42
  21. data/ext/ruby_prof/rp_measurement.c +237 -0
  22. data/ext/ruby_prof/rp_measurement.h +50 -0
  23. data/ext/ruby_prof/rp_method.c +491 -420
  24. data/ext/ruby_prof/rp_method.h +62 -57
  25. data/ext/ruby_prof/rp_profile.c +908 -0
  26. data/ext/ruby_prof/rp_profile.h +35 -0
  27. data/ext/ruby_prof/rp_stack.c +212 -128
  28. data/ext/ruby_prof/rp_stack.h +53 -51
  29. data/ext/ruby_prof/rp_thread.c +362 -268
  30. data/ext/ruby_prof/rp_thread.h +39 -27
  31. data/ext/ruby_prof/ruby_prof.c +52 -695
  32. data/ext/ruby_prof/ruby_prof.h +26 -55
  33. data/ext/ruby_prof/vc/ruby_prof.sln +28 -21
  34. data/ext/ruby_prof/vc/{ruby_prof_20.vcxproj → ruby_prof.vcxproj} +56 -8
  35. data/lib/ruby-prof.rb +52 -67
  36. data/lib/ruby-prof/assets/call_stack_printer.html.erb +710 -0
  37. data/lib/ruby-prof/assets/call_stack_printer.png +0 -0
  38. data/lib/ruby-prof/assets/graph_printer.html.erb +355 -0
  39. data/lib/ruby-prof/call_tree.rb +57 -0
  40. data/lib/ruby-prof/call_tree_visitor.rb +36 -0
  41. data/lib/ruby-prof/compatibility.rb +99 -169
  42. data/lib/ruby-prof/exclude_common_methods.rb +198 -0
  43. data/lib/ruby-prof/measurement.rb +17 -0
  44. data/lib/ruby-prof/method_info.rb +78 -131
  45. data/lib/ruby-prof/printers/abstract_printer.rb +137 -85
  46. data/lib/ruby-prof/printers/call_info_printer.rb +53 -41
  47. data/lib/ruby-prof/printers/call_stack_printer.rb +180 -773
  48. data/lib/ruby-prof/printers/call_tree_printer.rb +151 -92
  49. data/lib/ruby-prof/printers/dot_printer.rb +132 -132
  50. data/lib/ruby-prof/printers/flat_printer.rb +53 -69
  51. data/lib/ruby-prof/printers/graph_html_printer.rb +63 -255
  52. data/lib/ruby-prof/printers/graph_printer.rb +113 -116
  53. data/lib/ruby-prof/printers/multi_printer.rb +127 -56
  54. data/lib/ruby-prof/profile.rb +37 -77
  55. data/lib/ruby-prof/rack.rb +62 -15
  56. data/lib/ruby-prof/task.rb +147 -147
  57. data/lib/ruby-prof/thread.rb +10 -12
  58. data/lib/ruby-prof/version.rb +3 -0
  59. data/lib/unprof.rb +10 -10
  60. data/ruby-prof.gemspec +65 -61
  61. data/test/abstract_printer_test.rb +26 -0
  62. data/test/alias_test.rb +126 -0
  63. data/test/basic_test.rb +43 -128
  64. data/test/call_tree_visitor_test.rb +32 -0
  65. data/test/call_trees_test.rb +66 -0
  66. data/test/duplicate_names_test.rb +32 -32
  67. data/test/dynamic_method_test.rb +53 -74
  68. data/test/enumerable_test.rb +21 -16
  69. data/test/exceptions_test.rb +24 -16
  70. data/test/exclude_methods_test.rb +151 -0
  71. data/test/exclude_threads_test.rb +53 -54
  72. data/test/fiber_test.rb +129 -65
  73. data/test/gc_test.rb +90 -0
  74. data/test/inverse_call_tree_test.rb +175 -0
  75. data/test/line_number_test.rb +158 -71
  76. data/test/marshal_test.rb +113 -0
  77. data/test/measure_allocations.rb +30 -0
  78. data/test/measure_allocations_test.rb +375 -25
  79. data/test/measure_allocations_trace_test.rb +375 -0
  80. data/test/measure_memory_trace_test.rb +1101 -0
  81. data/test/measure_process_time_test.rb +785 -62
  82. data/test/measure_times.rb +56 -0
  83. data/test/measure_wall_time_test.rb +434 -254
  84. data/test/multi_printer_test.rb +71 -82
  85. data/test/no_method_class_test.rb +15 -15
  86. data/test/pause_resume_test.rb +175 -166
  87. data/test/prime.rb +54 -54
  88. data/test/prime_script.rb +6 -0
  89. data/test/printer_call_stack_test.rb +27 -0
  90. data/test/printer_call_tree_test.rb +30 -0
  91. data/test/printer_flat_test.rb +99 -0
  92. data/test/printer_graph_html_test.rb +59 -0
  93. data/test/printer_graph_test.rb +40 -0
  94. data/test/printers_test.rb +141 -257
  95. data/test/printing_recursive_graph_test.rb +81 -0
  96. data/test/profile_test.rb +16 -0
  97. data/test/rack_test.rb +93 -0
  98. data/test/recursive_test.rb +206 -215
  99. data/test/singleton_test.rb +38 -38
  100. data/test/stack_printer_test.rb +64 -78
  101. data/test/start_stop_test.rb +109 -112
  102. data/test/test_helper.rb +13 -115
  103. data/test/thread_test.rb +144 -178
  104. data/test/unique_call_path_test.rb +120 -224
  105. data/test/yarv_test.rb +56 -0
  106. metadata +77 -133
  107. data/doc/LICENSE.html +0 -155
  108. data/doc/README_rdoc.html +0 -648
  109. data/doc/Rack.html +0 -167
  110. data/doc/Rack/RubyProf.html +0 -319
  111. data/doc/RubyProf.html +0 -1000
  112. data/doc/RubyProf/AbstractPrinter.html +0 -580
  113. data/doc/RubyProf/AggregateCallInfo.html +0 -570
  114. data/doc/RubyProf/CallInfo.html +0 -512
  115. data/doc/RubyProf/CallInfoPrinter.html +0 -190
  116. data/doc/RubyProf/CallInfoVisitor.html +0 -332
  117. data/doc/RubyProf/CallStackPrinter.html +0 -1600
  118. data/doc/RubyProf/CallTreePrinter.html +0 -413
  119. data/doc/RubyProf/Cmd.html +0 -669
  120. data/doc/RubyProf/DotPrinter.html +0 -312
  121. data/doc/RubyProf/FlatPrinter.html +0 -229
  122. data/doc/RubyProf/FlatPrinterWithLineNumbers.html +0 -267
  123. data/doc/RubyProf/GraphHtmlPrinter.html +0 -630
  124. data/doc/RubyProf/GraphPrinter.html +0 -209
  125. data/doc/RubyProf/MethodInfo.html +0 -713
  126. data/doc/RubyProf/MultiPrinter.html +0 -407
  127. data/doc/RubyProf/Profile.html +0 -821
  128. data/doc/RubyProf/ProfileTask.html +0 -532
  129. data/doc/RubyProf/Test.html +0 -578
  130. data/doc/RubyProf/Thread.html +0 -262
  131. data/doc/created.rid +0 -32
  132. data/doc/examples/flat_txt.html +0 -191
  133. data/doc/examples/graph_txt.html +0 -305
  134. data/doc/images/add.png +0 -0
  135. data/doc/images/brick.png +0 -0
  136. data/doc/images/brick_link.png +0 -0
  137. data/doc/images/bug.png +0 -0
  138. data/doc/images/bullet_black.png +0 -0
  139. data/doc/images/bullet_toggle_minus.png +0 -0
  140. data/doc/images/bullet_toggle_plus.png +0 -0
  141. data/doc/images/date.png +0 -0
  142. data/doc/images/delete.png +0 -0
  143. data/doc/images/find.png +0 -0
  144. data/doc/images/loadingAnimation.gif +0 -0
  145. data/doc/images/macFFBgHack.png +0 -0
  146. data/doc/images/package.png +0 -0
  147. data/doc/images/page_green.png +0 -0
  148. data/doc/images/page_white_text.png +0 -0
  149. data/doc/images/page_white_width.png +0 -0
  150. data/doc/images/plugin.png +0 -0
  151. data/doc/images/ruby.png +0 -0
  152. data/doc/images/tag_blue.png +0 -0
  153. data/doc/images/tag_green.png +0 -0
  154. data/doc/images/transparent.png +0 -0
  155. data/doc/images/wrench.png +0 -0
  156. data/doc/images/wrench_orange.png +0 -0
  157. data/doc/images/zoom.png +0 -0
  158. data/doc/index.html +0 -647
  159. data/doc/js/darkfish.js +0 -155
  160. data/doc/js/jquery.js +0 -18
  161. data/doc/js/navigation.js +0 -142
  162. data/doc/js/search.js +0 -94
  163. data/doc/js/search_index.js +0 -1
  164. data/doc/js/searcher.js +0 -228
  165. data/doc/rdoc.css +0 -543
  166. data/doc/table_of_contents.html +0 -462
  167. data/examples/empty.png +0 -0
  168. data/examples/flat.txt +0 -55
  169. data/examples/graph.dot +0 -106
  170. data/examples/graph.html +0 -823
  171. data/examples/graph.png +0 -0
  172. data/examples/graph.txt +0 -170
  173. data/examples/minus.png +0 -0
  174. data/examples/multi.flat.txt +0 -23
  175. data/examples/multi.graph.html +0 -906
  176. data/examples/multi.grind.dat +0 -194
  177. data/examples/multi.stack.html +0 -573
  178. data/examples/plus.png +0 -0
  179. data/examples/stack.html +0 -573
  180. data/ext/ruby_prof/rp_call_info.c +0 -407
  181. data/ext/ruby_prof/rp_call_info.h +0 -48
  182. data/ext/ruby_prof/rp_measure.c +0 -48
  183. data/ext/ruby_prof/rp_measure.h +0 -45
  184. data/ext/ruby_prof/rp_measure_cpu_time.c +0 -112
  185. data/ext/ruby_prof/rp_measure_gc_runs.c +0 -65
  186. data/ext/ruby_prof/rp_measure_gc_time.c +0 -57
  187. data/ext/ruby_prof/vc/ruby_prof_18.vcxproj +0 -108
  188. data/ext/ruby_prof/vc/ruby_prof_19.vcxproj +0 -110
  189. data/ext/ruby_prof/version.h +0 -7
  190. data/lib/ruby-prof/aggregate_call_info.rb +0 -72
  191. data/lib/ruby-prof/call_info.rb +0 -89
  192. data/lib/ruby-prof/call_info_visitor.rb +0 -44
  193. data/lib/ruby-prof/images/empty.png +0 -0
  194. data/lib/ruby-prof/images/minus.png +0 -0
  195. data/lib/ruby-prof/images/plus.png +0 -0
  196. data/lib/ruby-prof/printers/flat_printer_with_line_numbers.rb +0 -57
  197. data/lib/ruby-prof/test.rb +0 -150
  198. data/test/aggregate_test.rb +0 -136
  199. data/test/call_info_test.rb +0 -78
  200. data/test/call_info_visitor_test.rb +0 -31
  201. data/test/exec_test.rb +0 -14
  202. data/test/measure_cpu_time_test.rb +0 -220
  203. data/test/measure_gc_runs_test.rb +0 -32
  204. data/test/measure_gc_time_test.rb +0 -36
  205. data/test/measure_memory_test.rb +0 -31
  206. data/test/method_elimination_test.rb +0 -84
  207. data/test/module_test.rb +0 -45
  208. data/test/stack_test.rb +0 -138
  209. data/test/test_suite.rb +0 -37
@@ -1,773 +1,180 @@
1
- # encoding: utf-8
2
-
3
- require 'erb'
4
- require 'fileutils'
5
-
6
- module RubyProf
7
- # prints a HTML visualization of the call tree
8
- class CallStackPrinter < AbstractPrinter
9
- include ERB::Util
10
-
11
- # Specify print options.
12
- #
13
- # options - Hash table
14
- # :min_percent - Number 0 to 100 that specifes the minimum
15
- # %self (the methods self time divided by the
16
- # overall total time) that a method must take
17
- # for it to be printed out in the report.
18
- # Default value is 0.
19
- #
20
- # :print_file - True or false. Specifies if a method's source
21
- # file should be printed. Default value if false.
22
- #
23
- # :threshold - a float from 0 to 100 that sets the threshold of
24
- # results displayed.
25
- # Default value is 1.0
26
- #
27
- # :title - a String to overide the default "ruby-prof call tree"
28
- # title of the report.
29
- #
30
- # :expansion - a float from 0 to 100 that sets the threshold of
31
- # results that are expanded, if the percent_total
32
- # exceeds it.
33
- # Default value is 10.0
34
- #
35
- # :application - a String to overide the name of the application,
36
- # as it appears on the report.
37
- #
38
- def print(output = STDOUT, options = {})
39
- @output = output
40
- setup_options(options)
41
- if @graph_html = options.delete(:graph)
42
- @graph_html = "file://" + @graph_html if @graph_html[0]=="/"
43
- end
44
-
45
- print_header
46
-
47
- @overall_threads_time = @result.threads.inject(0) do |val, thread|
48
- val += thread.total_time
49
- end
50
-
51
- @result.threads.each do |thread|
52
- @current_thread_id = thread.fiber_id
53
- @overall_time = thread.total_time
54
- thread_info = "Thread: #{thread.id}"
55
- thread_info << ", Fiber: #{thread.fiber_id}" unless thread.id == thread.fiber_id
56
- thread_info << " (#{"%4.2f%%" % ((@overall_time/@overall_threads_time)*100)} ~ #{@overall_time})"
57
- @output.print "<div class=\"thread\">#{thread_info}</div>"
58
- @output.print "<ul name=\"thread\">"
59
- thread.methods.each do |m|
60
- # $stderr.print m.dump
61
- next unless m.root?
62
- m.call_infos.each do |ci|
63
- next unless ci.root?
64
- print_stack ci, thread.total_time
65
- end
66
- end
67
- @output.print "</ul>"
68
- end
69
-
70
- print_footer
71
-
72
- copy_image_files
73
- end
74
-
75
- def print_stack(call_info, parent_time)
76
- total_time = call_info.total_time
77
- percent_parent = (total_time/parent_time)*100
78
- percent_total = (total_time/@overall_time)*100
79
- return unless percent_total > min_percent
80
- color = self.color(percent_total)
81
- kids = call_info.children
82
- visible = percent_total >= threshold
83
- expanded = percent_total >= expansion
84
- display = visible ? "block" : "none"
85
- @output.print "<li class=\"color#{color}\" style=\"display:#{display}\">"
86
- if kids.empty?
87
- @output.print "<img src=\"empty.png\">"
88
- else
89
- visible_children = kids.any?{|ci| (ci.total_time/@overall_time)*100 >= threshold}
90
- image = visible_children ? (expanded ? "minus" : "plus") : "empty"
91
- @output.print "<img class=\"toggle\" src=\"#{image}.png\">"
92
- end
93
- @output.printf " %4.2f%% (%4.2f%%) %s %s\n", percent_total, percent_parent, link(call_info), graph_link(call_info)
94
- unless kids.empty?
95
- if expanded
96
- @output.print "<ul>"
97
- else
98
- @output.print '<ul style="display:none">'
99
- end
100
- kids.sort_by{|c| -c.total_time}.each do |callinfo|
101
- print_stack callinfo, total_time
102
- end
103
- @output.print "</ul>"
104
- end
105
- @output.print "</li>"
106
- end
107
-
108
- def name(call_info)
109
- method = call_info.target
110
- method.full_name
111
- end
112
-
113
- def link(call_info)
114
- method = call_info.target
115
- file = File.expand_path(method.source_file)
116
- if file =~ /\/ruby_runtime$/
117
- h(name(call_info))
118
- else
119
- if RUBY_PLATFORM =~ /darwin/
120
- "<a href=\"txmt://open?url=file://#{file}&line=#{method.line}\">#{h(name(call_info))}</a>"
121
- else
122
- "<a href=\"file://#{file}##{method.line}\">#{h(name(call_info))}</a>"
123
- end
124
- end
125
- end
126
-
127
- def graph_link(call_info)
128
- total_calls = call_info.target.call_infos.inject(0){|t, ci| t += ci.called}
129
- href = "#{@graph_html}##{method_href(call_info.target)}"
130
- totals = @graph_html ? "<a href='#{href}'>#{total_calls}</a>" : total_calls.to_s
131
- "[#{call_info.called} calls, #{totals} total]"
132
- end
133
-
134
- def method_href(method)
135
- h(method.full_name.gsub(/[><#\.\?=:]/,"_") + "_" + @current_thread_id.to_s)
136
- end
137
-
138
- def total_time(call_infos)
139
- sum(call_infos.map{|ci| ci.total_time})
140
- end
141
-
142
- def sum(a)
143
- a.inject(0.0){|s,t| s+=t}
144
- end
145
-
146
- def dump(ci)
147
- $stderr.printf "%s/%d t:%f s:%f w:%f \n", ci, ci.object_id, ci.total_time, ci.self_time, ci.wait_time
148
- end
149
-
150
- def color(p)
151
- case i = p.to_i
152
- when 0..5
153
- "01"
154
- when 5..10
155
- "05"
156
- when 100
157
- "9"
158
- else
159
- "#{i/10}"
160
- end
161
- end
162
-
163
- def application
164
- @options[:application] || $PROGRAM_NAME
165
- end
166
-
167
- def arguments
168
- ARGV.join(' ')
169
- end
170
-
171
- def title
172
- @title ||= @options.delete(:title) || "ruby-prof call tree"
173
- end
174
-
175
- def threshold
176
- @options[:threshold] || 1.0
177
- end
178
-
179
- def expansion
180
- @options[:expansion] || 10.0
181
- end
182
-
183
- def copy_image_files
184
- if @output.is_a?(File)
185
- target_dir = File.dirname(@output.path)
186
- image_dir = File.join(File.dirname(__FILE__), '..', 'images')
187
- %w(empty plus minus).each do |img|
188
- source_file = "#{image_dir}/#{img}.png"
189
- target_file = "#{target_dir}/#{img}.png"
190
- FileUtils.cp(source_file, target_file) unless File.exist?(target_file)
191
- end
192
- end
193
- end
194
-
195
- def print_header
196
- @output.puts "<html><head>"
197
- @output.puts '<meta http-equiv="content-type" content="text/html; charset=utf-8">'
198
- @output.puts "<title>#{h title}</title>"
199
- print_css
200
- print_java_script
201
- @output.puts '</head><body><div style="display: inline-block;">'
202
- print_title_bar
203
- print_commands
204
- print_help
205
- end
206
-
207
- def print_footer
208
- @output.puts '<div id="sentinel"></div></div></body></html>'
209
- end
210
-
211
- def print_css
212
- @output.puts <<-'end_css'
213
- <style type="text/css">
214
- <!--
215
- body {
216
- font-size:70%;
217
- padding:0px;
218
- margin:5px;
219
- margin-right:0px;
220
- margin-left:0px;
221
- background: #ffffff;
222
- }
223
- ul {
224
- margin-left:0px;
225
- margin-top:0px;
226
- margin-bottom:0px;
227
- padding-left:0px;
228
- list-style-type:none;
229
- }
230
- li {
231
- margin-left:11px;
232
- padding:0px;
233
- white-space:nowrap;
234
- border-top:1px solid #cccccc;
235
- border-left:1px solid #cccccc;
236
- border-bottom:none;
237
- }
238
- .thread {
239
- margin-left:11px;
240
- background:#708090;
241
- padding-top:3px;
242
- padding-left:12px;
243
- padding-bottom:2px;
244
- border-left:1px solid #CCCCCC;
245
- border-top:1px solid #CCCCCC;
246
- font-weight:bold;
247
- }
248
- .hidden {
249
- display:none;
250
- width:0px;
251
- height:0px;
252
- margin:0px;
253
- padding:0px;
254
- border-style:none;
255
- }
256
- .color01 { background:#adbdeb }
257
- .color05 { background:#9daddb }
258
- .color0 { background:#8d9dcb }
259
- .color1 { background:#89bccb }
260
- .color2 { background:#56e3e7 }
261
- .color3 { background:#32cd70 }
262
- .color4 { background:#a3d53c }
263
- .color5 { background:#c4cb34 }
264
- .color6 { background:#dcb66d }
265
- .color7 { background:#cda59e }
266
- .color8 { background:#be9d9c }
267
- .color9 { background:#cf947a }
268
- #commands {
269
- font-size:10pt;
270
- padding:10px;
271
- margin-left:11px;
272
- margin-bottom:0px;
273
- margin-top:0px;
274
- background:#708090;
275
- border-top:1px solid #cccccc;
276
- border-left:1px solid #cccccc;
277
- border-bottom:none;
278
- }
279
- #titlebar {
280
- font-size:10pt;
281
- padding:10px;
282
- margin-left:11px;
283
- margin-bottom:0px;
284
- margin-top:10px;
285
- background:#8090a0;
286
- border-top:1px solid #cccccc;
287
- border-left:1px solid #cccccc;
288
- border-bottom:none;
289
- }
290
- #help {
291
- font-size:10pt;
292
- padding:10px;
293
- margin-left:11px;
294
- margin-bottom:0px;
295
- margin-top:0px;
296
- background:#8090a0;
297
- display:none;
298
- border-top:1px solid #cccccc;
299
- border-left:1px solid #cccccc;
300
- border-bottom:none;
301
- }
302
- #sentinel {
303
- height: 400px;
304
- margin-left:11px;
305
- background:#8090a0;
306
- border-top:1px solid #cccccc;
307
- border-left:1px solid #cccccc;
308
- border-bottom:none;
309
- }
310
- input { margin-left:10px; }
311
- -->
312
- </style>
313
- end_css
314
- end
315
-
316
- def print_java_script
317
- @output.puts <<-'end_java_script'
318
- <script type="text/javascript">
319
- /*
320
- Copyright (C) 2005,2009 Stefan Kaes
321
- skaes@railsexpress.de
322
- */
323
-
324
- function rootNode() {
325
- return currentThread;
326
- }
327
-
328
- function hideUL(node) {
329
- var lis = node.childNodes
330
- var l = lis.length;
331
- for (var i=0; i < l ; i++ ) {
332
- hideLI(lis[i]);
333
- }
334
- }
335
-
336
- function showUL(node) {
337
- var lis = node.childNodes;
338
- var l = lis.length;
339
- for (var i=0; i < l ; i++ ) {
340
- showLI(lis[i]);
341
- }
342
- }
343
-
344
- function findUlChild(li){
345
- var ul = li.childNodes[2];
346
- while (ul && ul.nodeName != "UL") {
347
- ul = ul.nextSibling;
348
- }
349
- return ul;
350
- }
351
-
352
- function isLeafNode(li) {
353
- var img = li.firstChild;
354
- return (img.src.indexOf('empty.png') > -1);
355
- }
356
-
357
- function hideLI(li) {
358
- if (isLeafNode(li))
359
- return;
360
-
361
- var img = li.firstChild;
362
- img.src = 'plus.png';
363
-
364
- var ul = findUlChild(li);
365
- if (ul) {
366
- ul.style.display = 'none';
367
- hideUL(ul);
368
- }
369
- }
370
-
371
- function showLI(li) {
372
- if (isLeafNode(li))
373
- return;
374
-
375
- var img = li.firstChild;
376
- img.src = 'minus.png';
377
-
378
- var ul = findUlChild(li);
379
- if (ul) {
380
- ul.style.display = 'block';
381
- showUL(ul);
382
- }
383
- }
384
-
385
- function toggleLI(li) {
386
- var img = li.firstChild;
387
- if (img.src.indexOf("minus.png")>-1)
388
- hideLI(li);
389
- else {
390
- if (img.src.indexOf("plus.png")>-1)
391
- showLI(li);
392
- }
393
- }
394
-
395
- function aboveThreshold(text, threshold) {
396
- var match = text.match(/\d+[.,]\d+/);
397
- return (match && parseFloat(match[0].replace(/,/, '.'))>=threshold);
398
- }
399
-
400
- function setThresholdLI(li, threshold) {
401
- var img = li.firstChild;
402
- var text = img.nextSibling;
403
- var ul = findUlChild(li);
404
-
405
- var visible = aboveThreshold(text.nodeValue, threshold) ? 1 : 0;
406
-
407
- var count = 0;
408
- if (ul) {
409
- count = setThresholdUL(ul, threshold);
410
- }
411
- if (count>0) {
412
- img.src = 'minus.png';
413
- }
414
- else {
415
- img.src = 'empty.png';
416
- }
417
- if (visible) {
418
- li.style.display = 'block'
419
- }
420
- else {
421
- li.style.display = 'none'
422
- }
423
- return visible;
424
- }
425
-
426
- function setThresholdUL(node, threshold) {
427
- var lis = node.childNodes;
428
- var l = lis.length;
429
-
430
- var count = 0;
431
- for ( var i = 0; i < l ; i++ ) {
432
- count = count + setThresholdLI(lis[i], threshold);
433
- }
434
-
435
- var visible = (count > 0) ? 1 : 0;
436
- if (visible) {
437
- node.style.display = 'block';
438
- }
439
- else {
440
- node.style.display = 'none';
441
- }
442
- return visible;
443
- }
444
-
445
- function toggleChildren(img, event) {
446
- event.cancelBubble=true;
447
-
448
- if (img.src.indexOf('empty.png') > -1)
449
- return;
450
-
451
- var minus = (img.src.indexOf('minus.png') > -1);
452
-
453
- if (minus) {
454
- img.src = 'plus.png';
455
- }
456
- else
457
- img.src = 'minus.png';
458
-
459
- var li = img.parentNode;
460
- var ul = findUlChild(li);
461
- if (ul) {
462
- if (minus)
463
- ul.style.display = 'none';
464
- else
465
- ul.style.display = 'block';
466
- }
467
- if (minus)
468
- moveSelectionIfNecessary(li);
469
- }
470
-
471
- function showChildren(li) {
472
- var img = li.firstChild;
473
- if (img.src.indexOf('empty.png') > -1)
474
- return;
475
- img.src = 'minus.png';
476
-
477
- var ul = findUlChild(li);
478
- if (ul) {
479
- ul.style.display = 'block';
480
- }
481
- }
482
-
483
- function setThreshold() {
484
- var tv = document.getElementById("threshold").value;
485
- if (tv.match(/[0-9]+([.,][0-9]+)?/)) {
486
- var f = parseFloat(tv.replace(/,/, '.'));
487
- var threads = document.getElementsByName("thread");
488
- var l = threads.length;
489
- for ( var i = 0; i < l ; i++ ) {
490
- setThresholdUL(threads[i], f);
491
- }
492
- var p = selectedNode;
493
- while (p && p.style.display=='none')
494
- p=p.parentNode.parentNode;
495
- if (p && p.nodeName=="LI")
496
- selectNode(p);
497
- }
498
- else {
499
- alert("Please specify a decimal number as threshold value!");
500
- }
501
- }
502
-
503
- function collapseAll(event) {
504
- event.cancelBubble=true;
505
- var threads = document.getElementsByName("thread");
506
- var l = threads.length;
507
- for ( var i = 0; i < l ; i++ ) {
508
- hideUL(threads[i]);
509
- }
510
- selectNode(rootNode(), null);
511
- }
512
-
513
- function expandAll(event) {
514
- event.cancelBubble=true;
515
- var threads = document.getElementsByName("thread");
516
- var l = threads.length;
517
- for ( var i = 0; i < l ; i++ ) {
518
- showUL(threads[i]);
519
- }
520
- }
521
-
522
- function toggleHelp(node) {
523
- var help = document.getElementById("help");
524
- if (node.value == "Show Help") {
525
- node.value = "Hide Help";
526
- help.style.display = 'block';
527
- }
528
- else {
529
- node.value = "Show Help";
530
- help.style.display = 'none';
531
- }
532
- }
533
-
534
- var selectedNode = null;
535
- var selectedColor = null;
536
- var selectedThread = null;
537
-
538
- function descendentOf(a,b){
539
- while (a!=b && b!=null)
540
- b=b.parentNode;
541
- return (a==b);
542
- }
543
-
544
- function moveSelectionIfNecessary(node){
545
- if (descendentOf(node, selectedNode))
546
- selectNode(node, null);
547
- }
548
-
549
- function selectNode(node, event) {
550
- if (event) {
551
- event.cancelBubble = true;
552
- thread = findThread(node);
553
- selectThread(thread);
554
- }
555
- if (selectedNode) {
556
- selectedNode.style.background = selectedColor;
557
- }
558
- selectedNode = node;
559
- selectedColor = node.style.background;
560
- selectedNode.style.background = "red";
561
- selectedNode.scrollIntoView();
562
- window.scrollBy(0,-400);
563
- }
564
-
565
- function moveUp(){
566
- var p = selectedNode.previousSibling;
567
- while (p && p.style.display == 'none')
568
- p = p.previousSibling;
569
- if (p && p.nodeName == "LI") {
570
- selectNode(p, null);
571
- }
572
- }
573
-
574
- function moveDown(){
575
- var p = selectedNode.nextSibling;
576
- while (p && p.style.display == 'none')
577
- p = p.nextSibling;
578
- if (p && p.nodeName == "LI") {
579
- selectNode(p, null);
580
- }
581
- }
582
-
583
- function moveLeft(){
584
- var p = selectedNode.parentNode.parentNode;
585
- if (p && p.nodeName=="LI") {
586
- selectNode(p, null);
587
- }
588
- }
589
-
590
- function moveRight(){
591
- if (!isLeafNode(selectedNode)) {
592
- showChildren(selectedNode);
593
- var ul = findUlChild(selectedNode);
594
- if (ul) {
595
- selectNode(ul.firstChild, null);
596
- }
597
- }
598
- }
599
-
600
- function moveForward(){
601
- if (isLeafNode(selectedNode)) {
602
- var p = selectedNode;
603
- while ((p.nextSibling == null || p.nextSibling.style.display=='none') && p.nodeName=="LI") {
604
- p = p.parentNode.parentNode;
605
- }
606
- if (p.nodeName=="LI")
607
- selectNode(p.nextSibling, null);
608
- }
609
- else {
610
- moveRight();
611
- }
612
- }
613
-
614
- function isExpandedNode(li){
615
- var img = li.firstChild;
616
- return(img.src.indexOf('minus.png')>-1);
617
- }
618
-
619
- function moveBackward(){
620
- var p = selectedNode;
621
- var q = p.previousSibling;
622
- while (q != null && q.style.display=='none')
623
- q = q.previousSibling;
624
- if (q == null) {
625
- p = p.parentNode.parentNode;
626
- } else {
627
- while (!isLeafNode(q) && isExpandedNode(q)) {
628
- q = findUlChild(q).lastChild;
629
- while (q.style.display=='none')
630
- q = q.previousSibling;
631
- }
632
- p = q;
633
- }
634
- if (p.nodeName=="LI")
635
- selectNode(p, null);
636
- }
637
-
638
- function moveHome() {
639
- selectNode(currentThread);
640
- }
641
-
642
- var currentThreadIndex = null;
643
-
644
- function findThread(node){
645
- while (node && node.parentNode.nodeName!="BODY") {
646
- node = node.parentNode;
647
- }
648
- return node.firstChild;
649
- }
650
-
651
- function selectThread(node){
652
- var threads = document.getElementsByName("thread");
653
- currentThread = node;
654
- for (var i=0; i<threads.length; i++) {
655
- if (threads[i]==currentThread.parentNode)
656
- currentThreadIndex = i;
657
- }
658
- }
659
-
660
- function nextThread(){
661
- var threads = document.getElementsByName("thread");
662
- if (currentThreadIndex==threads.length-1)
663
- currentThreadIndex = 0;
664
- else
665
- currentThreadIndex += 1
666
- currentThread = threads[currentThreadIndex].firstChild;
667
- selectNode(currentThread, null);
668
- }
669
-
670
- function previousThread(){
671
- var threads = document.getElementsByName("thread");
672
- if (currentThreadIndex==0)
673
- currentThreadIndex = threads.length-1;
674
- else
675
- currentThreadIndex -= 1
676
- currentThread = threads[currentThreadIndex].firstChild;
677
- selectNode(currentThread, null);
678
- }
679
-
680
- function switchThread(node, event){
681
- event.cancelBubble = true;
682
- selectThread(node.nextSibling.firstChild);
683
- selectNode(currentThread, null);
684
- }
685
-
686
- function handleKeyEvent(event){
687
- var code = event.charCode ? event.charCode : event.keyCode;
688
- var str = String.fromCharCode(code);
689
- switch (str) {
690
- case "a": moveLeft(); break;
691
- case "s": moveDown(); break;
692
- case "d": moveRight(); break;
693
- case "w": moveUp(); break;
694
- case "f": moveForward(); break;
695
- case "b": moveBackward(); break;
696
- case "x": toggleChildren(selectedNode.firstChild, event); break;
697
- case "*": toggleLI(selectedNode); break;
698
- case "n": nextThread(); break;
699
- case "h": moveHome(); break;
700
- case "p": previousThread(); break;
701
- }
702
- }
703
- document.onkeypress=function(event){ handleKeyEvent(event) };
704
-
705
- window.onload=function(){
706
- var images = document.getElementsByTagName("img");
707
- for (var i=0; i<images.length; i++) {
708
- var img = images[i];
709
- if (img.className == "toggle") {
710
- img.onclick = function(event){ toggleChildren(this, event); };
711
- }
712
- }
713
- var divs = document.getElementsByTagName("div");
714
- for (i=0; i<divs.length; i++) {
715
- var div = divs[i];
716
- if (div.className == "thread")
717
- div.onclick = function(event){ switchThread(this, event) };
718
- }
719
- var lis = document.getElementsByTagName("li");
720
- for (var i=0; i<lis.length; i++) {
721
- lis[i].onclick = function(event){ selectNode(this, event); };
722
- }
723
- var threads = document.getElementsByName("thread");
724
- currentThreadIndex = 0;
725
- currentThread = threads[0].firstChild;
726
- selectNode(currentThread, null);
727
- }
728
- </script>
729
- end_java_script
730
- end
731
-
732
- def print_title_bar
733
- @output.puts <<-"end_title_bar"
734
- <div id="titlebar">
735
- Call tree for application <b>#{h application} #{h arguments}</b><br/>
736
- Generated on #{Time.now} with options #{h @options.inspect}<br/>
737
- </div>
738
- end_title_bar
739
- end
740
-
741
- def print_commands
742
- @output.puts <<-"end_commands"
743
- <div id=\"commands\">
744
- <span style=\"font-size: 11pt; font-weight: bold;\">Threshold:</span>
745
- <input value=\"#{h threshold}\" size=\"3\" id=\"threshold\" type=\"text\">
746
- <input value=\"Apply\" onclick=\"setThreshold();\" type=\"submit\">
747
- <input value=\"Expand All\" onclick=\"expandAll(event);\" type=\"submit\">
748
- <input value=\"Collapse All\" onclick=\"collapseAll(event);\" type=\"submit\">
749
- <input value=\"Show Help\" onclick=\"toggleHelp(this);\" type=\"submit\">
750
- </div>
751
- end_commands
752
- end
753
-
754
- def print_help
755
- @output.puts <<-'end_help'
756
- <div style="display: none;" id="help">
757
- <img src="empty.png"> Enter a decimal value <i>d</i> into the threshold field and click "Apply"
758
- to hide all nodes marked with time values lower than <i>d</i>.<br>
759
- <img src="empty.png"> Click on "Expand All" for full tree expansion.<br>
760
- <img src="empty.png"> Click on "Collapse All" to show only top level nodes.<br>
761
- <img src="empty.png"> Use a, s, d, w as in Quake or Urban Terror to navigate the tree.<br>
762
- <img src="empty.png"> Use f and b to navigate the tree in preorder forward and backwards.<br>
763
- <img src="empty.png"> Use x to toggle visibility of a subtree.<br>
764
- <img src="empty.png"> Use * to expand/collapse a whole subtree.<br>
765
- <img src="empty.png"> Use h to navigate to thread root.<br>
766
- <img src="empty.png"> Use n and p to navigate between threads.<br>
767
- <img src="empty.png"> Click on background to move focus to a subtree.<br>
768
- </div>
769
- end_help
770
- end
771
- end
772
- end
773
-
1
+ # encoding: utf-8
2
+
3
+ require 'erb'
4
+ require 'fileutils'
5
+ require 'base64'
6
+ require 'set'
7
+ require 'stringio'
8
+
9
+ module RubyProf
10
+ # Prints a HTML visualization of the call tree.
11
+ #
12
+ # To use the printer:
13
+ #
14
+ # result = RubyProf.profile do
15
+ # [code to profile]
16
+ # end
17
+ #
18
+ # printer = RubyProf::CallStackPrinter.new(result)
19
+ # printer.print(STDOUT)
20
+
21
+ class CallStackPrinter < AbstractPrinter
22
+ include ERB::Util
23
+
24
+ # Specify print options.
25
+ #
26
+ # options - Hash table
27
+ # :min_percent - Number 0 to 100 that specifes the minimum
28
+ # %self (the methods self time divided by the
29
+ # overall total time) that a method must take
30
+ # for it to be printed out in the report.
31
+ # Default value is 0.
32
+ #
33
+ # :threshold - a float from 0 to 100 that sets the threshold of
34
+ # results displayed.
35
+ # Default value is 1.0
36
+ #
37
+ # :title - a String to overide the default "ruby-prof call tree"
38
+ # title of the report.
39
+ #
40
+ # :expansion - a float from 0 to 100 that sets the threshold of
41
+ # results that are expanded, if the percent_total
42
+ # exceeds it.
43
+ # Default value is 10.0
44
+ #
45
+ # :application - a String to overide the name of the application,
46
+ # as it appears on the report.
47
+ def print(output = STDOUT, options = {})
48
+ setup_options(options)
49
+ output << @erb.result(binding)
50
+ end
51
+
52
+ # :enddoc:
53
+ def setup_options(options)
54
+ super(options)
55
+ @erb = ERB.new(self.template)
56
+ end
57
+
58
+ def print_stack(output, visited, call_tree, parent_time)
59
+ total_time = call_tree.total_time
60
+ percent_parent = (total_time/parent_time)*100
61
+ percent_total = (total_time/@overall_time)*100
62
+ return unless percent_total > min_percent
63
+ color = self.color(percent_total)
64
+ visible = percent_total >= threshold
65
+ expanded = percent_total >= expansion
66
+ display = visible ? "block" : "none"
67
+
68
+ output << "<li class=\"color#{color}\" style=\"display:#{display}\">" << "\n"
69
+
70
+ if visited.include?(call_tree)
71
+ output << "<a href=\"#\" class=\"toggle empty\" ></a>" << "\n"
72
+ output << "<span>%s %s</span>" % [link(call_tree.target, true), graph_link(call_tree)] << "\n"
73
+ else
74
+ visited << call_tree
75
+
76
+ if call_tree.children.empty?
77
+ output << "<a href=\"#\" class=\"toggle empty\" ></a>" << "\n"
78
+ else
79
+ visible_children = call_tree.children.any?{|ci| (ci.total_time/@overall_time)*100 >= threshold}
80
+ image = visible_children ? (expanded ? "minus" : "plus") : "empty"
81
+ output << "<a href=\"#\" class=\"toggle #{image}\" ></a>" << "\n"
82
+ end
83
+ output << "<span>%4.2f%% (%4.2f%%) %s %s</span>" % [percent_total, percent_parent,
84
+ link(call_tree.target, false), graph_link(call_tree)] << "\n"
85
+
86
+ unless call_tree.children.empty?
87
+ output << (expanded ? '<ul>' : '<ul style="display:none">') << "\n"
88
+ call_tree.children.sort_by{|c| -c.total_time}.each do |child_call_tree|
89
+ print_stack(output, visited, child_call_tree, total_time)
90
+ end
91
+ output << '</ul>' << "\n"
92
+ end
93
+
94
+ visited.delete(call_tree)
95
+ end
96
+ output << '</li>' << "\n"
97
+ end
98
+
99
+ def name(call_tree)
100
+ method = call_tree.target
101
+ method.full_name
102
+ end
103
+
104
+ def link(method, recursive)
105
+ method_name = "#{recursive ? '*' : ''}#{method.full_name}"
106
+ if method.source_file.nil?
107
+ h method_name
108
+ else
109
+ file = File.expand_path(method.source_file)
110
+ "<a href=\"file://#{file}##{method.line}\">#{h method_name}</a>"
111
+ end
112
+ end
113
+
114
+ def graph_link(call_tree)
115
+ total_calls = call_tree.target.called
116
+ totals = total_calls.to_s
117
+ "[#{call_tree.called} calls, #{totals} total]"
118
+ end
119
+
120
+ def method_href(method)
121
+ h(method.full_name.gsub(/[><#\.\?=:]/,"_"))
122
+ end
123
+
124
+ def total_time(call_trees)
125
+ sum(call_trees.map{|ci| ci.total_time})
126
+ end
127
+
128
+ def sum(a)
129
+ a.inject(0.0){|s,t| s+=t}
130
+ end
131
+
132
+ def dump(ci)
133
+ $stderr.printf "%s/%d t:%f s:%f w:%f \n", ci, ci.object_id, ci.total_time, ci.self_time, ci.wait_time
134
+ end
135
+
136
+ def color(p)
137
+ case i = p.to_i
138
+ when 0..5
139
+ "01"
140
+ when 5..10
141
+ "05"
142
+ when 100
143
+ "9"
144
+ else
145
+ "#{i/10}"
146
+ end
147
+ end
148
+
149
+ def application
150
+ @options[:application] || $PROGRAM_NAME
151
+ end
152
+
153
+ def arguments
154
+ ARGV.join(' ')
155
+ end
156
+
157
+ def title
158
+ @title ||= @options.delete(:title) || "ruby-prof call tree"
159
+ end
160
+
161
+ def threshold
162
+ @options[:threshold] || 1.0
163
+ end
164
+
165
+ def expansion
166
+ @options[:expansion] || 10.0
167
+ end
168
+
169
+ def base64_image
170
+ @data ||= begin
171
+ file = open_asset('call_stack_printer.png')
172
+ Base64.encode64(file).gsub(/\n/, '')
173
+ end
174
+ end
175
+
176
+ def template
177
+ open_asset('call_stack_printer.html.erb')
178
+ end
179
+ end
180
+ end