module BenchmarkInterface::Backends::Bm

Constants

MIN_SAMPLING_TIME

Public Class Methods

run(benchmark_set, names, options) click to toggle source
# File lib/benchmark-interface/backends/benchmark.rb, line 15
def self.run(benchmark_set, names, options)
  # If we don't remove these we'll get a warning when we load the real
  # implementation.
    
  ::Benchmark.send(:remove_const, :CAPTION) if defined?(::Benchmark::CAPTION)
  ::Benchmark.send(:remove_const, :FORMAT)  if defined?(::Benchmark::FORMAT)

  benchmark_interface_original_require 'benchmark'

  unless options['--no-scale']
    min_time = benchmark_set.benchmarks.map(&:basic_iteration_time).min

    if min_time < MIN_SAMPLING_TIME
      short_iterations = true
      samples = (MIN_SAMPLING_TIME / min_time / MIN_SAMPLING_TIME).to_i
      puts "These are short benchmarks - we're running each #{samples} times so they take about a second"
    end
  end

  label_width = benchmark_set.benchmarks(names).map(&:name).map(&:size).max

  block = Proc.new do |x|
    benchmark_set.benchmarks(names).each do |benchmark|
      block = benchmark.block
      if short_iterations
        x.report(benchmark.name) do
          samples.times do
            block.call
          end
        end
      else
        x.report(benchmark.name, &benchmark.block)
      end
    end
  end

  if self == BmBm
    ::Benchmark.bmbm label_width, &block
  else
    ::Benchmark.bm label_width, &block
  end
end