class BenchmarkDriver::Runner::Once

Run only once, for testing

Constants

BenchmarkScript

@param [String] prelude @param [String] script @param [String] teardown @param [Integer] loop_count

Job

JobParser returns this, `BenchmarkDriver::Runner.runner_for` searches “*::Job”

JobParser

Dynamically fetched and used by `BenchmarkDriver::JobParser.parse`

METRIC

Public Class Methods

new(config:, output:, contexts:) click to toggle source

@param [BenchmarkDriver::Config::RunnerConfig] config @param [BenchmarkDriver::Output] output @param [BenchmarkDriver::Context] contexts

# File lib/benchmark_driver/runner/once.rb, line 20
def initialize(config:, output:, contexts:)
  @config = config
  @output = output
  @contexts = contexts
end

Public Instance Methods

run(jobs) click to toggle source

This method is dynamically called by `BenchmarkDriver::JobRunner.run` @param [Array<BenchmarkDriver::Default::Job>] jobs

# File lib/benchmark_driver/runner/once.rb, line 28
def run(jobs)
  jobs = jobs.map do |job|
    Job.new(job.to_h.merge(loop_count: 1)) # to show this on output
  end

  @output.with_benchmark do
    jobs.each do |job|
      @output.with_job(name: job.name) do
        job.runnable_contexts(@contexts).each do |context|
          duration = run_benchmark(job, context: context) # no repeat support
          if BenchmarkDriver::Result::ERROR.equal?(duration)
            value = BenchmarkDriver::Result::ERROR
          else
            value = 1.0 / duration
          end

          @output.with_context(name: context.name, executable: context.executable, gems: context.gems, prelude: context.prelude) do
            @output.report(values: { METRIC => value }, duration: duration, loop_count: 1)
          end
        end
      end
    end
  end
end

Private Instance Methods

execute(*args) click to toggle source
# File lib/benchmark_driver/runner/once.rb, line 91
def execute(*args)
  output = IO.popen(args, err: [:child, :out], &:read) # handle stdout?
  unless $?.success?
    raise "Failed to execute: #{args.shelljoin} (status: #{$?.exitstatus})"
  end
  output
end
run_benchmark(job, context:) click to toggle source

@param [BenchmarkDriver::Runner::Ips::Job] job - loop_count is not nil @param [BenchmarkDriver::Context] context @return [Float] duration

# File lib/benchmark_driver/runner/once.rb, line 58
def run_benchmark(job, context:)
  benchmark = BenchmarkScript.new(
    preludes:   [context.prelude, job.prelude],
    script:     job.script,
    teardown:   job.teardown,
    loop_count: job.loop_count,
  )

  Tempfile.open(['benchmark_driver-', '.rb']) do |f|
    with_script(benchmark.render(result: f.path)) do |path|
      IO.popen([*context.executable.command, path], &:read) # TODO: print stdout if verbose=2
      if $?.success?
        Float(f.read)
      else
        BenchmarkDriver::Result::ERROR
      end
    end
  end
end
with_script(script) { |path| ... } click to toggle source
# File lib/benchmark_driver/runner/once.rb, line 78
def with_script(script)
  if @config.verbose >= 2
    sep = '-' * 30
    $stdout.puts "\n\n#{sep}[Script begin]#{sep}\n#{script}#{sep}[Script end]#{sep}\n\n"
  end

  Tempfile.open(['benchmark_driver-', '.rb']) do |f|
    f.puts script
    f.close
    return yield(f.path)
  end
end