diff options
Diffstat (limited to 'tasks/benchmark.rake')
-rw-r--r-- | tasks/benchmark.rake | 45 |
1 files changed, 40 insertions, 5 deletions
diff --git a/tasks/benchmark.rake b/tasks/benchmark.rake index 7456c5c0a..69c0fc27c 100644 --- a/tasks/benchmark.rake +++ b/tasks/benchmark.rake @@ -6,6 +6,14 @@ namespace :benchmark do def generate_scenario_tasks(location, name) desc File.read(File.join(location, 'description')) task name => "#{name}:run" + # Load a BenchmarkerTask to handle config of the benchmark + task_handler_file = File.expand_path(File.join(location, 'benchmarker_task.rb')) + if File.exist?(task_handler_file) + require task_handler_file + run_args = BenchmarkerTask.run_args + else + run_args = [] + end namespace name do task :setup do @@ -27,20 +35,20 @@ namespace :benchmark do end desc "Run the #{name} scenario." - task :run => :generate do + task :run, [*run_args] => :generate do |_, args| format = if RUBY_VERSION =~ /^1\.8/ Benchmark::FMTSTR else Benchmark::FORMAT end - report = [] + details = [] Benchmark.benchmark(Benchmark::CAPTION, 10, format, "> total:", "> avg:") do |b| times = [] ENV['ITERATIONS'].to_i.times do |i| start_time = Time.now.to_i times << b.report("Run #{i + 1}") do - @benchmark.run + details << @benchmark.run(args) end report << [to_millis(start_time), to_millis(times.last.real), 200, true, name] end @@ -53,14 +61,41 @@ namespace :benchmark do write_csv("#{name}.samples", %w{timestamp elapsed responsecode success name}, report) + + # report details, if any were produced + if details[0].is_a?(Array) && details[0][0].is_a?(Benchmark::Tms) + # assume all entries are Tms if the first is + # turn each into a hash of label => tms (since labels are lost when doing arithmetic on Tms) + hashed = details.reduce([]) do |memo, measures| + memo << measures.reduce({}) {|memo2, measure| memo2[measure.label] = measure; memo2} + memo + end + # sum across all hashes + result = {} + + hashed_totals = hashed.reduce {|memo, h| memo.merge(h) {|k, old, new| old + new }} + # average the totals + hashed_totals.keys.each {|k| hashed_totals[k] /= details.length } + min_width = 14 + max_width = (hashed_totals.keys.map(&:length) << min_width).max + puts "\n" + puts sprintf("%2$*1$s %3$s", -max_width, 'Details (avg)', " user system total real") + puts "-" * (46 + max_width) + hashed_totals.sort.each {|k,v| puts sprintf("%2$*1$s %3$s", -max_width, k, v.format) } + end end desc "Profile a single run of the #{name} scenario." - task :profile => :generate do + task :profile, [:warm_up_runs, *run_args] => :generate do |_, args| + warm_up_runs = (args[:warm_up_runs] || '0').to_i + warm_up_runs.times do + @benchmark.run(args) + end + require 'ruby-prof' result = RubyProf.profile do - @benchmark.run + @benchmark.run(args) end printer = RubyProf::CallTreePrinter.new(result) |