rack-perftools_profiler 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.rdoc CHANGED
@@ -55,7 +55,7 @@ For Rack::Builder, call 'use' inside the Builder constructor block
55
55
  == Options
56
56
 
57
57
  * :default_printer - can be set to 'text', 'gif', or 'pdf'. Default is :text
58
- * :mode - can be set to 'cputime' or 'walltime'. Default is :cputime
58
+ * :mode - can be set to 'cputime', 'objects', 'walltime'. Default is :cputime. See the 'Profiling Modes' section below.
59
59
  * :frequency - in :cputime mode, the number of times per second the app will be sampled. Default is 100 (times/sec)
60
60
  * :bundler - run the profiler binary using 'bundle' if set to true. Default is false
61
61
  * :gemfile_dir - directory with Gemfile. Default is the current directory.
@@ -99,6 +99,47 @@ start/stop mode, they are added to the \_\_data\_\_ URL
99
99
  (for 'ignore' and 'focus', please see http://google-perftools.googlecode.com/svn/trunk/doc/cpuprofile.html#pprof
100
100
  for more details)
101
101
 
102
+ == Profiling Modes
103
+
104
+ perftools.rb can be put into three different profiling modes.
105
+
106
+ * CPU time mode - Reports how many CPU cycles are spent in each section of code. This is the default and can be enabled by setting ':mode => :cputime'
107
+ * Wall time mode - Reports the amount of time (as in, wall clock time) spent in each section of code. Enable by setting ':mode => :walltime'
108
+ * Object allocation mode - Reports the percentage of object allocations performed in each section of code. Enable by setting ':mode => :objects'
109
+
110
+ For example, consider the following Sinatra application:
111
+
112
+ require 'sinatra'
113
+ require 'rack/perftools_profiler'
114
+
115
+ configure do
116
+ use Rack::PerftoolsProfiler, :default_printer => 'gif', :mode => :cputime
117
+ end
118
+
119
+ get "/slow" do
120
+ sleep(3)
121
+ "hello"
122
+ end
123
+
124
+ In the default mode, there will be no profiling data for the 'slow' route, because it uses few CPU cycles (You'll see the message 'No nodes to print').
125
+
126
+ If you change the mode to ':walltime', you'll get profiling data, since the call to 'sleep' causes the code to spend several seconds of wall time in the block.
127
+
128
+ == Changing behavior with environment variables
129
+
130
+ The mode and frequency settings are enabled by setting environment variables. Some of these environment variables must be set before 'perftools' is required. If you only require 'rack/perftools_profiler', it will do the right thing (require 'perftools' after setting the environment variables).
131
+
132
+ If you need to require 'perftools' before 'rack/perftools_profiler' (or you have other problems changing the mode or frequency), try using these environment variables yourself.
133
+
134
+ Setting the frequency:
135
+ CPUPROFILE_FREQUENCY=500 ruby your_app.rb
136
+
137
+ Setting the mode to 'wall time'
138
+ CPUPROFILE_REALTIME=1 ruby your_app.rb
139
+
140
+ Setting the mode to 'object allocation'
141
+ CPUPROFILE_OBJECTS=1 ruby your_app.rb
142
+
102
143
  == Acknowledgments
103
144
 
104
145
  A huge thanks to Aman Gupta for the awesome perftools.rb gem.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.2.0
@@ -23,7 +23,7 @@ module Rack::PerftoolsProfiler
23
23
  PROFILING_SETTINGS_FILE = ::File.join(self.tmpdir, 'rack_perftools_profiler.config')
24
24
  DEFAULT_PRINTER = :text
25
25
  DEFAULT_MODE = :cputime
26
- UNSET_FREQUENCY = -1
26
+ UNSET_FREQUENCY = "-1"
27
27
  DEFAULT_GEMFILE_DIR = '.'
28
28
 
29
29
  def initialize(app, options)
@@ -33,6 +33,9 @@ module Rack::PerftoolsProfiler
33
33
  @bundler = (options.delete(:bundler) { false })
34
34
  @gemfile_dir = (options.delete(:gemfile_dir) { DEFAULT_GEMFILE_DIR })
35
35
  ProfileDataAction.check_printer(@printer)
36
+ # We need to set the enviroment variables before loading perftools
37
+ set_env_vars
38
+ require 'perftools'
36
39
  raise ProfilerArgumentError, "Invalid option(s): #{options.keys.join(' ')}" unless options.empty?
37
40
  end
38
41
 
@@ -48,7 +51,6 @@ module Rack::PerftoolsProfiler
48
51
  end
49
52
 
50
53
  def start
51
- set_env_vars
52
54
  PerfTools::CpuProfiler.stop
53
55
  PerfTools::CpuProfiler.start(PROFILING_DATA_FILE)
54
56
  self.profiling = true
@@ -105,12 +107,15 @@ module Rack::PerftoolsProfiler
105
107
 
106
108
  def set_env_vars
107
109
  ENV['CPUPROFILE_REALTIME'] = '1' if @mode == :walltime
110
+ ENV['CPUPROFILE_OBJECTS'] = '1' if @mode == :objects
108
111
  ENV['CPUPROFILE_FREQUENCY'] = @frequency if @frequency != UNSET_FREQUENCY
109
112
  end
110
113
 
114
+ # Useful for testing
111
115
  def unset_env_vars
112
116
  ENV.delete('CPUPROFILE_REALTIME')
113
117
  ENV.delete('CPUPROFILE_FREQUENCY')
118
+ ENV.delete('CPUPROFILE_OBJECTS')
114
119
  end
115
120
 
116
121
  def profiling=(value)
@@ -1,5 +1,4 @@
1
1
  require 'rack'
2
- require 'perftools'
3
2
  require 'pstore'
4
3
  require 'open4'
5
4
 
@@ -159,6 +159,28 @@ class RackPerftoolsProfilerTest < Test::Unit::TestCase
159
159
  assert_equal '1', realtime
160
160
  end
161
161
 
162
+ should "set CPUPROFILE_OBJECTS to 1 if mode is 'objects'" do
163
+ realtime = ENV['CPUPROFILE_OBJECTS']
164
+ assert_nil realtime
165
+ app = lambda do |env|
166
+ realtime = ENV['CPUPROFILE_OBJECTS']
167
+ [200, {}, ["hi"]]
168
+ end
169
+ Rack::PerftoolsProfiler.new(app, :mode => 'objects').call(@profiled_request_env)
170
+ assert_equal '1', realtime
171
+ end
172
+
173
+ should "not set CPUPROFILE_FREQUENCY by default" do
174
+ frequency = ENV['CPUPROFILE_FREQUENCY']
175
+ assert_nil frequency
176
+ app = lambda do |env|
177
+ frequency = ENV['CPUPROFILE_FREQUENCY']
178
+ [200, {}, ["hi"]]
179
+ end
180
+ Rack::PerftoolsProfiler.new(app).call(@profiled_request_env)
181
+ assert_nil frequency
182
+ end
183
+
162
184
  should 'alter CPUPROFILE_FREQUENCY if frequency is set' do
163
185
  frequency = ENV['CPUPROFILE_FREQUENCY']
164
186
  assert_nil frequency
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
8
- - 1
9
- version: 0.1.1
7
+ - 2
8
+ - 0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Ben Brinckerhoff
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-10-11 00:00:00 -06:00
17
+ date: 2010-11-09 00:00:00 -07:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency