@@ -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
0 commit comments