class BenchmarkDriver::Runner::Once

Run only once, for testing

def execute(*args)

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

def initialize(config:, output:, contexts:)

Parameters:
  • contexts (BenchmarkDriver::Context) --
  • output (BenchmarkDriver::Output) --
  • config (BenchmarkDriver::Config::RunnerConfig) --
def initialize(config:, output:, contexts:)
  @config = config
  @output = output
  @contexts = contexts
end

def run(jobs)

Parameters:
  • jobs (Array) --
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

def run_benchmark(job, context:)

Returns:
  • (Float) - duration

Parameters:
  • context (BenchmarkDriver::Context) --
  • job (BenchmarkDriver::Runner::Ips::Job) -- - loop_count is not nil
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? && ((value = Float(f.read)) > 0)
        value
      else
        BenchmarkDriver::Result::ERROR
      end
    end
  end
end

def with_script(script)

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