class BenchmarkDriver::Output

BenchmarkDriver::Runner::* –> BenchmarkDriver::Output –> BenchmarkDriver::Output::*

This is interface between runner plugin and output plugin, so that they can be loosely coupled and to simplify implementation of both runner and output.

Runner should call its interface in the following manner:

metrics=
with_warmup
  with_job(name:)
    with_context(name:, executable:, gems:)
      report(values:, duration: nil, loop_count: nil, environment: {})
with_benchmark
  with_job(name:)
    with_context(name:, executable:, gems:)
      report(values:, duration: nil, loop_count: nil, environment: {})

Public Class Methods

get(type) click to toggle source

@param [String] type

# File lib/benchmark_driver/output.rb, line 24
def self.get(type)
  if type.include?(':')
    raise ArgumentError.new("Output type '#{type}' cannot contain ':'")
  end

  require "benchmark_driver/output/#{type}" # for plugin
  camelized = type.split('_').map(&:capitalize).join
  ::BenchmarkDriver::Output.const_get(camelized, false)
end
new(type:, metrics:, jobs:, contexts:, options:) click to toggle source

BenchmarkDriver::Output is pluggable. Create `BenchmarkDriver::Output::Foo` as benchmark_dirver-output-foo.gem and specify `-o foo`.

@param [String] type @param [Array<BenchmarkDriver::Metric>] metrics @param [Array<BenchmarkDriver::Job>] jobs @param [Array<BenchmarkDriver::Context>] contexts @param [Hash{ Symbol => Object }] options

# File lib/benchmark_driver/output.rb, line 42
def initialize(type:, metrics:, jobs:, contexts:, options:)
  output = ::BenchmarkDriver::Output.get(type)
  output_params = output.instance_method(:initialize).parameters.select do |type, _name|
    type == :keyreq || type == :key
  end.map(&:last)

  # Optionally pass `options` to #initialize
  kwargs = {}
  if output_params.include?(:options)
    kwargs[:options] = options
  end

  @output = output.new(
    metrics: metrics,
    jobs: jobs,
    contexts: contexts,
    **kwargs,
  )
end

Public Instance Methods

metrics=(metrics) click to toggle source

@param [Array<BenchmarkDriver::Metric>] metrics

# File lib/benchmark_driver/output.rb, line 63
def metrics=(metrics)
  @output.metrics = metrics
end
report(values:, all_values: nil, duration: nil, loop_count: nil, environment: {}) click to toggle source

@param [Hash{ BenchmarkDriver::Metric => Float }] values @param [Hash{ BenchmarkDriver::Metric => [Float] },nil] values @param [BenchmarkDriver::Metric] metic

# File lib/benchmark_driver/output.rb, line 96
def report(values:, all_values: nil, duration: nil, loop_count: nil, environment: {})
  result = BenchmarkDriver::Result.new(
    values: values,
    all_values: all_values,
    duration: duration,
    loop_count: loop_count,
    environment: environment,
  )
  @output.report(result)
end
with_benchmark(&block) click to toggle source
# File lib/benchmark_driver/output.rb, line 71
def with_benchmark(&block)
  @output.with_benchmark(&block)
end
with_context(name:, executable:, gems: {}, prelude: '', &block) click to toggle source

@param [String] name @param [BenchmarkDriver::Config::Executable] executable @param [Hash{ String => String}] gems

# File lib/benchmark_driver/output.rb, line 86
def with_context(name:, executable:, gems: {}, prelude: '', &block)
  context = BenchmarkDriver::Context.new(name: name, executable: executable, gems: gems, prelude: prelude)
  @output.with_context(context) do
    block.call
  end
end
with_job(name:, &block) click to toggle source

@param [String] name

# File lib/benchmark_driver/output.rb, line 76
def with_job(name:, &block)
  job = BenchmarkDriver::Job.new(name: name)
  @output.with_job(job) do
    block.call
  end
end
with_warmup(&block) click to toggle source
# File lib/benchmark_driver/output.rb, line 67
def with_warmup(&block)
  @output.with_warmup(&block)
end