class Bench::Implementation

Constants

BEFORE_WARMUP_TIME
MAX_WARMUP
MAX_WARMUP_TIME
SAMPLES_COUNT
WARMED_UP_RELATIVE_RANGE
WARMUP_WINDOW_SIZE

Attributes

name[R]

Public Class Methods

new(name) click to toggle source
# File lib/bench9000/implementation.rb, line 22
def initialize(name)
  @name = name
end

Public Instance Methods

measure(flags, benchmark) click to toggle source
# File lib/bench9000/implementation.rb, line 26
def measure(flags, benchmark)
  command = "bash -c \"#{command(benchmark)}\""

  puts command if flags.has_key? "--show-commands"

  warming_up = true
  warmup_window = []
  warmup_samples = []
  samples = []

  overall_time = Time.now

  IO.popen command, "r+" do |subprocess|
    while true
      line = subprocess.gets

      if line.nil? || line == "error"
        return :failed
      end

      unless line.match(/\d+\.\d+/)
        STDERR.puts line
        next
      end

      time = line.to_f
      puts time if flags.has_key? "--show-samples"

      elapsed_time = Time.now - overall_time

      if elapsed_time < BEFORE_WARMUP_TIME
        warmup_samples.push time

        subprocess.puts "continue"
      elsif warming_up
        warmup_samples.push time

        warmup_window.shift if warmup_window.size == WARMUP_WINDOW_SIZE
        warmup_window.push time
        window_relative_range = Stats.range(warmup_window) / Stats.mean(warmup_window)

        if warmup_window.size == WARMUP_WINDOW_SIZE && window_relative_range < WARMED_UP_RELATIVE_RANGE
          warming_up = false
          warmup_samples = warmup_samples.reverse.drop(WARMUP_WINDOW_SIZE).reverse
          samples = warmup_window
        elsif warmup_samples.size > MAX_WARMUP || elapsed_time > MAX_WARMUP_TIME
          puts "warning: #{@name} #{benchmark} never warmed up!"
          warming_up = false
        end

        subprocess.puts "continue"
      else
        samples.push time

        if samples.size < SAMPLES_COUNT
          subprocess.puts "continue"
        else
          subprocess.puts "stop"
          break
        end
      end
    end
  end

  raise "not enough warmup samples" if warmup_samples.nil?
  raise "not enough samples" if samples.nil? || samples.size < SAMPLES_COUNT

  Measurement.new(warmup_samples, samples)
end
to_s() click to toggle source
# File lib/bench9000/implementation.rb, line 96
def to_s
  @name
end