Skip to content

Commit 886a066

Browse files
committed
Add docs, don't use instance variables for per-request info
1 parent 3d8a5c8 commit 886a066

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

lib/rack/contrib/profiler.rb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ module Rack
55
# calltree profile of the request.
66
#
77
# Pass the :printer option to pick a different result format.
8+
#
9+
# You can cause every request to be run multiple times by passing the
10+
# `:times` option to the `use Rack::Profiler` call. You can also run a
11+
# given request multiple times, by setting the `profiler_runs` query
12+
# parameter in the request URL.
13+
#
814
class Profiler
915
MODES = %w(process_time wall_time cpu_time
1016
allocations memory gc_runs gc_time)
@@ -27,7 +33,6 @@ def initialize(app, options = {})
2733

2834
def call(env)
2935
if mode = profiling?(env)
30-
@times = @request.params['times'].to_i if @request.params['times']
3136
profile(env, mode)
3237
else
3338
@app.call(env)
@@ -37,8 +42,8 @@ def call(env)
3742
private
3843
def profiling?(env)
3944
unless ::RubyProf.running?
40-
@request = Rack::Request.new(env.clone)
41-
if mode = @request.params.delete('profile')
45+
request = Rack::Request.new(env.clone)
46+
if mode = request.params.delete('profile')
4247
if ::RubyProf.const_defined?(mode.upcase)
4348
mode
4449
else
@@ -54,8 +59,10 @@ def profile(env, mode)
5459
::RubyProf.measure_mode = ::RubyProf.const_get(mode.upcase)
5560

5661
GC.enable_stats if GC.respond_to?(:enable_stats)
62+
request = Rack::Request.new(env.clone)
63+
runs = (request.params['profiler_runs'] || @times).to_i
5764
result = ::RubyProf.profile do
58-
@times.times { @app.call(env) }
65+
runs.times { @app.call(env) }
5966
end
6067
GC.disable_stats if GC.respond_to?(:disable_stats)
6168

test/spec_rack_profiler.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
specify 'called multiple times via query params' do
1818
req = Rack::MockRequest.env_for("/", :params => "profile=process_time&times=4")
1919
body = Rack::Profiler.new(app).call(req)[2].string
20-
body.must_match(/Fixnum#to_s \[4 calls, 4 total\]/)
20+
body.must_match(/Proc#call \[4 calls, 4 total\]/)
2121
end
2222

2323
specify 'CallStackPrinter has Content-Type test/html' do

0 commit comments

Comments
 (0)