class RepeatingBenchmark require 'benchmark' def self.measure(&block) Benchmark.measure do 1000.times(&block) end end def self.warm_up(&block) # ensure that everything that needs to be lazy instantiated is block.call # and if it's JRuby, warmup takes A LOT LONGER :) if Config::CONFIG['ruby_install_name'] == 'jruby' STDERR.puts "Warming up JIT" 10000.times { block.call } STDERR.puts "Done" end end def self.report(description, &block) self.warm_up(&block) 10.times do log "#{description}: #{ measure(&block) }" end end def self.log(message) File.open("#{RAILS_ROOT}/log/benchmark.log", "a") { |f| f.puts message } end end # shortcut for console work def bm(&block) RepeatingBenchmark.measure(&block).to_s end