Skip to content

Commit

Permalink
Require ruby-prof 0.6.1 or later. Use resume/pause to omit extraneous…
Browse files Browse the repository at this point in the history
… machinery from profile.
  • Loading branch information
jeremy committed May 30, 2008
1 parent 131dd0e commit 946f514
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions actionpack/lib/action_controller/request_profiler.rb
Expand Up @@ -17,13 +17,13 @@ def initialize(script_path)
reset!
end

def benchmark(n)
def benchmark(n, profiling = false)
@quiet = true
print ' '

result = Benchmark.realtime do
n.times do |i|
run
run(profiling)
print_progress(i)
end
end
Expand All @@ -43,8 +43,15 @@ def define_run_method(script_path)
script = File.read(script_path)

source = <<-end_source
def run
#{script}
def run(profiling = false)
if profiling
RubyProf.resume do
#{script}
end
else
#{script}
end
old_request_count = request_count
reset!
self.request_count = old_request_count
Expand Down Expand Up @@ -91,21 +98,22 @@ def run
def profile(sandbox)
load_ruby_prof

results = RubyProf.profile { benchmark(sandbox) }
benchmark(sandbox, true)
results = RubyProf.stop

show_profile_results results
results
end

def benchmark(sandbox)
def benchmark(sandbox, profiling = false)
sandbox.request_count = 0
elapsed = sandbox.benchmark(options[:n]).to_f
elapsed = sandbox.benchmark(options[:n], profiling).to_f
count = sandbox.request_count.to_i
puts '%.2f sec, %d requests, %d req/sec' % [elapsed, count, count / elapsed]
end

def warmup(sandbox)
Benchmark.realtime { sandbox.run }
Benchmark.realtime { sandbox.run(false) }
end

def default_options
Expand Down Expand Up @@ -136,6 +144,7 @@ def parse_options(args)
protected
def load_ruby_prof
begin
gem 'ruby-prof', '>= 0.6.1'
require 'ruby-prof'
if mode = options[:measure]
RubyProf.measure_mode = RubyProf.const_get(mode.upcase)
Expand Down

0 comments on commit 946f514

Please sign in to comment.