class BenchmarkDriver::Output::Simple

Constants

NAME_LENGTH

Public Class Methods

new(metrics:, jobs:, contexts:) click to toggle source

@param [Array<BenchmarkDriver::Metric>] metrics @param [Array<BenchmarkDriver::Job>] jobs @param [Array<BenchmarkDriver::Context>] contexts

# File lib/benchmark_driver/output/simple.rb, line 7
def initialize(metrics:, jobs:, contexts:)
  @metrics = metrics
  @context_names = contexts.map(&:name)
  @name_length = jobs.map(&:name).map(&:size).max
end

Public Instance Methods

report(result) click to toggle source

@param [BenchmarkDriver::Result] result

# File lib/benchmark_driver/output/simple.rb, line 62
def report(result)
  if @with_benchmark
    $stdout.print("%#{NAME_LENGTH}s  " % humanize(result.values.fetch(@metrics.first)))
  else
    $stdout.print '.'
  end
end
with_benchmark(&block) click to toggle source
# File lib/benchmark_driver/output/simple.rb, line 23
def with_benchmark(&block)
  @with_benchmark = true
  without_stdout_buffering do
    # Show header
    $stdout.puts "#{@metrics.first.name} (#{@metrics.first.unit}):"

    # Show executable names
    if @context_names.size > 1
      $stdout.print("#{' ' * @name_length}  ")
      @context_names.each do |context_name|
        $stdout.print("%#{NAME_LENGTH}s  " % context_name)
      end
      $stdout.puts
    end

    block.call
  end
ensure
  @with_benchmark = false
end
with_context(context, &block) click to toggle source

@param [BenchmarkDriver::Context] context

# File lib/benchmark_driver/output/simple.rb, line 57
def with_context(context, &block)
  block.call
end
with_job(job, &block) click to toggle source

@param [BenchmarkDriver::Job] job

# File lib/benchmark_driver/output/simple.rb, line 45
def with_job(job, &block)
  if @with_benchmark
    $stdout.print("%-#{@name_length}s  " % job.name)
  end
  block.call
ensure
  if @with_benchmark
    $stdout.puts
  end
end
with_warmup(&block) click to toggle source
# File lib/benchmark_driver/output/simple.rb, line 13
def with_warmup(&block)
  @with_benchmark = false
  without_stdout_buffering do
    $stdout.print 'warming up'
    block.call
  end
ensure
  $stdout.puts
end

Private Instance Methods

humanize(value) click to toggle source
# File lib/benchmark_driver/output/simple.rb, line 80
def humanize(value)
  if BenchmarkDriver::Result::ERROR.equal?(value)
    return " %#{NAME_LENGTH}s" % 'ERROR'
  elsif value == 0.0
    return " %#{NAME_LENGTH}.3f" % 0.0
  elsif value < 0
    raise ArgumentError.new("Negative value: #{value.inspect}")
  end

  scale = (Math.log10(value) / 3).to_i
  prefix = "%#{NAME_LENGTH}.3f" % (value.to_f / (1000 ** scale))
  suffix =
    case scale
    when 1; 'k'
    when 2; 'M'
    when 3; 'G'
    when 4; 'T'
    when 5; 'Q'
    else # < 1000 or > 10^15, no scale or suffix
      return " #{prefix}"
    end
  "#{prefix}#{suffix}"
end
without_stdout_buffering() { || ... } click to toggle source

benchmark_driver ouputs logs ASAP. This enables sync flag for it.

# File lib/benchmark_driver/output/simple.rb, line 73
def without_stdout_buffering
  sync, $stdout.sync = $stdout.sync, true
  yield
ensure
  $stdout.sync = sync
end