ruby-prof 0.8.1-x86-mingw32 → 0.11.0.rc1-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (119) hide show
  1. data/CHANGES +89 -13
  2. data/LICENSE +4 -3
  3. data/{README → README.rdoc} +155 -162
  4. data/Rakefile +50 -123
  5. data/bin/ruby-prof +86 -47
  6. data/examples/empty.png +0 -0
  7. data/examples/graph.dot +106 -0
  8. data/examples/graph.png +0 -0
  9. data/examples/minus.png +0 -0
  10. data/examples/multi.flat.txt +23 -0
  11. data/examples/multi.graph.html +906 -0
  12. data/examples/multi.grind.dat +194 -0
  13. data/examples/multi.stack.html +573 -0
  14. data/examples/plus.png +0 -0
  15. data/examples/stack.html +573 -0
  16. data/ext/ruby_prof/extconf.rb +53 -0
  17. data/ext/ruby_prof/rp_call_info.c +369 -0
  18. data/ext/ruby_prof/rp_call_info.h +46 -0
  19. data/ext/ruby_prof/rp_measure.c +48 -0
  20. data/ext/ruby_prof/rp_measure.h +45 -0
  21. data/ext/ruby_prof/rp_measure_allocations.c +86 -0
  22. data/ext/ruby_prof/rp_measure_cpu_time.c +112 -0
  23. data/ext/ruby_prof/rp_measure_gc_runs.c +87 -0
  24. data/ext/ruby_prof/rp_measure_gc_time.c +73 -0
  25. data/ext/ruby_prof/rp_measure_memory.c +81 -0
  26. data/ext/ruby_prof/rp_measure_process_time.c +71 -0
  27. data/ext/ruby_prof/rp_measure_wall_time.c +42 -0
  28. data/ext/ruby_prof/rp_method.c +363 -0
  29. data/ext/ruby_prof/rp_method.h +55 -0
  30. data/ext/ruby_prof/rp_stack.c +61 -0
  31. data/ext/ruby_prof/rp_stack.h +40 -0
  32. data/ext/ruby_prof/rp_thread.c +113 -0
  33. data/ext/ruby_prof/rp_thread.h +20 -0
  34. data/ext/ruby_prof/ruby_prof.c +332 -1377
  35. data/ext/ruby_prof/ruby_prof.h +54 -188
  36. data/ext/ruby_prof/version.h +6 -3
  37. data/lib/1.8/ruby_prof.so +0 -0
  38. data/lib/1.9/ruby_prof.exp +0 -0
  39. data/lib/1.9/ruby_prof.ilk +0 -0
  40. data/lib/1.9/ruby_prof.lib +0 -0
  41. data/lib/1.9/ruby_prof.pdb +0 -0
  42. data/lib/1.9/ruby_prof.so +0 -0
  43. data/lib/ruby-prof.rb +32 -18
  44. data/lib/ruby-prof/abstract_printer.rb +15 -5
  45. data/lib/ruby-prof/aggregate_call_info.rb +11 -3
  46. data/lib/ruby-prof/call_info.rb +68 -1
  47. data/lib/ruby-prof/call_stack_printer.rb +775 -0
  48. data/lib/ruby-prof/call_tree_printer.rb +17 -9
  49. data/lib/ruby-prof/compatibility.rb +134 -0
  50. data/lib/ruby-prof/dot_printer.rb +152 -0
  51. data/lib/ruby-prof/empty.png +0 -0
  52. data/lib/ruby-prof/flat_printer.rb +23 -24
  53. data/lib/ruby-prof/flat_printer_with_line_numbers.rb +17 -21
  54. data/lib/ruby-prof/graph_html_printer.rb +69 -39
  55. data/lib/ruby-prof/graph_printer.rb +35 -35
  56. data/lib/ruby-prof/method_info.rb +26 -4
  57. data/lib/ruby-prof/minus.png +0 -0
  58. data/lib/ruby-prof/multi_printer.rb +56 -0
  59. data/lib/ruby-prof/plus.png +0 -0
  60. data/lib/ruby-prof/profile.rb +72 -0
  61. data/lib/ruby-prof/rack.rb +31 -0
  62. data/lib/ruby-prof/symbol_to_proc.rb +3 -1
  63. data/lib/ruby-prof/task.rb +20 -19
  64. data/lib/ruby-prof/test.rb +5 -3
  65. data/lib/ruby_prof.exp +0 -0
  66. data/lib/ruby_prof.ilk +0 -0
  67. data/lib/ruby_prof.lib +0 -0
  68. data/lib/ruby_prof.pdb +0 -0
  69. data/lib/ruby_prof.so +0 -0
  70. data/lib/unprof.rb +2 -0
  71. data/test/aggregate_test.rb +29 -14
  72. data/test/basic_test.rb +3 -251
  73. data/test/bug_test.rb +6 -0
  74. data/test/duplicate_names_test.rb +4 -4
  75. data/test/dynamic_method_test.rb +61 -0
  76. data/test/enumerable_test.rb +4 -4
  77. data/test/exceptions_test.rb +6 -5
  78. data/test/exclude_threads_test.rb +47 -47
  79. data/test/exec_test.rb +5 -5
  80. data/test/line_number_test.rb +16 -16
  81. data/test/measure_allocations_test.rb +25 -0
  82. data/test/measure_cpu_time_test.rb +212 -0
  83. data/test/measure_gc_runs_test.rb +29 -0
  84. data/test/measure_gc_time_test.rb +29 -0
  85. data/test/measure_memory_test.rb +36 -0
  86. data/test/measure_process_time_test.rb +205 -0
  87. data/test/measure_wall_time_test.rb +209 -0
  88. data/test/method_elimination_test.rb +74 -0
  89. data/test/module_test.rb +12 -21
  90. data/test/multi_printer_test.rb +81 -0
  91. data/test/no_method_class_test.rb +5 -3
  92. data/test/prime.rb +7 -10
  93. data/test/prime_test.rb +3 -3
  94. data/test/printers_test.rb +180 -54
  95. data/test/recursive_test.rb +34 -72
  96. data/test/singleton_test.rb +5 -4
  97. data/test/stack_printer_test.rb +73 -0
  98. data/test/stack_test.rb +7 -7
  99. data/test/start_stop_test.rb +23 -6
  100. data/test/test_helper.rb +81 -0
  101. data/test/test_suite.rb +35 -21
  102. data/test/thread_test.rb +40 -39
  103. data/test/unique_call_path_test.rb +6 -6
  104. metadata +106 -51
  105. data/ext/ruby_prof/measure_allocations.h +0 -58
  106. data/ext/ruby_prof/measure_cpu_time.h +0 -152
  107. data/ext/ruby_prof/measure_gc_runs.h +0 -76
  108. data/ext/ruby_prof/measure_gc_time.h +0 -57
  109. data/ext/ruby_prof/measure_memory.h +0 -101
  110. data/ext/ruby_prof/measure_process_time.h +0 -52
  111. data/ext/ruby_prof/measure_wall_time.h +0 -53
  112. data/ext/ruby_prof/mingw/Rakefile +0 -23
  113. data/ext/ruby_prof/mingw/build.rake +0 -38
  114. data/rails/environment/profile.rb +0 -24
  115. data/rails/example/example_test.rb +0 -9
  116. data/rails/profile_test_helper.rb +0 -21
  117. data/test/current_failures_windows +0 -8
  118. data/test/measurement_test.rb +0 -121
  119. data/test/ruby-prof-bin +0 -20
data/CHANGES CHANGED
@@ -1,3 +1,79 @@
1
+ 0.11.0.rc1 (2012-03-24)
2
+ ======================
3
+ * On Windows, use QueryPerformanceCounter and QueryPerformanceFrequency to measure CPU time instead
4
+ of rdtsc. This change is based on Microsoft's recommendation (Charlie Savage).
5
+ * On Windows use GetProcessTimes to return real PROCESS_TIME times instead of wall times (Charlie Savage).
6
+ * Big internal refactoring of C code to make RubyProf easier to understand and extend (Charlie Savage).
7
+ * Profile results are now returned as instances of a new class RubyProf::Profile. The old api
8
+ is supported via a compatability layer that at some point will be deprecated. (Charlie Savage).
9
+ * Split out tests for cpu_time, process_time and wall_time into separate files (Charlie Savage).
10
+ * Dropped support for Ruby 1.8.4 and 1.8.6 - does anybody still use these? (Charlie Savage).
11
+ * Added support for sorting results by total, self, wait and child times (Jan Suchal)
12
+ * Added tests for sorting behaviour & removed options from constructor to print method (Jan Suchal)
13
+ * Fix line number tests due to changes at top of file (Charlie Savage).
14
+ * Add encoding statement to top of all files for Ruby 1.9.x compatability (Charlie Savage).
15
+ * Add def file for VC to avoid the whole declspec annoyance (Charlie Savage).
16
+ * Update test suite to ensure current working directory is correct (Charlie Savage).
17
+ * Modernize gem file and remove various custom/home grown solutions that aren't needed anymore (Charlie Savage).
18
+ * Remove custom mingw build scripts, use rake compiler instead (Charlie Savage).
19
+ * Fixes for compiling with VC 2010 (Charlie Savage).
20
+
21
+ 0.10.8 (2011-07-06)
22
+ ======================
23
+ * 1.9.3 super class (Roger Pack)
24
+
25
+ 0.10.7 (2011-05-09)
26
+ ======================
27
+ * Fix a bug with REE's GC stats. Issue #53 [thanks graaff]
28
+
29
+ 0.10.6 (2011-04-29)
30
+ ======================
31
+ * Slightly more normalized url for linux/windows links to files.
32
+
33
+ 0.10.5 (2011-04-20)
34
+ =======================
35
+ * 1.8.6 compat for -v command (bug fix)
36
+
37
+ 0.10.4 (2011-04-20)
38
+ =======================
39
+ * Faster load time for ruby-prof itself.
40
+
41
+ 0.10.3
42
+ =======================
43
+ * Can cleanly load before rubygems now.
44
+
45
+ 0.10.2
46
+ =======================
47
+ * Fix for 1.9.2, os x for latest commits (thanks skaes!)
48
+
49
+ 0.10.1
50
+ =======================
51
+ * Fix bug in linux wall time, also load with only relative paths so that you can use it to benchmark rubygems startup overhead,
52
+ itself.
53
+
54
+ 0.10.0
55
+ =======================
56
+ * Some rdoc changes, for linux wall time attempt to use higher granularity (thanks to all the contributors for this round!)
57
+
58
+ 0.9.2
59
+ =======================
60
+ * Make graphviz work on 1.8.6
61
+ * Roll back some 1.9.2 optimizations until I can figure out what caused them.
62
+
63
+ 0.9.1
64
+ =======================
65
+ * Add a graphviz output
66
+
67
+ 0.9.0
68
+ =======================
69
+ * measurements for recursive methods are now correct
70
+ * gave up on splitting up recursive methods according to call depth
71
+ * made it possible to eliminate methods from profiling results
72
+ * new printer for call stack visualization
73
+ * new printer to print several profiles in one run
74
+ * HTML profiles contain Textmate links so you can jump to the source code easily
75
+ * producing an event log is now a runtime option
76
+
1
77
  0.7.10 (2009-01-22)
2
78
  =======================
3
79
  * fix SEGFAULT in 1.9
@@ -55,29 +131,29 @@
55
131
 
56
132
  Features
57
133
  --------
58
- * Added two new methods - RubyProf.resume and RubyProf.pause.
134
+ * Added two new methods - RubyProf.resume and RubyProf.pause.
59
135
  RubyProf.resume takes an optional block, which ensures that
60
136
  RubyProf.pause is called. For example:
61
-
137
+
62
138
  10.times do |i|
63
139
  RubyProf.resume do
64
140
  # Some long process
65
141
  end
66
142
  end
67
-
143
+
68
144
  result = RubyProf.stop
69
145
 
70
146
  * Added support for profiling tests that use Ruby's built-in
71
- unit test framework (ie, test derived from
147
+ unit test framework (ie, test derived from
72
148
  Test::Unit::TestCase). To enable profiling simply add
73
149
  the following line of code to your test class:
74
-
150
+
75
151
  include RubyProf::Test
76
-
77
- By default, profiling results are written to the current
152
+
153
+ By default, profiling results are written to the current
78
154
  processes working directory. To change this, or other
79
155
  profiling options, simply modify the PROFILE_OPTIONS hash
80
- table as needed.
156
+ table as needed.
81
157
 
82
158
  * Used the new support for profiling test cases to revamp
83
159
  the way that Rails profiling works. For more information
@@ -93,8 +169,8 @@ Features
93
169
 
94
170
  * Add support for Lloyd Hilaiel's Ruby patch for measuring total heap size.
95
171
  See http://lloydforge.org/projects/ruby. (Jeremy Kemper).
96
-
97
-
172
+
173
+
98
174
  Fixes
99
175
  -------
100
176
  * RubyProf.profile no longer crashes if an exception is
@@ -102,7 +178,7 @@ Fixes
102
178
 
103
179
  * Measure memory in fractional kilobytes rather than rounding down (Jeremy Kemper)
104
180
 
105
-
181
+
106
182
  0.6.0 (2008-02-03)
107
183
  ========================
108
184
 
@@ -122,7 +198,7 @@ Fixes
122
198
  fixes for min_time support, ability to specify templates using
123
199
  strings or filenames, and table layout fixes (Makoto Kuwata)
124
200
  * Fixes to scaling factor for calltrees so that precision is not lost
125
- due to the conversion to doubles (Sylvain Joyeux)
201
+ due to the conversion to doubles (Sylvain Joyeux)
126
202
  * Changed constant ALLOCATED_OBJECTS to ALLOCATIONS in the C code to
127
203
  match the Ruby code (Sylvain Joyeux)
128
204
  * Added support for calltree printer to ruby-prof binary script (Sylvain Joyeux)
@@ -132,7 +208,7 @@ Fixes
132
208
  * Fix ruby-prof to work with the latest version of GEMS (Alexander Dymo)
133
209
  * Always define MEASURE_CPU_TIME and MEASURE_ALLOCATIONS in Ruby code, but
134
210
  set their values to nil if the functionality is not available.
135
-
211
+
136
212
 
137
213
  0.5.2 (2007-07-19)
138
214
  ========================
data/LICENSE CHANGED
@@ -1,15 +1,16 @@
1
- Copyright (C) 2005 Shugo Maeda <shugo@ruby-lang.org>
1
+ Copyright (C) 2005 - 20011 Shugo Maeda <shugo@ruby-lang.org> and Charlie Savage <cfis@savagexi.com>
2
2
  All rights reserved.
3
- *
3
+
4
4
  Redistribution and use in source and binary forms, with or without
5
5
  modification, are permitted provided that the following conditions
6
6
  are met:
7
+
7
8
  1. Redistributions of source code must retain the above copyright
8
9
  notice, this list of conditions and the following disclaimer.
9
10
  2. Redistributions in binary form must reproduce the above copyright
10
11
  notice, this list of conditions and the following disclaimer in the
11
12
  documentation and/or other materials provided with the distribution.
12
- *
13
+
13
14
  THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
14
15
  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15
16
  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -5,89 +5,82 @@
5
5
  ruby-prof is a fast code profiler for Ruby. Its features include:
6
6
 
7
7
  * Speed - it is a C extension and therefore many times faster than the standard Ruby profiler.
8
- * Modes - Ruby prof can measure a number of different parameters, including
9
- call times, memory usage and object allocations.
8
+ * Modes - Ruby prof can measure a number of different parameters, including call times, memory usage and object allocations.
10
9
  * Reports - can generate text and cross-referenced html reports
11
10
  - Flat Profiles - similar to the reports generated by the standard Ruby profiler
12
11
  - Graph profiles - similar to GProf, these show how long a method runs, which methods call it and which methods it calls.
13
12
  - Call tree profiles - outputs results in the calltree format suitable for the KCacheGrind profiling tool.
13
+ - Many more -- see reports section of this README.
14
14
  * Threads - supports profiling multiple threads simultaneously
15
- * Recursive calls - supports profiling recursive method calls
16
-
17
15
 
18
16
  == Requirements
19
17
 
20
- ruby-prof requires Ruby 1.8.4 or higher.
18
+ ruby-prof requires Ruby 1.8.7 or 1.9.1 and higher.
21
19
 
22
20
  If you are running Linux or Unix you'll need a C compiler so the extension
23
21
  can be compiled when it is installed.
24
22
 
25
- If you are running Windows, then you may need to install the
26
- Windows specific RubyGem which includes an already built extension (see below).
23
+ If you are running Windows, then you may need to install the
24
+ Windows specific RubyGem which includes an already built extension (see Install section).
27
25
 
28
26
  == Install
29
27
 
30
28
  The easiest way to install ruby-prof is by using Ruby Gems. To install:
31
29
 
32
- <tt>gem install ruby-prof</tt>
33
-
34
- If you on windows mswin [not mingw] (check via ruby -v) and
35
- don't have an MSVC compiler, please install v0.7.3 which
36
- has a prebuilt binary
37
- C:> gem install ruby-prof -v0.7.3
30
+ gem install ruby-prof
38
31
 
39
- If you're on mingw, please install the devkit first, then install the latest version (gem install ruby-prof).
32
+ If you're on windows then a prebuilt binary gem is available. You may of course
33
+ compile it yourself via use of devkit on MinGW.
40
34
 
41
35
  == Usage
42
36
 
43
- There are three ways of running ruby-prof.
37
+ There are two ways of running ruby-prof, via the command line or via its API.
44
38
 
45
39
  === ruby-prof executable
46
40
 
47
- The first is to use ruby-prof to run the Ruby program
48
- you want to profile. For more information refer to
49
- the ruby-prof documentation[link:files/bin/ruby-prof.html].
41
+ The first is to use ruby-prof to run the Ruby program you want to
42
+ profile. For more information refer to the documentation of the
43
+ ruby-prof command.
50
44
 
51
45
 
52
46
  === ruby-prof API
53
47
 
54
48
  The second way is to use the ruby-prof API to profile
55
- particular segments of code.
49
+ particular segments of code.
56
50
 
57
51
  require 'ruby-prof'
58
-
52
+
59
53
  # Profile the code
60
54
  RubyProf.start
61
55
  ...
62
56
  [code to profile]
63
57
  ...
64
58
  result = RubyProf.stop
65
-
59
+
66
60
  # Print a flat profile to text
67
61
  printer = RubyProf::FlatPrinter.new(result)
68
62
  printer.print(STDOUT)
69
-
63
+
70
64
  Alternatively, you can use a block to tell ruby-prof what
71
65
  to profile:
72
66
 
73
67
  require 'ruby-prof'
74
-
68
+
75
69
  # Profile the code
76
70
  result = RubyProf.profile do
77
71
  ...
78
72
  [code to profile]
79
73
  ...
80
74
  end
81
-
75
+
82
76
  # Print a graph profile to text
83
77
  printer = RubyProf::GraphPrinter.new(result)
84
- printer.print(STDOUT, 0)
78
+ printer.print(STDOUT, {})
85
79
 
86
- Starting with the 0.6.1 release, ruby-prof also supports pausing and resuming
87
- profiling runs.
80
+ ruby-prof also supports pausing and resuming profiling runs.
88
81
 
89
82
  require 'ruby-prof'
90
-
83
+
91
84
  # Profile the code
92
85
  RubyProf.start
93
86
  [code to profile]
@@ -96,59 +89,94 @@ profiling runs.
96
89
  RubyProf.resume
97
90
  [code to profile]
98
91
  result = RubyProf.stop
99
-
92
+
100
93
  Note that resume will automatically call start if a profiling run
101
94
  has not yet started. In addition, resume can also take a block:
102
95
 
103
96
  require 'ruby-prof'
104
-
97
+
105
98
  # Profile the code
106
99
  RubyProf.resume do
107
100
  [code to profile]
108
101
  end
109
102
 
110
103
  data = RubyProf.stop
111
-
112
- With this usage, resume will automatically call pause at the
104
+
105
+ With this usage, resume will automatically call pause at the
113
106
  end of the block.
114
107
 
115
108
 
116
- === require unprof
109
+ == Method and Thread Elimination
110
+
111
+ ruby-prof supports eliminating specific methods and threads from profiling
112
+ results. This is useful for reducing connectivity in the call graph, making it easier to
113
+ identify the source of performance problems when using a graph printer.
114
+
115
+ For example, consider Integer#times: it's hardly ever useful to know how much time is
116
+ spent in the method itself. We're much more interested in how much the passed in block
117
+ contributes to the time spent in the method which contains the Integer#times call.
118
+
119
+ Methods are eliminated from the collected data by calling `eliminate_methods!` on the
120
+ profiling result, before submitting it to a printer.
121
+
122
+ result = RubyProf.stop
123
+ result.eliminate_methods!([/Integer#times/])
124
+
125
+ The argument given to `eliminate_methods!` is either an array of regular expressions, or
126
+ the name of a file containing a list of regular expressions (line separated text).
127
+
128
+ After eliminating methods the resulting profile will appear exactly as if those methods
129
+ had been inlined at their call sites.
130
+
131
+ In a similar manner, threads can be excluded so they are not profiled at all. To do this,
132
+ pass an array of threads to exclude to ruby-prof:
133
+
134
+ RubyProf::exclude_threads = [ thread2 ]
135
+ RubyProf.start
136
+
137
+ Note that the excluded threads must be specified *before* profiling.
138
+
139
+
140
+ == Benchmarking full load time including rubygems startup cost ==
141
+
142
+ If you want to get a more accurate measurement of what takes all of a gem's bin/xxx
143
+ command to load, you may want to also measure rubygems' startup penalty.
144
+ You can do this by calling into bin/ruby-prof directly, ex:
117
145
 
118
- The third way of using ruby-prof is by requiring unprof.rb:
146
+ $ gem which ruby-prof
147
+ g:/192/lib/ruby/gems/1.9.1/gems/ruby-prof-0.10.2/lib/ruby-prof.rb
119
148
 
120
- require 'unprof'
149
+ now run it thus (substitute lib/ruby-prof.rb with bin/ruby-prof):
121
150
 
122
- This will start profiling immediately and will output the results
123
- using a flat profile report.
151
+ $ ruby g:/192/lib/ruby/gems/1.9.1/gems/ruby-prof-0.10.2/bin/ruby-prof g:\192\bin\some_installed_gem_command
124
152
 
125
- This method is provided for backwards compatibility. Using
126
- {ruby-prof}[link:files/bin/ruby-prof.html] provides more flexibility.
153
+ or
127
154
 
155
+ $ ruby g:/192/lib/ruby/gems/1.9.1/gems/ruby-prof-0.10.2/bin/ruby-prof ./some_file_that_does_a_require_rubygems_at_the_beginning.rb
128
156
 
129
157
  == Profiling Tests
130
158
 
131
- Starting with the 0.6.1 release, ruby-prof supports profiling tests cases
132
- written using Ruby's built-in unit test framework (ie, test derived from
133
- Test::Unit::TestCase). To enable profiling simply add the following line
159
+ ruby-prof supports profiling tests cases
160
+ written using Ruby's built-in unit test framework (ie, test derived from
161
+ Test::Unit::TestCase). To enable profiling simply add the following line
134
162
  of code to within your test class:
135
-
136
- include RubyProf::Test
137
-
163
+
164
+ include RubyProf::Test
165
+
138
166
  Each test method is profiled separately. ruby-prof will run each test method
139
167
  once as a warmup and then ten additional times to gather profile data.
140
- Note that the profile data will *not* include the class's setup or
168
+ Note that the profile data will *not* include the class's setup or
141
169
  teardown methods.
142
170
 
143
- Separate reports are generated for each method and saved, by default,
171
+ Separate reports are generated for each method and saved, by default,
144
172
  in the test process's working directory. To change this, or other profiling
145
- options, modify your test class's PROFILE_OPTIONS hash table. To globally
146
- change test profiling options, modify RubyProf::Test::PROFILE_OPTIONS.
173
+ options, modify your test class's PROFILE_OPTIONS hash table. To globally
174
+ change test profiling options, modify RubyProf::Test::PROFILE_OPTIONS.
147
175
 
148
176
 
149
177
  == Profiling Rails
150
178
 
151
- To profile a Rails application it is vital to run it using production like
179
+ To profile a Rails application it is vital to run it using production like
152
180
  settings (cache classes, cache view lookups, etc.). Otherwise, Rail's
153
181
  dependency loading code will overwhelm any time spent in the application
154
182
  itself (our tests show that Rails dependency loading causes a roughly 6x
@@ -157,49 +185,23 @@ profile.rb.
157
185
 
158
186
  So to profile Rails:
159
187
 
160
- 1. Create a new profile.rb environment - or simply copy the example file
161
- in ruby-prof/rails/environment/profile.rb
162
-
163
- 2. Copy the file:
164
-
165
- ruby-prof/rails/profile_test_helper.rb
166
-
167
- To:
168
-
169
- your_rails_app/test/profile_test_helper.rb
170
-
171
- 3. Create a new test directory for profiling:
172
-
173
- your_rails_app/test/profile
174
-
175
-
176
- 4. Write unit, functional or integration tests specifically designed
177
- to profile some part of your Rails application. At the top
178
- of each test, replace this line:
179
-
180
- require File.dirname(__FILE__) + '/../test_helper'
181
-
182
- With:
183
-
184
- require File.dirname(__FILE__) + '/../profile_test_helper'
185
-
186
- For example:
187
-
188
- require File.dirname(__FILE__) + '/../profile_test_helper'
189
-
190
- class ExampleTest < Test::Unit::TestCase
191
- include RubyProf::Test
192
- fixtures ....
193
-
194
- def test_stuff
195
- puts "Test method"
196
- end
197
- end
198
-
199
- 5. Now run your tests. Results will be written to:
200
-
201
- your_rails_app/tmp/profile
202
-
188
+ 1. Create a new profile.rb environment. Make sure to turn on cache_classes
189
+ and cache_template_loading. Otherwise your profiling results will be
190
+ overwhelemed by the time Rails spends loading required files.
191
+
192
+ 2. Add the ruby prof rack adapter to your middleware stack. One way to
193
+ do this is by adding the following code to config.ru:
194
+
195
+ if Rails.env.profile?
196
+ use Rack::RubyProf, :path => '/temp/profile'
197
+ end
198
+
199
+ The path is where you want profiling results to be stored. By default the
200
+ rack adapter will generate a html call graph report and flat text report.
201
+
202
+ 3. Now make a request to your running server. New profiling information will
203
+ be generated for each request. Note that each request will overwrite
204
+ the profiling reports created by the previous request!
203
205
 
204
206
  == Reports
205
207
 
@@ -209,36 +211,46 @@ ruby-prof can generate a number of different reports:
209
211
  * Graph Reports
210
212
  * HTML Graph Reports
211
213
  * Call graphs
214
+ * Call stack reports
215
+ * More!
212
216
 
213
- Flat profiles show the overall time spent in each method. They
217
+ Flat profiles show the overall time spent in each method. They
214
218
  are a good of quickly identifying which methods take the most time.
215
219
  An example of a flat profile and an explanation can be found in
216
- {examples/flat.txt}[link:files/examples/flat_txt.html].
217
-
218
- Graph profiles also show the overall time spent in each method.
219
- In addition, they also show which methods call the current
220
- method and which methods its calls. Thus they are good for
221
- understanding how methods gets called and provide insight into
222
- the flow of your program. An example text graph profile
223
- is located at {examples/graph.txt}[link:files/examples/graph_txt.html].
224
-
225
- HTML Graph profiles are the same as graph profiles, except
226
- output is generated in hyper-linked HTML. Since graph profiles
227
- can be quite large, the embedded links make it much easier to
228
- navigate the results. An example html graph profile
229
- is located at {examples/graph.html}[link:files/examples/graph_html.html].
230
-
231
- HTML Graph profiles are the same as graph profiles, except
232
- output is generated in hyper-linked HTML. Since graph profiles
233
- can be quite large, the embedded links make it much easier to
234
- navigate the results. An example html graph profile
235
- is located at {examples/graph.html}[link:files/examples/graph_html.html].
220
+ {examples/flat.txt}[http://github.com/rdp/ruby-prof/tree/master/examples/flat.txt].
221
+
222
+ There are several varieties of these -- run $ ruby-prof --help
223
+
224
+ Graph profiles also show the overall time spent in each method. In
225
+ addition, they also show which methods call the current method and which
226
+ methods its calls. Thus they are good for understanding how methods
227
+ gets called and provide insight into the flow of your program. An
228
+ example text graph profile is located at
229
+ {examples/graph.txt}[http://github.com/rdp/ruby-prof/tree/master/examples/graph.txt].
230
+
231
+ HTML Graph profiles are the same as graph profiles, except output is
232
+ generated in hyper-linked HTML. Since graph profiles can be quite large,
233
+ the embedded links make it much easier to navigate the results. An
234
+ example html graph profile is located at
235
+ {examples/graph.html}[http://github.com/rdp/ruby-prof/tree/master/examples/graph.html].
236
236
 
237
237
  Call graphs output results in the calltree profile format which is used
238
- by KCachegrind. Call graph support was generously donated by Carl Shimer.
239
- More information about the format can be found at
240
- the {KCachegrind}[link:http://kcachegrind.sourceforge.net/cgi-bin/show.cgi/KcacheGrindCalltreeFormat] site.
238
+ by KCachegrind. Call graph support was generously donated by Carl
239
+ Shimer. More information about the format can be found at the
240
+ {KCachegrind}[http://kcachegrind.sourceforge.net/cgi-bin/show.cgi/KcacheGrindCalltreeFormat]
241
+ site.
242
+
243
+ Call stack reports produce a HTML visualization of the time spent in
244
+ each execution path of the profiled code. An example can be found at
245
+ {examples/stack.html}[http://github.com/rdp/ruby-prof/tree/master/examples/call_stack.html].
246
+
247
+ Another good example: [http://twitpic.com/28z94a]
248
+
249
+ Finally, there's a so called MultiPrinter which can generate several
250
+ reports in one profiling run. See
251
+ {examples/multi.stack.html}[http://github.com/rdp/ruby-prof/tree/master/examples/multi.stack.html].
241
252
 
253
+ There is also a graphviz .dot visualiser.
242
254
 
243
255
  == Printers
244
256
 
@@ -248,7 +260,11 @@ Reports are created by printers. Supported printers include:
248
260
  * RubyProf::FlatPrinterWithLineNumbers - same as above but more verbose
249
261
  * RubyProf::GraphPrinter - Creates a call graph report in text format
250
262
  * RubyProf::GraphHtmlPrinter - Creates a call graph report in HTML (separate files per thread)
263
+ * RubyProf::DotPrinter - Creates a call graph report in GraphViz's DOT format which can be converted to an image
251
264
  * RubyProf::CallTreePrinter - Creates a call tree report compatible with KCachegrind.
265
+ * RubyProf::CallStackPrinter - Creates a HTML visualization of the Ruby stack
266
+ * RubyProf::MultiPrinter - Uses the other printers to create several reports in one profiling run
267
+ * More!
252
268
 
253
269
  To use a printer:
254
270
 
@@ -258,13 +274,20 @@ To use a printer:
258
274
  printer.print(STDOUT, :min_percent => 2)
259
275
 
260
276
  The first parameter is any writable IO object such as STDOUT or a file.
261
- The second parameter, specifies the minimum percentage a method must take
262
- to be printed. Percentages should be specified as integers in the range 0 to 100.
277
+ The second parameter, specifies the minimum percentage a method must take
278
+ to be printed. Percentages should be specified as integers in the range 0 to 100.
263
279
  For more information please see the documentation for the different printers.
264
280
 
265
281
  The other option is :print_file => true (default false), which adds the filename to the
266
282
  output (GraphPrinter only).
267
283
 
284
+ The MultiPrinter differs from the other printers in that it requires a directory path
285
+ and a basename for the files it produces.
286
+
287
+ printer = RubyProf::MultiPrinter.new(result)
288
+ printer.print(:path => ".", :profile => "profile")
289
+
290
+
268
291
  == Measurements
269
292
 
270
293
  Depending on the mode and platform, ruby-prof can measure various
@@ -279,7 +302,7 @@ aspects of a Ruby program. Supported measurements include:
279
302
  * garbage collection time (RubyProf::GC_TIME)
280
303
 
281
304
  Process time measures the time used by a process between any two moments.
282
- It is unaffected by other processes concurrently running
305
+ It is unaffected by other processes concurrently running
283
306
  on the system. Note that Windows does not support measuring process
284
307
  times - therefore, measurements on Windows defaults to wall time.
285
308
 
@@ -335,17 +358,15 @@ environment variable:
335
358
  * export RUBY_PROF_MEASURE_MODE=memory
336
359
  * export RUBY_PROF_MEASURE_MODE=gc_runs
337
360
  * export RUBY_PROF_MEASURE_MODE=gc_time
338
-
339
- Note that these values have changed since ruby-prof-0.3.0.
340
361
 
341
- On Linux, process time is measured using the clock method provided
362
+ On Linux, process time is measured using the clock method provided
342
363
  by the C runtime library. Note that the clock method does not
343
364
  report time spent in the kernel or child processes and therefore
344
365
  does not measure time spent in methods such as Kernel.sleep method.
345
366
  If you need to measure these values, then use wall time. Wall time
346
367
  is measured using the gettimeofday kernel method.
347
368
 
348
- On Windows, timings default to wall times. If you set the clock
369
+ On Windows, timings default to wall times. If you set the clock
349
370
  mode to PROCESS_TIME, then timing are read using the clock method
350
371
  provided by the C runtime library. Note though, these values are
351
372
  wall times on Windows and not process times like on Linux.
@@ -354,62 +375,35 @@ Wall time is measured using the GetLocalTime API.
354
375
  If you use wall time, the results will be affected by other
355
376
  processes running on your computer, network delays, disk access,
356
377
  etc. As result, for the best results, try to make sure your
357
- computer is only performing your profiling run and is
378
+ computer is only performing your profiling run and is
358
379
  otherwise quiescent.
359
380
 
360
381
  On both platforms, cpu time is measured using the RDTSC assembly
361
382
  function provided by the Pentium and PowerPC platforms. CPU time
362
- is dependent on the cpu's frequency. On Linux, ruby-prof attempts
383
+ is dependent on the cpu's frequency. On Linux, ruby-prof attempts
363
384
  to read this value from "/proc/cpuinfo." On Windows, you must
364
385
  manually specify the clock frequency. This can be done using the
365
386
  RUBY_PROF_CPU_FREQUENCY environment variable:
366
387
 
367
388
  export RUBY_PROF_CPU_FREQUENCY=<value>
368
-
369
- You can also directly set the cpu frequency by calling:
370
-
371
- RubyProf.cpu_frequency = <value>
372
-
373
389
 
374
- == Recursive Calls
375
-
376
- Recursive calls occur when method A calls method A and cycles
377
- occur when method A calls method B calls method C calls method A.
378
- ruby-prof detects both direct recursive calls and cycles. Both
379
- are indicated in reports by a "d number" in parentheses following a method
380
- name. For example, here is a flat profile from the test method
381
-
382
- RecursiveTest#test_recursive:
383
-
384
- %self total self wait child calls name
385
- 100.00 2.00 2.00 0.00 0.00 2 Kernel#sleep
386
- 0.00 2.00 0.00 0.00 2.00 0 RecursiveTest#test_cycle
387
- 0.00 0.00 0.00 0.00 0.00 2 Fixnum#==
388
- 0.00 0.00 0.00 0.00 0.00 2 Fixnum#-
389
- 0.00 1.00 0.00 0.00 1.00 1 Object#sub_cycle(d1)
390
- 0.00 2.00 0.00 0.00 2.00 1 Object#sub_cycle
391
- 0.00 2.00 0.00 0.00 2.00 1 Object#cycle
392
- 0.00 1.00 0.00 0.00 1.00 1 Object#cycle(d1)
390
+ You can also directly set the cpu frequency by calling:
393
391
 
394
- Notice the presence of Object#cycle and Object#cycle(d1). The d1 means
395
- depth 1 -- the method was either recursively called (directly or indirectly).
392
+ RubyProf.cpu_frequency = <value>
396
393
 
397
- However, the self time values for recursive calls should always
398
- be accurate. It is also believed that the total times are
399
- accurate, but these should be carefully analyzed to verify their veracity.
400
394
 
401
395
  == Multi-threaded Applications
402
396
 
403
397
  Unfortunately, Ruby does not provide an internal api
404
398
  for detecting thread context switches in 1.8. As a result, the
405
399
  timings ruby-prof reports for each thread may be slightly
406
- inaccurate. In particular, this will happen for newly
407
- spawned threads that go to sleep immediately (their first call).
400
+ inaccurate. In particular, this will happen for newly
401
+ spawned threads that go to sleep immediately (their first call).
408
402
  For instance, if you use Ruby's timeout library to wait for 2 seconds,
409
403
  the 2 seconds will be assigned to the foreground thread
410
404
  and not the newly created background thread. These errors
411
405
  can largely be avoided if the background thread performs any
412
- operation before going to sleep.
406
+ operation before going to sleep.
413
407
 
414
408
  == Performance
415
409
 
@@ -420,13 +414,12 @@ profiled. Most programs will run approximately twice as slow
420
414
  while highly recursive programs (like the fibonacci series test)
421
415
  will run three times slower.
422
416
 
423
- Because of some threading difficulties in 1.9, it currently runs a bit slower there.
424
- http://www.ruby-forum.com/topic/201329
425
-
426
417
  == License
427
418
 
428
419
  See LICENSE for license information.
429
420
 
430
421
  == Development
431
422
 
432
- Code is located at http://github.com/rdp/ruby-prof
423
+ Code is located at http://github.com/rdp/ruby-prof
424
+
425
+ Google group/mailing list: http://groups.google.com/group/ruby-optimization