ruby-prof 0.16.2 → 1.1.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 (203) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGES +532 -467
  3. data/LICENSE +24 -24
  4. data/README.rdoc +5 -454
  5. data/Rakefile +110 -113
  6. data/bin/ruby-prof +380 -340
  7. data/bin/ruby-prof-check-trace +45 -45
  8. data/ext/ruby_prof/extconf.rb +36 -64
  9. data/ext/ruby_prof/rp_allocation.c +279 -0
  10. data/ext/ruby_prof/rp_allocation.h +31 -0
  11. data/ext/ruby_prof/rp_call_info.c +271 -407
  12. data/ext/ruby_prof/rp_call_info.h +35 -48
  13. data/ext/ruby_prof/rp_measure_allocations.c +52 -76
  14. data/ext/ruby_prof/rp_measure_memory.c +42 -77
  15. data/ext/ruby_prof/rp_measure_process_time.c +67 -71
  16. data/ext/ruby_prof/rp_measure_wall_time.c +62 -45
  17. data/ext/ruby_prof/rp_measurement.c +230 -0
  18. data/ext/ruby_prof/rp_measurement.h +50 -0
  19. data/ext/ruby_prof/rp_method.c +630 -411
  20. data/ext/ruby_prof/rp_method.h +70 -52
  21. data/ext/ruby_prof/rp_profile.c +895 -0
  22. data/ext/ruby_prof/rp_profile.h +37 -0
  23. data/ext/ruby_prof/rp_stack.c +196 -128
  24. data/ext/ruby_prof/rp_stack.h +56 -51
  25. data/ext/ruby_prof/rp_thread.c +337 -273
  26. data/ext/ruby_prof/rp_thread.h +36 -27
  27. data/ext/ruby_prof/ruby_prof.c +48 -671
  28. data/ext/ruby_prof/ruby_prof.h +17 -56
  29. data/ext/ruby_prof/vc/ruby_prof.sln +20 -21
  30. data/ext/ruby_prof/vc/{ruby_prof_20.vcxproj → ruby_prof.vcxproj} +38 -5
  31. data/lib/ruby-prof.rb +52 -58
  32. data/lib/ruby-prof/assets/call_stack_printer.html.erb +713 -0
  33. data/lib/ruby-prof/assets/call_stack_printer.png +0 -0
  34. data/lib/ruby-prof/assets/graph_printer.html.erb +356 -0
  35. data/lib/ruby-prof/call_info.rb +57 -126
  36. data/lib/ruby-prof/call_info_visitor.rb +38 -40
  37. data/lib/ruby-prof/compatibility.rb +109 -178
  38. data/lib/ruby-prof/exclude_common_methods.rb +198 -0
  39. data/lib/ruby-prof/measurement.rb +14 -0
  40. data/lib/ruby-prof/method_info.rb +90 -129
  41. data/lib/ruby-prof/printers/abstract_printer.rb +127 -85
  42. data/lib/ruby-prof/printers/call_info_printer.rb +51 -41
  43. data/lib/ruby-prof/printers/call_stack_printer.rb +182 -260
  44. data/lib/ruby-prof/printers/call_tree_printer.rb +151 -130
  45. data/lib/ruby-prof/printers/dot_printer.rb +132 -132
  46. data/lib/ruby-prof/printers/flat_printer.rb +52 -70
  47. data/lib/ruby-prof/printers/graph_html_printer.rb +63 -244
  48. data/lib/ruby-prof/printers/graph_printer.rb +114 -116
  49. data/lib/ruby-prof/printers/multi_printer.rb +127 -58
  50. data/lib/ruby-prof/profile.rb +33 -55
  51. data/lib/ruby-prof/rack.rb +171 -95
  52. data/lib/ruby-prof/task.rb +147 -147
  53. data/lib/ruby-prof/thread.rb +35 -41
  54. data/lib/ruby-prof/version.rb +3 -3
  55. data/lib/unprof.rb +10 -10
  56. data/ruby-prof.gemspec +58 -57
  57. data/test/abstract_printer_test.rb +26 -0
  58. data/test/alias_test.rb +129 -0
  59. data/test/basic_test.rb +129 -128
  60. data/test/call_info_visitor_test.rb +31 -31
  61. data/test/duplicate_names_test.rb +32 -32
  62. data/test/dynamic_method_test.rb +53 -55
  63. data/test/enumerable_test.rb +21 -21
  64. data/test/exceptions_test.rb +24 -16
  65. data/test/exclude_methods_test.rb +146 -0
  66. data/test/exclude_threads_test.rb +53 -53
  67. data/test/fiber_test.rb +73 -79
  68. data/test/gc_test.rb +96 -0
  69. data/test/line_number_test.rb +161 -71
  70. data/test/marshal_test.rb +119 -0
  71. data/test/measure_allocations.rb +30 -0
  72. data/test/measure_allocations_test.rb +385 -26
  73. data/test/measure_allocations_trace_test.rb +385 -0
  74. data/test/measure_memory_trace_test.rb +756 -0
  75. data/test/measure_process_time_test.rb +849 -63
  76. data/test/measure_times.rb +54 -0
  77. data/test/measure_wall_time_test.rb +459 -255
  78. data/test/multi_printer_test.rb +71 -83
  79. data/test/no_method_class_test.rb +15 -15
  80. data/test/parser_timings.rb +24 -0
  81. data/test/pause_resume_test.rb +166 -166
  82. data/test/prime.rb +56 -54
  83. data/test/printer_call_stack_test.rb +28 -0
  84. data/test/printer_call_tree_test.rb +31 -0
  85. data/test/printer_flat_test.rb +68 -0
  86. data/test/printer_graph_html_test.rb +60 -0
  87. data/test/printer_graph_test.rb +41 -0
  88. data/test/printers_test.rb +141 -255
  89. data/test/printing_recursive_graph_test.rb +81 -127
  90. data/test/rack_test.rb +157 -93
  91. data/test/recursive_test.rb +210 -215
  92. data/test/singleton_test.rb +38 -38
  93. data/test/stack_printer_test.rb +64 -78
  94. data/test/start_stop_test.rb +109 -112
  95. data/test/test_helper.rb +24 -264
  96. data/test/thread_test.rb +144 -187
  97. data/test/unique_call_path_test.rb +190 -202
  98. data/test/yarv_test.rb +56 -55
  99. metadata +34 -114
  100. data/doc/LICENSE.html +0 -114
  101. data/doc/README_rdoc.html +0 -603
  102. data/doc/Rack.html +0 -95
  103. data/doc/Rack/RubyProf.html +0 -226
  104. data/doc/RubyProf.html +0 -962
  105. data/doc/RubyProf/AbstractPrinter.html +0 -546
  106. data/doc/RubyProf/AggregateCallInfo.html +0 -551
  107. data/doc/RubyProf/CallInfo.html +0 -639
  108. data/doc/RubyProf/CallInfoPrinter.html +0 -120
  109. data/doc/RubyProf/CallInfoVisitor.html +0 -198
  110. data/doc/RubyProf/CallStackPrinter.html +0 -1121
  111. data/doc/RubyProf/CallTreePrinter.html +0 -641
  112. data/doc/RubyProf/Cmd.html +0 -631
  113. data/doc/RubyProf/DotPrinter.html +0 -257
  114. data/doc/RubyProf/FlatPrinter.html +0 -163
  115. data/doc/RubyProf/FlatPrinterWithLineNumbers.html +0 -208
  116. data/doc/RubyProf/GraphHtmlPrinter.html +0 -552
  117. data/doc/RubyProf/GraphPrinter.html +0 -139
  118. data/doc/RubyProf/MethodInfo.html +0 -745
  119. data/doc/RubyProf/MultiPrinter.html +0 -360
  120. data/doc/RubyProf/Profile.html +0 -763
  121. data/doc/RubyProf/ProfileTask.html +0 -490
  122. data/doc/RubyProf/Thread.html +0 -310
  123. data/doc/created.rid +0 -31
  124. data/doc/css/fonts.css +0 -167
  125. data/doc/css/rdoc.css +0 -590
  126. data/doc/examples/flat_txt.html +0 -138
  127. data/doc/examples/graph_html.html +0 -909
  128. data/doc/examples/graph_txt.html +0 -247
  129. data/doc/fonts/Lato-Light.ttf +0 -0
  130. data/doc/fonts/Lato-LightItalic.ttf +0 -0
  131. data/doc/fonts/Lato-Regular.ttf +0 -0
  132. data/doc/fonts/Lato-RegularItalic.ttf +0 -0
  133. data/doc/fonts/SourceCodePro-Bold.ttf +0 -0
  134. data/doc/fonts/SourceCodePro-Regular.ttf +0 -0
  135. data/doc/images/add.png +0 -0
  136. data/doc/images/arrow_up.png +0 -0
  137. data/doc/images/brick.png +0 -0
  138. data/doc/images/brick_link.png +0 -0
  139. data/doc/images/bug.png +0 -0
  140. data/doc/images/bullet_black.png +0 -0
  141. data/doc/images/bullet_toggle_minus.png +0 -0
  142. data/doc/images/bullet_toggle_plus.png +0 -0
  143. data/doc/images/date.png +0 -0
  144. data/doc/images/delete.png +0 -0
  145. data/doc/images/find.png +0 -0
  146. data/doc/images/loadingAnimation.gif +0 -0
  147. data/doc/images/macFFBgHack.png +0 -0
  148. data/doc/images/package.png +0 -0
  149. data/doc/images/page_green.png +0 -0
  150. data/doc/images/page_white_text.png +0 -0
  151. data/doc/images/page_white_width.png +0 -0
  152. data/doc/images/plugin.png +0 -0
  153. data/doc/images/ruby.png +0 -0
  154. data/doc/images/tag_blue.png +0 -0
  155. data/doc/images/tag_green.png +0 -0
  156. data/doc/images/transparent.png +0 -0
  157. data/doc/images/wrench.png +0 -0
  158. data/doc/images/wrench_orange.png +0 -0
  159. data/doc/images/zoom.png +0 -0
  160. data/doc/index.html +0 -626
  161. data/doc/js/darkfish.js +0 -161
  162. data/doc/js/jquery.js +0 -4
  163. data/doc/js/navigation.js +0 -142
  164. data/doc/js/navigation.js.gz +0 -0
  165. data/doc/js/search.js +0 -109
  166. data/doc/js/search_index.js +0 -1
  167. data/doc/js/search_index.js.gz +0 -0
  168. data/doc/js/searcher.js +0 -228
  169. data/doc/js/searcher.js.gz +0 -0
  170. data/doc/table_of_contents.html +0 -942
  171. data/examples/cachegrind.out.1 +0 -114
  172. data/examples/cachegrind.out.1.32313213 +0 -114
  173. data/examples/flat.txt +0 -50
  174. data/examples/graph.dot +0 -84
  175. data/examples/graph.html +0 -823
  176. data/examples/graph.txt +0 -139
  177. data/examples/multi.flat.txt +0 -23
  178. data/examples/multi.graph.html +0 -760
  179. data/examples/multi.grind.dat +0 -114
  180. data/examples/multi.stack.html +0 -547
  181. data/examples/stack.html +0 -547
  182. data/ext/ruby_prof/rp_measure.c +0 -40
  183. data/ext/ruby_prof/rp_measure.h +0 -45
  184. data/ext/ruby_prof/rp_measure_cpu_time.c +0 -136
  185. data/ext/ruby_prof/rp_measure_gc_runs.c +0 -73
  186. data/ext/ruby_prof/rp_measure_gc_time.c +0 -60
  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/lib/ruby-prof/aggregate_call_info.rb +0 -76
  190. data/lib/ruby-prof/assets/call_stack_printer.css.html +0 -117
  191. data/lib/ruby-prof/assets/call_stack_printer.js.html +0 -385
  192. data/lib/ruby-prof/printers/flat_printer_with_line_numbers.rb +0 -64
  193. data/test/aggregate_test.rb +0 -136
  194. data/test/block_test.rb +0 -74
  195. data/test/call_info_test.rb +0 -78
  196. data/test/issue137_test.rb +0 -63
  197. data/test/measure_cpu_time_test.rb +0 -213
  198. data/test/measure_gc_runs_test.rb +0 -32
  199. data/test/measure_gc_time_test.rb +0 -36
  200. data/test/measure_memory_test.rb +0 -33
  201. data/test/method_elimination_test.rb +0 -84
  202. data/test/module_test.rb +0 -45
  203. data/test/stack_test.rb +0 -138
data/Rakefile CHANGED
@@ -1,113 +1,110 @@
1
- # encoding: utf-8
2
-
3
- require "rubygems/package_task"
4
- require "rake/extensiontask"
5
- require "rake/testtask"
6
- require "rdoc/task"
7
- require "date"
8
- require "rake/clean"
9
- begin
10
- require "bundler/setup"
11
- Bundler::GemHelper.install_tasks
12
- [:build, :install, :release].each {|t| Rake::Task[t].enhance [:rdoc] }
13
- rescue LoadError
14
- $stderr.puts "Install bundler to get support for simplified gem publishing"
15
- end
16
-
17
- # To release a version of ruby-prof:
18
- # * Update lib/ruby-prof/version.rb
19
- # * Update CHANGES
20
- # * git commit to commit files
21
- # * rake clobber to remove extra files
22
- # * rake compile to build windows gems
23
- # * rake package to create the gems
24
- # * Tag the release (git tag 0.10.1)
25
- # * Push to ruybgems.org (gem push pkg/<gem files>)
26
- # For a ruby only release, just run
27
- # * rake release
28
- # it will push changes to github, tag the release, build the package and upload it to rubygems.org
29
- # and in case you forgot to increment the version number or have uncommitted changes, it will refuse to work
30
-
31
- GEM_NAME = 'ruby-prof'
32
- SO_NAME = 'ruby_prof'
33
-
34
- default_spec = Gem::Specification.load("#{GEM_NAME}.gemspec")
35
-
36
- # specify which versions/builds to cross compile
37
- Rake::ExtensionTask.new do |ext|
38
- ext.gem_spec = default_spec
39
- ext.name = SO_NAME
40
- ext.ext_dir = "ext/#{SO_NAME}"
41
- ext.lib_dir = "lib/#{RUBY_VERSION}"
42
- ext.cross_compile = true
43
- ext.cross_platform = ['x86-mswin32-60', 'x86-mingw32-60']
44
- end
45
-
46
- # Rake task to build the default package
47
- Gem::PackageTask.new(default_spec) do |pkg|
48
- pkg.need_tar = true
49
- end
50
-
51
- # make sure rdoc has been built when packaging
52
- # why do we ship rdoc as part of the gem?
53
- Rake::Task[:package].enhance [:rdoc]
54
-
55
- # Setup Windows Gem
56
- if RUBY_PLATFORM.match(/win32|mingw32/)
57
- # Windows specification
58
- win_spec = default_spec.clone
59
- win_spec.platform = Gem::Platform::CURRENT
60
- win_spec.files += Dir.glob('lib/**/*.so')
61
- win_spec.instance_variable_set(:@cache_file, nil) # Hack to work around gem issue
62
-
63
- # Unset extensions
64
- win_spec.extensions = nil
65
-
66
- # Rake task to build the windows package
67
- Gem::PackageTask.new(win_spec) do |pkg|
68
- pkg.need_tar = false
69
- end
70
- end
71
-
72
- # --------- RDoc Documentation ------
73
- desc "Generate rdoc documentation"
74
- RDoc::Task.new("rdoc") do |rdoc|
75
- rdoc.rdoc_dir = 'doc'
76
- rdoc.title = "ruby-prof"
77
- # Show source inline with line numbers
78
- rdoc.options << "--line-numbers"
79
- # Make the readme file the start page for the generated html
80
- rdoc.options << '--main' << 'README.rdoc'
81
- rdoc.rdoc_files.include('bin/*',
82
- 'doc/*.rdoc',
83
- 'examples/flat.txt',
84
- 'examples/graph.txt',
85
- 'examples/graph.html',
86
- 'lib/**/*.rb',
87
- 'ext/ruby_prof/ruby_prof.c',
88
- 'ext/ruby_prof/measure_*.h',
89
- 'README.rdoc',
90
- 'LICENSE')
91
- end
92
-
93
- task :default => :test
94
-
95
- for file in Dir['lib/**/*.{o,so,bundle}']
96
- CLEAN.include file
97
- end
98
- for file in Dir['doc/**/*.{txt,dat,png,html}']
99
- CLEAN.include file
100
- end
101
- CLEAN.reject!{|f| !File.exist?(f)}
102
- task :clean do
103
- # remove tmp dir contents completely after cleaning
104
- FileUtils.rm_rf('tmp/*')
105
- end
106
-
107
- desc 'Run the ruby-prof test suite'
108
- Rake::TestTask.new do |t|
109
- t.libs += %w(lib ext test)
110
- t.test_files = Dir['test/**_test.rb']
111
- t.verbose = true
112
- t.warning = true
113
- end
1
+ # encoding: utf-8
2
+
3
+ require "rubygems/package_task"
4
+ require "rake/extensiontask"
5
+ require "rake/testtask"
6
+ require "rdoc/task"
7
+ require "date"
8
+ require "rake/clean"
9
+ begin
10
+ require "bundler/setup"
11
+ Bundler::GemHelper.install_tasks
12
+ [:build, :install, :release].each {|t| Rake::Task[t].enhance [:rdoc] }
13
+ rescue LoadError
14
+ $stderr.puts "Install bundler to get support for simplified gem publishing"
15
+ end
16
+
17
+ # To release a version of ruby-prof:
18
+ # * Update lib/ruby-prof/version.rb
19
+ # * Update CHANGES
20
+ # * git commit to commit files
21
+ # * rake clobber to remove extra files
22
+ # * rake compile to build windows gems
23
+ # * rake package to create the gems
24
+ # * Tag the release (git tag 0.10.1)
25
+ # * Push to ruybgems.org (gem push pkg/<gem files>)
26
+ # For a ruby only release, just run
27
+ # * rake release
28
+ # it will push changes to github, tag the release, build the package and upload it to rubygems.org
29
+ # and in case you forgot to increment the version number or have uncommitted changes, it will refuse to work
30
+
31
+ GEM_NAME = 'ruby-prof'
32
+ SO_NAME = 'ruby_prof'
33
+
34
+ default_spec = Gem::Specification.load("#{GEM_NAME}.gemspec")
35
+
36
+ # specify which versions/builds to cross compile
37
+ Rake::ExtensionTask.new do |ext|
38
+ ext.gem_spec = default_spec
39
+ ext.name = SO_NAME
40
+ ext.ext_dir = "ext/#{SO_NAME}"
41
+ ext.lib_dir = "lib/#{RUBY_VERSION}"
42
+ ext.cross_compile = true
43
+ ext.cross_platform = ['x86-mingw32', 'x64-mingw32']
44
+ end
45
+
46
+ # Rake task to build the default package
47
+ Gem::PackageTask.new(default_spec) do |pkg|
48
+ pkg.need_tar = true
49
+ end
50
+
51
+ # make sure rdoc has been built when packaging
52
+ # why do we ship rdoc as part of the gem?
53
+ Rake::Task[:package].enhance [:rdoc]
54
+
55
+ # Setup Windows Gem
56
+ if RUBY_PLATFORM.match(/win32|mingw32/)
57
+ # Windows specification
58
+ win_spec = default_spec.clone
59
+ win_spec.platform = Gem::Platform::CURRENT
60
+ win_spec.files += Dir.glob('lib/**/*.so')
61
+ win_spec.instance_variable_set(:@cache_file, nil) # Hack to work around gem issue
62
+
63
+ # Unset extensions
64
+ win_spec.extensions = nil
65
+
66
+ # Rake task to build the windows package
67
+ Gem::PackageTask.new(win_spec) do |pkg|
68
+ pkg.need_tar = false
69
+ end
70
+ end
71
+
72
+ # --------- RDoc Documentation ------
73
+ desc "Generate rdoc documentation"
74
+ RDoc::Task.new("rdoc") do |rdoc|
75
+ rdoc.rdoc_dir = 'doc'
76
+ rdoc.title = "ruby-prof"
77
+ # Show source inline with line numbers
78
+ rdoc.options << "--line-numbers"
79
+ # Make the readme file the start page for the generated html
80
+ rdoc.options << '--main' << 'README.rdoc'
81
+ rdoc.rdoc_files.include('bin/*',
82
+ 'doc/*.rdoc',
83
+ 'lib/**/*.rb',
84
+ 'ext/ruby_prof/*.c',
85
+ 'ext/ruby_prof/*.h',
86
+ 'README.rdoc',
87
+ 'LICENSE')
88
+ end
89
+
90
+ task :default => :test
91
+
92
+ for file in Dir['lib/**/*.{o,so,bundle}']
93
+ CLEAN.include file
94
+ end
95
+ for file in Dir['doc/**/*.{txt,dat,png,html}']
96
+ CLEAN.include file
97
+ end
98
+ CLEAN.reject!{|f| !File.exist?(f)}
99
+ task :clean do
100
+ # remove tmp dir contents completely after cleaning
101
+ FileUtils.rm_rf('tmp/*')
102
+ end
103
+
104
+ desc 'Run the ruby-prof test suite'
105
+ Rake::TestTask.new do |t|
106
+ t.libs += %w(lib ext test)
107
+ t.test_files = Dir['test/**_test.rb']
108
+ t.verbose = true
109
+ t.warning = true
110
+ end
@@ -1,340 +1,380 @@
1
- #! /usr/bin/env ruby
2
-
3
- # == Synopsis
4
- #
5
- # Profiles a Ruby program.
6
- #
7
- # == Usage
8
- #
9
- # ruby_prof [options] <script.rb> [--] [script-options]"
10
- #
11
- # Various options:
12
- # run "$ ruby-prof --help" to see them
13
- #
14
- # See also the readme "reports" section for the various outputs
15
-
16
- # First require ruby-prof
17
- require 'rubygems'
18
- require 'ruby-prof'
19
-
20
- # Now setup option parser
21
- require 'ostruct'
22
- require 'optparse'
23
-
24
- module RubyProf
25
- class Cmd
26
- attr_accessor :options
27
-
28
- def initialize
29
- setup_options
30
- parse_args
31
-
32
- load_pre_libs
33
- load_pre_execs
34
- end
35
-
36
- def setup_options
37
- @options = OpenStruct.new
38
- options.printer = RubyProf::FlatPrinter
39
- options.min_percent = 0
40
- options.file = nil
41
- options.replace_prog_name = false
42
- options.specialized_instruction = false
43
-
44
- options.pre_libs = Array.new
45
- options.pre_execs = Array.new
46
- end
47
-
48
- def option_parser
49
- OptionParser.new do |opts|
50
- opts.banner = "ruby_prof #{RubyProf::VERSION}\n" +
51
- "Usage: ruby-prof [options] <script.rb> [--] [profiled-script-command-line-options]"
52
-
53
- opts.separator ""
54
- opts.separator "Options:"
55
-
56
- opts.on('-p printer', '--printer=printer', [:flat, :flat_with_line_numbers, :graph, :graph_html, :call_tree, :call_stack, :dot, :multi],
57
- 'Select a printer:',
58
- ' flat - Prints a flat profile as text (default).',
59
- ' flat_with_line_numbers - same as flat, with line numbers.',
60
- ' graph - Prints a graph profile as text.',
61
- ' graph_html - Prints a graph profile as html.',
62
- ' call_tree - format for KCacheGrind',
63
- ' call_stack - prints a HTML visualization of the call tree',
64
- ' dot - Prints a graph profile as a dot file',
65
- ' multi - Creates several reports in output directory'
66
- ) do |printer|
67
-
68
-
69
- case printer
70
- when :flat
71
- options.printer = RubyProf::FlatPrinter
72
- when :flat_with_line_numbers
73
- options.printer = RubyProf::FlatPrinterWithLineNumbers
74
- when :graph
75
- options.printer = RubyProf::GraphPrinter
76
- when :graph_html
77
- options.printer = RubyProf::GraphHtmlPrinter
78
- when :call_tree
79
- options.printer = RubyProf::CallTreePrinter
80
- when :call_stack
81
- options.printer = RubyProf::CallStackPrinter
82
- when :dot
83
- options.printer = RubyProf::DotPrinter
84
- when :multi
85
- options.printer = RubyProf::MultiPrinter
86
- end
87
- end
88
-
89
- opts.on('-m min_percent', '--min_percent=min_percent', Float,
90
- 'The minimum percent a method must take before ',
91
- ' being included in output reports.',
92
- ' this option is not supported for call tree.') do |min_percent|
93
- options.min_percent = min_percent
94
- end
95
-
96
- opts.on('-f path', '--file=path',
97
- 'Output results to a file instead of standard out.') do |file|
98
- options.file = file
99
- options.old_wd = Dir.pwd
100
- end
101
-
102
- opts.on('--mode=measure_mode',
103
- [:process, :wall, :cpu, :allocations, :memory, :gc_runs, :gc_time],
104
- 'Select what ruby-prof should measure:',
105
- ' wall - Wall time (default).',
106
- ' process - Process time.',
107
- ' cpu - CPU time (Pentium and PowerPCs only).',
108
- ' allocations - Object allocations (requires patched Ruby interpreter).',
109
- ' memory - Allocated memory in KB (requires patched Ruby interpreter).',
110
- ' gc_runs - Number of garbage collections (requires patched Ruby interpreter).',
111
- ' gc_time - Time spent in garbage collection (requires patched Ruby interpreter).') do |measure_mode|
112
-
113
- case measure_mode
114
- when :wall
115
- options.measure_mode = RubyProf::WALL_TIME
116
- when :process
117
- options.measure_mode = RubyProf::PROCESS_TIME
118
- when :cpu
119
- options.measure_mode = RubyProf::CPU_TIME
120
- when :allocations
121
- options.measure_mode = RubyProf::ALLOCATIONS
122
- when :memory
123
- options.measure_mode = RubyProf::MEMORY
124
- when :gc_runs
125
- options.measure_mode = RubyProf::GC_RUNS
126
- when :gc_time
127
- options.measure_mode = RubyProf::GC_TIME
128
- end
129
- end
130
-
131
- opts.on('-s sort_mode', '--sort=sort_mode', [:total, :self, :wait, :child],
132
- 'Select how ruby-prof results should be sorted:',
133
- ' total - Total time',
134
- ' self - Self time',
135
- ' wait - Wait time',
136
- ' child - Child time') do |sort_mode|
137
-
138
- options.sort_method = case sort_mode
139
- when :total
140
- :total_time
141
- when :self
142
- :self_time
143
- when :wait
144
- :wait_time
145
- when :child
146
- :children_time
147
- end
148
- end
149
-
150
- opts.on("--replace-progname", "Replace $0 when loading the .rb files.") do
151
- options.replace_prog_name = true
152
- end
153
-
154
- if defined?(RubyVM)
155
- opts.on("--specialized-instruction", "Turn on specified instruction.") do
156
- options.specialized_instruction = true
157
- end
158
- end
159
-
160
- opts.on_tail("-h", "--help", "Show help message") do
161
- puts opts
162
- exit
163
- end
164
-
165
- opts.on_tail("--version", "Show version #{RubyProf::VERSION}") do
166
- puts "ruby_prof " + RubyProf::VERSION
167
- exit
168
- end
169
-
170
- opts.on("-v","Show version, set $VERBOSE to true, profile script if option given") do
171
- puts "ruby version: " + [RUBY_PATCHLEVEL, RUBY_PLATFORM, RUBY_VERSION].join(' ')
172
- $VERBOSE = true
173
- end
174
-
175
- opts.on("-d", "Set $DEBUG to true") do
176
- $DEBUG = true
177
- end
178
-
179
- opts.on('-R lib', '--require-noprof lib', 'require a specific library (not profiled)') do |lib|
180
- options.pre_libs << lib
181
- end
182
-
183
- opts.on('-E code', '--eval-noprof code', 'execute the ruby statements (not profiled)') do |code|
184
- options.pre_execs << code
185
- end
186
-
187
- opts.on('-x regexp', '--exclude regexp', 'exclude methods by regexp (see method elimination)') do|meth|
188
- options.eliminate_methods ||= []
189
- options.eliminate_methods << Regexp.new(meth)
190
- end
191
-
192
- opts.on('-X file', '--exclude-file file', 'exclude methods by regexp listed in file (see method elimination)') do|file|
193
- options.eliminate_methods_files ||= []
194
- options.eliminate_methods_files << file
195
- end
196
-
197
- opts.on('--exclude-common-cycles', 'make common iterators like Integer#times appear inlined') do |meth|
198
- options.eliminate_methods ||= []
199
- options.eliminate_methods += %w{
200
- Integer#times
201
- Integer#upto
202
- Integer#downto
203
- Enumerator#each
204
- Enumerator#each_with_index
205
- Enumerator#each_with_object
206
-
207
- Array#each
208
- Array#each_index
209
- Array#reverse_each
210
- Array#map
211
-
212
- Hash#each
213
- Hash#each_pair
214
- Hash#each_key
215
- Hash#each_value
216
-
217
- Range#each
218
- Enumerable#each_cons
219
- Enumerable#each_entry
220
- Enumerable#each_slice
221
- Enumerable#each_with_index
222
- Enumerable#each_with_object
223
- Enumerable#reverse_each
224
- Enumerable#inject
225
- Enumerable#collect
226
- Enumerable#reduce
227
- }
228
- #TODO: may be the whole Enumerable module should be excluded via 'Enumerable#.*', we need feedback on use cases.
229
- end
230
-
231
- opts.on('--exclude-common-callbacks', 'make common callbacks invocations like Integer#times appear inlined so you can see call origins in graph') do|meth|
232
- options.eliminate_methods ||= []
233
- options.eliminate_methods += %w{
234
- Method#call
235
- Proc#call
236
- ActiveSupport::Callbacks::ClassMethods#__run_callback
237
- }
238
- end
239
- end
240
- end
241
-
242
- def parse_args
243
- # Make sure the user specified at least one file
244
- if ARGV.length < 1 and not options.exec
245
- puts self.option_parser
246
- puts ""
247
- puts "Must specify a script to run"
248
- exit(-1)
249
- end
250
-
251
- self.option_parser.parse! ARGV
252
-
253
- if options.printer == RubyProf::MultiPrinter
254
- options.file ||= "."
255
- options.old_wd ||= Dir.pwd
256
- end
257
- rescue OptionParser::InvalidOption, OptionParser::InvalidArgument, OptionParser::MissingArgument => e
258
- puts self.option_parser
259
- puts e.message
260
- exit(-1)
261
- end
262
-
263
- def load_pre_libs
264
- options.pre_libs.each do |lib|
265
- require lib
266
- end
267
- end
268
-
269
- def load_pre_execs
270
- options.pre_execs.each do |exec|
271
- eval(exec)
272
- end
273
- end
274
-
275
- def run
276
- # Get the script we will execute
277
- script = ARGV.shift
278
- if options.replace_prog_name
279
- $0 = File.expand_path(script)
280
- end
281
-
282
- # Set VM compile option
283
- if defined?(RubyVM)
284
- RubyVM::InstructionSequence.compile_option = {
285
- :trace_instruction => true,
286
- :specialized_instruction => options.specialized_instruction
287
- }
288
- end
289
-
290
- # Set the measure mode
291
- RubyProf.measure_mode = options.measure_mode if options.measure_mode
292
- RubyProf.start_script(script)
293
- end
294
- end
295
- end
296
-
297
- # Parse command line options
298
- cmd = RubyProf::Cmd.new
299
-
300
- # Install at_exit handler. It is important that we do this
301
- # before loading the scripts so our at_exit handler run
302
- # *after* any other one that will be installed.
303
-
304
- at_exit {
305
- # Stop profiling
306
- result = RubyProf.stop
307
-
308
- # Eliminate unwanted methods from call graph
309
- if cmd.options.eliminate_methods
310
- result.eliminate_methods!(cmd.options.eliminate_methods)
311
- end
312
-
313
- if cmd.options.eliminate_methods_files
314
- cmd.options.eliminate_methods_files.each {|f| result.eliminate_methods!(f)}
315
- end
316
-
317
- # Create a printer
318
- printer = cmd.options.printer.new(result)
319
- printer_options = {:min_percent => cmd.options.min_percent, :sort_method => cmd.options.sort_method}
320
-
321
- # Get output
322
- if cmd.options.file
323
- # write it relative to the dir they *started* in, as it's a bit surprising to write it in the dir they end up in.
324
- Dir.chdir(cmd.options.old_wd) do
325
- if printer.is_a?(RubyProf::MultiPrinter)
326
- printer.print(printer_options.merge(:path => cmd.options.file))
327
- else
328
- File.open(cmd.options.file, 'w') do |file|
329
- printer.print(file, printer_options)
330
- end
331
- end
332
- end
333
- else
334
- # Print out results
335
- printer.print(STDOUT, printer_options)
336
- end
337
- }
338
-
339
- # Now profile some code
340
- cmd.run
1
+ #! /usr/bin/env ruby
2
+
3
+ # First require ruby-prof
4
+ require 'rubygems'
5
+ require 'ruby-prof'
6
+
7
+ # Now setup option parser
8
+ require 'ostruct'
9
+ require 'optparse'
10
+
11
+ module RubyProf
12
+ # == Synopsis
13
+ #
14
+ # Profiles a Ruby program.
15
+ #
16
+ # == Usage
17
+ #
18
+ # ruby_prof [options] <script.rb> [--] [script-options]
19
+ #
20
+ # Options:
21
+ # -p, --printer=printer Select a printer:
22
+ # flat - Prints a flat profile as text (default).
23
+ # graph - Prints a graph profile as text.
24
+ # graph_html - Prints a graph profile as html.
25
+ # call_tree - format for KCacheGrind
26
+ # call_stack - prints a HTML visualization of the call tree
27
+ # dot - Prints a graph profile as a dot file
28
+ # multi - Creates several reports in output directory
29
+ #
30
+ # -m, --min_percent=min_percent The minimum percent a method must take before
31
+ # being included in output reports. This option is not supported for call tree reports.
32
+ #
33
+ # -f, --file=path Output results to a file instead of standard out.
34
+ #
35
+ # --mode=measure_mode Select what ruby-prof should measure:
36
+ # wall - Wall time (default)
37
+ # process - Process time
38
+ # allocations - Object allocations
39
+ # memory - Allocated memory
40
+ #
41
+ # -s, --sort=sort_mode Select how ruby-prof results should be sorted:
42
+ # total - Total time
43
+ # self - Self time
44
+ # wait - Wait time
45
+ # child - Child time
46
+ #
47
+ # --replace-progname Replace $0 when loading the .rb files.
48
+ #
49
+ # --specialized-instruction Turn on specified instruction.
50
+ #
51
+ # -v Show version, set $VERBOSE to true, profile script if option given
52
+ #
53
+ # -d Set $DEBUG to true
54
+ #
55
+ # -R, --require-noprof lib Require a specific library (not profiled)
56
+ #
57
+ # -E, --eval-noprof code Execute the ruby statements (not profiled)
58
+ #
59
+ # -x, --exclude regexp Exclude methods by regexp (see method elimination)
60
+ #
61
+ # -X, --exclude-file file Exclude methods by regexp listed in file (see method elimination)
62
+ #
63
+ # --exclude-common-cycles Make common iterators like Integer#times appear inlined
64
+ #
65
+ # --exclude-common-callbacks Make common callbacks invocations like Integer#times appear inlined so you can see call origins in graph
66
+ #
67
+ # -h, --help Show help message
68
+ #
69
+ # --version Show version
70
+ #
71
+ #
72
+ class Cmd
73
+ # :enddoc:
74
+ attr_accessor :options
75
+
76
+ def initialize
77
+ setup_options
78
+ parse_args
79
+
80
+ load_pre_libs
81
+ load_pre_execs
82
+ end
83
+
84
+ def setup_options
85
+ @options = OpenStruct.new
86
+ options.printer = RubyProf::FlatPrinter
87
+ options.min_percent = 0
88
+ options.file = nil
89
+ options.replace_prog_name = false
90
+ options.specialized_instruction = false
91
+
92
+ options.pre_libs = Array.new
93
+ options.pre_execs = Array.new
94
+ end
95
+
96
+ def option_parser
97
+ OptionParser.new do |opts|
98
+ opts.banner = "ruby_prof #{RubyProf::VERSION}\n" +
99
+ "Usage: ruby-prof [options] <script.rb> [--] [profiled-script-command-line-options]"
100
+
101
+ opts.separator ""
102
+ opts.separator "Options:"
103
+
104
+ opts.on('-p printer', '--printer=printer', [:flat, :flat_with_line_numbers, :graph, :graph_html, :call_tree, :call_stack, :dot, :multi],
105
+ 'Select a printer:',
106
+ ' flat - Prints a flat profile as text (default).',
107
+ ' graph - Prints a graph profile as text.',
108
+ ' graph_html - Prints a graph profile as html.',
109
+ ' call_tree - format for KCacheGrind',
110
+ ' call_stack - prints a HTML visualization of the call tree',
111
+ ' dot - Prints a graph profile as a dot file',
112
+ ' multi - Creates several reports in output directory'
113
+ ) do |printer|
114
+
115
+ case printer
116
+ when :flat
117
+ options.printer = RubyProf::FlatPrinter
118
+ when :graph
119
+ options.printer = RubyProf::GraphPrinter
120
+ when :graph_html
121
+ options.printer = RubyProf::GraphHtmlPrinter
122
+ when :call_tree
123
+ options.printer = RubyProf::CallTreePrinter
124
+ when :call_stack
125
+ options.printer = RubyProf::CallStackPrinter
126
+ when :dot
127
+ options.printer = RubyProf::DotPrinter
128
+ when :multi
129
+ options.printer = RubyProf::MultiPrinter
130
+ end
131
+ end
132
+
133
+ opts.on('-m min_percent', '--min_percent=min_percent', Float,
134
+ 'The minimum percent a method must take before ',
135
+ ' being included in output reports.',
136
+ ' This option is not supported for call tree.') do |min_percent|
137
+ options.min_percent = min_percent
138
+ end
139
+
140
+ opts.on('-f path', '--file=path',
141
+ 'Output results to a file instead of standard out.') do |file|
142
+ options.file = file
143
+ options.old_wd = Dir.pwd
144
+ end
145
+
146
+ opts.on('--mode=measure_mode',
147
+ [:process, :wall, :allocations, :memory],
148
+ 'Select what ruby-prof should measure:',
149
+ ' wall - Wall time (default).',
150
+ ' process - Process time.',
151
+ ' allocations - Object allocations (requires patched Ruby interpreter).',
152
+ ' memory - Allocated memory in KB (requires patched Ruby interpreter).') do |measure_mode|
153
+
154
+ case measure_mode
155
+ when :wall
156
+ options.measure_mode = RubyProf::WALL_TIME
157
+ when :process
158
+ options.measure_mode = RubyProf::PROCESS_TIME
159
+ when :allocations
160
+ options.measure_mode = RubyProf::ALLOCATIONS
161
+ when :memory
162
+ options.measure_mode = RubyProf::MEMORY
163
+ end
164
+ end
165
+
166
+ opts.on('-s sort_mode', '--sort=sort_mode', [:total, :self, :wait, :child],
167
+ 'Select how ruby-prof results should be sorted:',
168
+ ' total - Total time',
169
+ ' self - Self time',
170
+ ' wait - Wait time',
171
+ ' child - Child time') do |sort_mode|
172
+
173
+ options.sort_method = case sort_mode
174
+ when :total
175
+ :total_time
176
+ when :self
177
+ :self_time
178
+ when :wait
179
+ :wait_time
180
+ when :child
181
+ :children_time
182
+ end
183
+ end
184
+
185
+ opts.on("--replace-progname", "Replace $0 when loading the .rb files.") do
186
+ options.replace_prog_name = true
187
+ end
188
+
189
+ if defined?(RubyVM)
190
+ opts.on("--specialized-instruction", "Turn on specified instruction.") do
191
+ options.specialized_instruction = true
192
+ end
193
+ end
194
+
195
+ opts.on_tail("-h", "--help", "Show help message") do
196
+ puts opts
197
+ exit
198
+ end
199
+
200
+ opts.on_tail("--version", "Show version #{RubyProf::VERSION}") do
201
+ puts "ruby_prof " + RubyProf::VERSION
202
+ exit
203
+ end
204
+
205
+ opts.on("-v","Show version, set $VERBOSE to true, profile script if option given") do
206
+ puts "ruby version: " + [RUBY_PATCHLEVEL, RUBY_PLATFORM, RUBY_VERSION].join(' ')
207
+ $VERBOSE = true
208
+ end
209
+
210
+ opts.on("-d", "Set $DEBUG to true") do
211
+ $DEBUG = true
212
+ end
213
+
214
+ opts.on('-R lib', '--require-noprof lib', 'require a specific library (not profiled)') do |lib|
215
+ options.pre_libs << lib
216
+ end
217
+
218
+ opts.on('-E code', '--eval-noprof code', 'execute the ruby statements (not profiled)') do |code|
219
+ options.pre_execs << code
220
+ end
221
+
222
+ opts.on('-x regexp', '--exclude regexp', 'exclude methods by regexp (see method elimination)') do |meth|
223
+ options.eliminate_methods ||= []
224
+ options.eliminate_methods << Regexp.new(meth)
225
+ end
226
+
227
+ opts.on('-X file', '--exclude-file file', 'exclude methods by regexp listed in file (see method elimination)') do|file|
228
+ options.eliminate_methods_files ||= []
229
+ options.eliminate_methods_files << file
230
+ end
231
+
232
+ opts.on('--exclude-common-cycles', 'make common iterators like Integer#times appear inlined') do |meth|
233
+ options.eliminate_methods ||= []
234
+ options.eliminate_methods += %w{
235
+ Integer#times
236
+ Integer#upto
237
+ Integer#downto
238
+ Enumerator#each
239
+ Enumerator#each_with_index
240
+ Enumerator#each_with_object
241
+
242
+ Array#each
243
+ Array#each_index
244
+ Array#reverse_each
245
+ Array#map
246
+
247
+ Hash#each
248
+ Hash#each_pair
249
+ Hash#each_key
250
+ Hash#each_value
251
+
252
+ Range#each
253
+ Enumerable#each_cons
254
+ Enumerable#each_entry
255
+ Enumerable#each_slice
256
+ Enumerable#each_with_index
257
+ Enumerable#each_with_object
258
+ Enumerable#reverse_each
259
+ Enumerable#inject
260
+ Enumerable#collect
261
+ Enumerable#reduce
262
+ }
263
+ #TODO: may be the whole Enumerable module should be excluded via 'Enumerable#.*', we need feedback on use cases.
264
+ end
265
+
266
+ opts.on('--exclude-common-callbacks', 'make common callbacks invocations like Integer#times appear inlined so you can see call origins in graph') do|meth|
267
+ options.eliminate_methods ||= []
268
+ options.eliminate_methods += %w{
269
+ Method#call
270
+ Proc#call
271
+ ActiveSupport::Callbacks::ClassMethods#__run_callback
272
+ }
273
+ end
274
+ end
275
+ end
276
+
277
+ def parse_args
278
+ # Make sure the user specified at least one file
279
+ if ARGV.length < 1 and not options.exec
280
+ puts self.option_parser
281
+ puts ""
282
+ puts "Must specify a script to run"
283
+ exit(-1)
284
+ end
285
+
286
+ self.option_parser.parse! ARGV
287
+
288
+ if options.printer.needs_dir?
289
+ options.file ||= "."
290
+ options.old_wd ||= Dir.pwd
291
+ if !File.directory?(options.file)
292
+ puts "'#{options.file}' is not a directory"
293
+ puts "#{options.printer} needs an existing directory path to put profiles under."
294
+ exit(-1)
295
+ end
296
+ end
297
+ rescue OptionParser::InvalidOption, OptionParser::InvalidArgument, OptionParser::MissingArgument => e
298
+ puts self.option_parser
299
+ puts e.message
300
+ exit(-1)
301
+ end
302
+
303
+ def load_pre_libs
304
+ options.pre_libs.each do |lib|
305
+ require lib
306
+ end
307
+ end
308
+
309
+ def load_pre_execs
310
+ options.pre_execs.each do |exec|
311
+ eval(exec)
312
+ end
313
+ end
314
+
315
+ def run
316
+ # Get the script we will execute
317
+ script = ARGV.shift
318
+ if options.replace_prog_name
319
+ $0 = File.expand_path(script)
320
+ end
321
+
322
+ # Set VM compile option
323
+ if defined?(RubyVM)
324
+ RubyVM::InstructionSequence.compile_option = {
325
+ :trace_instruction => true,
326
+ :specialized_instruction => options.specialized_instruction
327
+ }
328
+ end
329
+
330
+ # Set the measure mode
331
+ RubyProf.measure_mode = options.measure_mode if options.measure_mode
332
+ RubyProf.start_script(script)
333
+ end
334
+ end
335
+ end
336
+
337
+ # Parse command line options
338
+ cmd = RubyProf::Cmd.new
339
+
340
+ # Install at_exit handler. It is important that we do this
341
+ # before loading the scripts so our at_exit handler run
342
+ # *after* any other one that will be installed.
343
+
344
+ at_exit {
345
+ # Stop profiling
346
+ result = RubyProf.stop
347
+
348
+ # Eliminate unwanted methods from call graph
349
+ if cmd.options.eliminate_methods
350
+ result.eliminate_methods!(cmd.options.eliminate_methods)
351
+ end
352
+
353
+ if cmd.options.eliminate_methods_files
354
+ cmd.options.eliminate_methods_files.each {|f| result.eliminate_methods!(f)}
355
+ end
356
+
357
+ # Create a printer
358
+ printer = cmd.options.printer.new(result)
359
+ printer_options = {:min_percent => cmd.options.min_percent, :sort_method => cmd.options.sort_method}
360
+
361
+ # Get output
362
+ if cmd.options.file
363
+ # write it relative to the dir they *started* in, as it's a bit surprising to write it in the dir they end up in.
364
+ Dir.chdir(cmd.options.old_wd) do
365
+ if printer.class.needs_dir?
366
+ printer.print(printer_options.merge(:path => cmd.options.file))
367
+ else
368
+ File.open(cmd.options.file, 'w') do |file|
369
+ printer.print(file, printer_options)
370
+ end
371
+ end
372
+ end
373
+ else
374
+ # Print out results
375
+ printer.print(STDOUT, printer_options)
376
+ end
377
+ }
378
+
379
+ # Now profile some code
380
+ cmd.run