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,56 +1,127 @@
1
- # encoding: utf-8
2
-
3
- module RubyProf
4
- # Helper class to simplify printing profiles of several types from
5
- # one profiling run. Currently prints a flat profile, a callgrind
6
- # profile, a call stack profile and a graph profile.
7
- class MultiPrinter
8
- def initialize(result)
9
- @stack_printer = CallStackPrinter.new(result)
10
- @graph_printer = GraphHtmlPrinter.new(result)
11
- @tree_printer = CallTreePrinter.new(result)
12
- @flat_printer = FlatPrinter.new(result)
13
- end
14
-
15
- # create profile files under options[:path] or the current
16
- # directory. options[:profile] is used as the base name for the
17
- # pofile file, defaults to "profile".
18
- def print(options)
19
- @profile = options.delete(:profile) || "profile"
20
- @directory = options.delete(:path) || File.expand_path(".")
21
- File.open(stack_profile, "w") do |f|
22
- @stack_printer.print(f, options.merge(:graph => "#{@profile}.graph.html"))
23
- end
24
- File.open(graph_profile, "w") do |f|
25
- @graph_printer.print(f, options)
26
- end
27
- File.open(tree_profile, "w") do |f|
28
- @tree_printer.print(f, options)
29
- end
30
- File.open(flat_profile, "w") do |f|
31
- @flat_printer.print(f, options)
32
- end
33
- end
34
-
35
- # the name of the call stack profile file
36
- def stack_profile
37
- "#{@directory}/#{@profile}.stack.html"
38
- end
39
-
40
- # the name of the graph profile file
41
- def graph_profile
42
- "#{@directory}/#{@profile}.graph.html"
43
- end
44
-
45
- # the name of the callgrind profile file
46
- def tree_profile
47
- "#{@directory}/#{@profile}.grind.dat"
48
- end
49
-
50
- # the name of the flat profile file
51
- def flat_profile
52
- "#{@directory}/#{@profile}.flat.txt"
53
- end
54
-
55
- end
56
- end
1
+ # encoding: utf-8
2
+
3
+ module RubyProf
4
+ # Helper class to simplify printing profiles of several types from
5
+ # one profiling run. Currently prints a flat profile, a callgrind
6
+ # profile, a call stack profile and a graph profile.
7
+ class MultiPrinter
8
+ def initialize(result, printers = [:flat, :graph_html])
9
+ @flat_printer = FlatPrinter.new(result) if printers.include?(:flat)
10
+
11
+ @graph_printer = GraphPrinter.new(result) if printers.include?(:graph)
12
+ @graph_html_printer = GraphHtmlPrinter.new(result) if printers.include?(:graph_html)
13
+
14
+ @tree_printer = CallTreePrinter.new(result) if printers.include?(:tree)
15
+ @call_info_printer = CallInfoPrinter.new(result) if printers.include?(:call_tree)
16
+
17
+ @stack_printer = CallStackPrinter.new(result) if printers.include?(:stack)
18
+ @dot_printer = DotPrinter.new(result) if printers.include?(:dot)
19
+ end
20
+
21
+ def self.needs_dir?
22
+ true
23
+ end
24
+
25
+ # create profile files under options[:path] or the current
26
+ # directory. options[:profile] is used as the base name for the
27
+ # profile file, defaults to "profile".
28
+ def print(options)
29
+ validate_print_params(options)
30
+
31
+ @profile = options.delete(:profile) || "profile"
32
+ @directory = options.delete(:path) || File.expand_path(".")
33
+
34
+ print_to_flat(options) if @flat_printer
35
+
36
+ print_to_graph(options) if @graph_printer
37
+ print_to_graph_html(options) if @graph_html_printer
38
+
39
+ print_to_stack(options) if @stack_printer
40
+ print_to_call_info(options) if @call_info_printer
41
+ print_to_tree(options) if @tree_printer
42
+ print_to_dot(options) if @dot_printer
43
+ end
44
+
45
+ # the name of the flat profile file
46
+ def flat_report
47
+ "#{@directory}/#{@profile}.flat.txt"
48
+ end
49
+
50
+ # the name of the graph profile file
51
+ def graph_report
52
+ "#{@directory}/#{@profile}.graph.txt"
53
+ end
54
+
55
+ def graph_html_report
56
+ "#{@directory}/#{@profile}.graph.html"
57
+ end
58
+
59
+ # the name of the callinfo profile file
60
+ def call_info_report
61
+ "#{@directory}/#{@profile}.call_tree.txt"
62
+ end
63
+
64
+ # the name of the callgrind profile file
65
+ def tree_report
66
+ "#{@directory}/#{@profile}.callgrind.out.#{$$}"
67
+ end
68
+
69
+ # the name of the call stack profile file
70
+ def stack_report
71
+ "#{@directory}/#{@profile}.stack.html"
72
+ end
73
+
74
+ # the name of the call stack profile file
75
+ def dot_report
76
+ "#{@directory}/#{@profile}.dot"
77
+ end
78
+
79
+ def print_to_flat(options)
80
+ File.open(flat_report, "wb") do |file|
81
+ @flat_printer.print(file, options)
82
+ end
83
+ end
84
+
85
+ def print_to_graph(options)
86
+ File.open(graph_report, "wb") do |file|
87
+ @graph_printer.print(file, options)
88
+ end
89
+ end
90
+
91
+ def print_to_graph_html(options)
92
+ File.open(graph_html_report, "wb") do |file|
93
+ @graph_html_printer.print(file, options)
94
+ end
95
+ end
96
+
97
+ def print_to_call_info(options)
98
+ File.open(call_info_report, "wb") do |file|
99
+ @call_info_printer.print(file, options)
100
+ end
101
+ end
102
+
103
+ def print_to_tree(options)
104
+ @tree_printer.print(options.merge(:path => @directory, :profile => @profile))
105
+ end
106
+
107
+ def print_to_stack(options)
108
+ File.open(stack_report, "wb") do |file|
109
+ @stack_printer.print(file, options.merge(:graph => "#{@profile}.graph.html"))
110
+ end
111
+ end
112
+
113
+ def print_to_dot(options)
114
+ File.open(dot_report, "wb") do |file|
115
+ @dot_printer.print(file, options)
116
+ end
117
+ end
118
+
119
+ def validate_print_params(options)
120
+ if options.is_a?(IO)
121
+ raise ArgumentError, "#{self.class.name}#print cannot print to IO objects"
122
+ elsif !options.is_a?(Hash)
123
+ raise ArgumentError, "#{self.class.name}#print requires an options hash"
124
+ end
125
+ end
126
+ end
127
+ end
@@ -1,77 +1,37 @@
1
- # encoding: utf-8
2
-
3
- require 'set'
4
- module RubyProf
5
- class Profile
6
- # This method gets called once profiling has been completed
7
- # but before results are returned to the user. Thus it provides
8
- # a hook to do any necessary post-processing on the call graph.
9
- def post_process
10
- self.threads.each do |thread|
11
- detect_recursion(thread)
12
- end
13
- end
14
-
15
- # This method detect recursive calls in the call graph.
16
- def detect_recursion(thread)
17
- visited_methods = Hash.new do |hash, key|
18
- hash[key] = 0
19
- end
20
-
21
- visitor = CallInfoVisitor.new(thread)
22
- visitor.visit do |call_info, event|
23
- case event
24
- when :enter
25
- visited_methods[call_info.target] += 1
26
- call_info.recursive = (visited_methods[call_info.target] > 1)
27
- when :exit
28
- visited_methods[call_info.target] -= 1
29
- if visited_methods[call_info.target] == 0
30
- visited_methods.delete(call_info.target)
31
- end
32
- end
33
- end
34
- end
35
-
36
- # eliminate some calls from the graph by merging the information into callers.
37
- # matchers can be a list of strings or regular expressions or the name of a file containing regexps.
38
- def eliminate_methods!(matchers)
39
- matchers = read_regexps_from_file(matchers) if matchers.is_a?(String)
40
- eliminated = []
41
- threads.each do |thread|
42
- matchers.each{ |matcher| eliminated.concat(eliminate_methods(thread.methods, matcher)) }
43
- end
44
- eliminated
45
- end
46
-
47
- private
48
-
49
- # read regexps from file
50
- def read_regexps_from_file(file_name)
51
- matchers = []
52
- File.open(file_name).each_line do |l|
53
- next if (l =~ /^(#.*|\s*)$/) # emtpy lines and lines starting with #
54
- matchers << Regexp.new(l.strip)
55
- end
56
- end
57
-
58
- # eliminate methods matching matcher
59
- def eliminate_methods(methods, matcher)
60
- eliminated = []
61
- i = 0
62
- while i < methods.size
63
- method_info = methods[i]
64
- method_name = method_info.full_name
65
- if matcher === method_name
66
- raise "can't eliminate root method" if method_info.root?
67
- eliminated << methods.delete_at(i)
68
- method_info.eliminate!
69
- else
70
- i += 1
71
- end
72
- end
73
- eliminated
74
- end
75
-
76
- end
77
- end
1
+ # encoding: utf-8
2
+
3
+ require 'ruby-prof/exclude_common_methods'
4
+
5
+ module RubyProf
6
+ class Profile
7
+ # :nodoc:
8
+ def measure_mode_string
9
+ case self.measure_mode
10
+ when WALL_TIME
11
+ "wall_time"
12
+ when PROCESS_TIME
13
+ "process_time"
14
+ when ALLOCATIONS
15
+ "allocations"
16
+ when MEMORY
17
+ "memory"
18
+ end
19
+ end
20
+
21
+ # Hides methods that, when represented as a call graph, have
22
+ # extremely large in and out degrees and make navigation impossible.
23
+ def exclude_common_methods!
24
+ ExcludeCommonMethods.apply!(self)
25
+ end
26
+
27
+ def exclude_methods!(mod, *method_or_methods)
28
+ [method_or_methods].flatten.each do |name|
29
+ exclude_method!(mod, name)
30
+ end
31
+ end
32
+
33
+ def exclude_singleton_methods!(mod, *method_or_methods)
34
+ exclude_methods!(mod.singleton_class, *method_or_methods)
35
+ end
36
+ end
37
+ end
@@ -7,42 +7,89 @@ module Rack
7
7
  @app = app
8
8
  @options = options
9
9
  @options[:min_percent] ||= 1
10
+
10
11
  @tmpdir = options[:path] || Dir.tmpdir
12
+ FileUtils.mkdir_p(@tmpdir)
13
+
11
14
  @printer_klasses = @options[:printers] || {::RubyProf::FlatPrinter => 'flat.txt',
12
15
  ::RubyProf::GraphPrinter => 'graph.txt',
13
16
  ::RubyProf::GraphHtmlPrinter => 'graph.html',
14
17
  ::RubyProf::CallStackPrinter => 'call_stack.html'}
15
18
 
16
- @skip_paths = options[:skip_paths] || [%r{^/assets}, %r{\.css$}, %r{\.js$}, %r{\.png$}, %r{\.jpeg$}, %r{\.jpg$}, %r{\.gif$}]
19
+ @skip_paths = options[:skip_paths] || [%r{^/assets}, %r{\.(css|js|png|jpeg|jpg|gif)$}]
20
+ @only_paths = options[:only_paths]
17
21
  end
18
22
 
19
23
  def call(env)
20
24
  request = Rack::Request.new(env)
21
25
 
22
- if @skip_paths.any? {|skip_path| skip_path =~ request.path}
23
- @app.call(env)
24
- else
25
- result = nil
26
- data = ::RubyProf::Profile.profile do
27
- result = @app.call(env)
26
+ if should_profile?(request.path)
27
+ begin
28
+ result = nil
29
+ data = ::RubyProf::Profile.profile(profiling_options) do
30
+ result = @app.call(env)
31
+ end
32
+
33
+ path = request.path.gsub('/', '-')
34
+ path.slice!(0)
35
+
36
+ print(data, path)
37
+ result
28
38
  end
39
+ else
40
+ @app.call(env)
41
+ end
42
+ end
29
43
 
30
- path = request.path.gsub('/', '-')
31
- path.slice!(0)
44
+ private
32
45
 
33
- print(data, path)
34
- result
46
+ def should_profile?(path)
47
+ return false if paths_match?(path, @skip_paths)
48
+
49
+ @only_paths ? paths_match?(path, @only_paths) : true
50
+ end
51
+
52
+ def paths_match?(path, paths)
53
+ paths.any? { |skip_path| skip_path =~ path }
54
+ end
55
+
56
+ def profiling_options
57
+ options = {}
58
+ options[:measure_mode] = ::RubyProf.measure_mode
59
+ options[:exclude_threads] =
60
+ if @options[:ignore_existing_threads]
61
+ Thread.list.select{|t| t != Thread.current}
62
+ else
63
+ ::RubyProf.exclude_threads
64
+ end
65
+ if @options[:request_thread_only]
66
+ options[:include_threads] = [Thread.current]
67
+ end
68
+ if @options[:merge_fibers]
69
+ options[:merge_fibers] = true
35
70
  end
71
+ options
36
72
  end
37
73
 
38
74
  def print(data, path)
39
75
  @printer_klasses.each do |printer_klass, base_name|
40
76
  printer = printer_klass.new(data)
41
- file_name = ::File.join(@tmpdir, "#{path}-#{base_name}")
42
- ::File.open(file_name, 'wb') do |file|
43
- printer.print(file, @options)
77
+
78
+ if base_name.respond_to?(:call)
79
+ base_name = base_name.call
80
+ end
81
+
82
+ if printer_klass == ::RubyProf::MultiPrinter
83
+ printer.print(@options.merge(:profile => "#{path}-#{base_name}"))
84
+ elsif printer_klass == ::RubyProf::CallTreePrinter
85
+ printer.print(@options.merge(:profile => "#{path}-#{base_name}"))
86
+ else
87
+ file_name = ::File.join(@tmpdir, "#{path}-#{base_name}")
88
+ ::File.open(file_name, 'wb') do |file|
89
+ printer.print(file, @options)
90
+ end
44
91
  end
45
92
  end
46
93
  end
47
94
  end
48
- end
95
+ end
@@ -1,147 +1,147 @@
1
- #!/usr/bin/env ruby
2
- # encoding: utf-8
3
-
4
- require 'rake'
5
- require 'rake/testtask'
6
- require 'fileutils'
7
-
8
- module RubyProf
9
-
10
- # Define a task library for profiling unit tests with ruby-prof.
11
- #
12
- # All of the options provided by
13
- # the Rake:TestTask are supported except the loader
14
- # which is set to ruby-prof. For detailed information
15
- # please refer to the Rake:TestTask documentation.
16
- #
17
- # ruby-prof specific options include:
18
- #
19
- # output_dir - For each file specified an output
20
- # file with profile information will be
21
- # written to the output directory.
22
- # By default, the output directory is
23
- # called "profile" and is created underneath
24
- # the current working directory.
25
- #
26
- # printer - Specifies the output printer. Valid values include
27
- # :flat, :graph, :graph_html and :call_tree.
28
- #
29
- # min_percent - Methods that take less than the specified percent
30
- # will not be written out.
31
- #
32
- # Example:
33
- #
34
- # require 'ruby-prof/task'
35
- #
36
- # RubyProf::ProfileTask.new do |t|
37
- # t.test_files = FileList['test/test*.rb']
38
- # t.output_dir = "c:/temp"
39
- # t.printer = :graph
40
- # t.min_percent = 10
41
- # end
42
- #
43
- # If rake is invoked with a "TEST=filename" command line option,
44
- # then the list of test files will be overridden to include only the
45
- # filename specified on the command line. This provides an easy way
46
- # to run just one test.
47
- #
48
- # If rake is invoked with a "TESTOPTS=options" command line option,
49
- # then the given options are passed to the test process after a
50
- # '--'. This allows Test::Unit options to be passed to the test
51
- # suite.
52
- #
53
- # Examples:
54
- #
55
- # rake profile # run tests normally
56
- # rake profile TEST=just_one_file.rb # run just one test file.
57
- # rake profile TESTOPTS="-v" # run in verbose mode
58
- # rake profile TESTOPTS="--runner=fox" # use the fox test runner
59
-
60
- class ProfileTask < Rake::TestTask
61
- attr_accessor :output_dir
62
- attr_accessor :min_percent
63
- attr_accessor :printer
64
-
65
- def initialize(name = :profile)
66
- super(name)
67
- end
68
-
69
- # Create the tasks defined by this task lib.
70
- def define
71
- lib_path = @libs.join(File::PATH_SEPARATOR)
72
- desc "Profile" + (@name==:profile ? "" : " for #{@name}")
73
-
74
- task @name do
75
- create_output_directory
76
-
77
- @ruby_opts.unshift( "-I#{lib_path}" )
78
- @ruby_opts.unshift( "-w" ) if @warning
79
- @ruby_opts.push("-S ruby-prof")
80
- @ruby_opts.push("--printer #{@printer}")
81
- @ruby_opts.push("--min_percent #{@min_percent}")
82
-
83
- file_list.each do |file_path|
84
- run_script(file_path)
85
- end
86
- end
87
- self
88
- end
89
-
90
- # Run script
91
- def run_script(script_path)
92
- run_code = ''
93
- RakeFileUtils.verbose(@verbose) do
94
- file_name = File.basename(script_path, File.extname(script_path))
95
- case @printer
96
- when :flat, :graph, :call_tree
97
- file_name += ".txt"
98
- when :graph_html
99
- file_name += ".html"
100
- else
101
- file_name += ".txt"
102
- end
103
-
104
- output_file_path = File.join(output_directory, file_name)
105
-
106
- command_line = @ruby_opts.join(" ") +
107
- " --file=" + output_file_path +
108
- " " + script_path
109
-
110
- puts "ruby " + command_line
111
- # We have to catch the exeption to continue on. However,
112
- # the error message will have been output to STDERR
113
- # already by the time we get here so we don't have to
114
- # do that again
115
- begin
116
- ruby command_line
117
- rescue => e
118
- STDOUT << e << "\n"
119
- STDOUT.flush
120
- end
121
- puts ""
122
- puts ""
123
- end
124
- end
125
-
126
- def output_directory
127
- File.expand_path(@output_dir)
128
- end
129
-
130
- def create_output_directory
131
- if not File.exist?(output_directory)
132
- Dir.mkdir(output_directory)
133
- end
134
- end
135
-
136
- def clean_output_directory
137
- if File.exist?(output_directory)
138
- files = Dir.glob(output_directory + '/*')
139
- FileUtils.rm(files)
140
- end
141
- end
142
-
143
- def option_list # :nodoc:
144
- ENV['OPTIONS'] || @options.join(" ") || ""
145
- end
146
- end
147
- end
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ require 'rake'
5
+ require 'rake/testtask'
6
+ require 'fileutils'
7
+
8
+ module RubyProf
9
+
10
+ # Define a task library for profiling unit tests with ruby-prof.
11
+ #
12
+ # All of the options provided by
13
+ # the Rake:TestTask are supported except the loader
14
+ # which is set to ruby-prof. For detailed information
15
+ # please refer to the Rake:TestTask documentation.
16
+ #
17
+ # ruby-prof specific options include:
18
+ #
19
+ # output_dir - For each file specified an output
20
+ # file with profile information will be
21
+ # written to the output directory.
22
+ # By default, the output directory is
23
+ # called "profile" and is created underneath
24
+ # the current working directory.
25
+ #
26
+ # printer - Specifies the output printer. Valid values include
27
+ # :flat, :graph, :graph_html and :call_tree.
28
+ #
29
+ # min_percent - Methods that take less than the specified percent
30
+ # will not be written out.
31
+ #
32
+ # Example:
33
+ #
34
+ # require 'ruby-prof/task'
35
+ #
36
+ # RubyProf::ProfileTask.new do |t|
37
+ # t.test_files = FileList['test/test*.rb']
38
+ # t.output_dir = "c:/temp"
39
+ # t.printer = :graph
40
+ # t.min_percent = 10
41
+ # end
42
+ #
43
+ # If rake is invoked with a "TEST=filename" command line option,
44
+ # then the list of test files will be overridden to include only the
45
+ # filename specified on the command line. This provides an easy way
46
+ # to run just one test.
47
+ #
48
+ # If rake is invoked with a "TESTOPTS=options" command line option,
49
+ # then the given options are passed to the test process after a
50
+ # '--'. This allows Test::Unit options to be passed to the test
51
+ # suite.
52
+ #
53
+ # Examples:
54
+ #
55
+ # rake profile # run tests normally
56
+ # rake profile TEST=just_one_file.rb # run just one test file.
57
+ # rake profile TESTOPTS="-v" # run in verbose mode
58
+ # rake profile TESTOPTS="--runner=fox" # use the fox test runner
59
+
60
+ class ProfileTask < Rake::TestTask
61
+ attr_accessor :output_dir
62
+ attr_accessor :min_percent
63
+ attr_accessor :printer
64
+
65
+ def initialize(name = :profile)
66
+ super(name)
67
+ end
68
+
69
+ # Create the tasks defined by this task lib.
70
+ def define
71
+ lib_path = @libs.join(File::PATH_SEPARATOR)
72
+ desc "Profile" + (@name==:profile ? "" : " for #{@name}")
73
+
74
+ task @name do
75
+ create_output_directory
76
+
77
+ @ruby_opts.unshift( "-I#{lib_path}" )
78
+ @ruby_opts.unshift( "-w" ) if @warning
79
+ @ruby_opts.push("-S ruby-prof")
80
+ @ruby_opts.push("--printer #{@printer}")
81
+ @ruby_opts.push("--min_percent #{@min_percent}")
82
+
83
+ file_list.each do |file_path|
84
+ run_script(file_path)
85
+ end
86
+ end
87
+ self
88
+ end
89
+
90
+ # Run script
91
+ def run_script(script_path)
92
+ run_code = ''
93
+ RakeFileUtils.verbose(@verbose) do
94
+ file_name = File.basename(script_path, File.extname(script_path))
95
+ case @printer
96
+ when :flat, :graph, :call_tree
97
+ file_name += ".txt"
98
+ when :graph_html
99
+ file_name += ".html"
100
+ else
101
+ file_name += ".txt"
102
+ end
103
+
104
+ output_file_path = File.join(output_directory, file_name)
105
+
106
+ command_line = @ruby_opts.join(" ") +
107
+ " --file=" + output_file_path +
108
+ " " + script_path
109
+
110
+ puts "ruby " + command_line
111
+ # We have to catch the exeption to continue on. However,
112
+ # the error message will have been output to STDERR
113
+ # already by the time we get here so we don't have to
114
+ # do that again
115
+ begin
116
+ ruby command_line
117
+ rescue => e
118
+ STDOUT << e << "\n"
119
+ STDOUT.flush
120
+ end
121
+ puts ""
122
+ puts ""
123
+ end
124
+ end
125
+
126
+ def output_directory
127
+ File.expand_path(@output_dir)
128
+ end
129
+
130
+ def create_output_directory
131
+ if not File.exist?(output_directory)
132
+ Dir.mkdir(output_directory)
133
+ end
134
+ end
135
+
136
+ def clean_output_directory
137
+ if File.exist?(output_directory)
138
+ files = Dir.glob(output_directory + '/*')
139
+ FileUtils.rm(files)
140
+ end
141
+ end
142
+
143
+ def option_list # :nodoc:
144
+ ENV['OPTIONS'] || @options.join(" ") || ""
145
+ end
146
+ end
147
+ end