class BenchmarkDriver::Runner::CommandStdout
Use stdout of ruby command
def execute(*args)
def execute(*args) stdout, stderr, status = Open3.capture3(*args) unless status.success? raise "Failed to execute: #{args.shelljoin} (status: #{$?.exitstatus}):\n[stdout]:\n#{stdout}\n[stderr]:\n#{stderr}" end stdout end
def initialize(config:, output:, contexts:)
-
contexts
(BenchmarkDriver::Context
) -- -
output
(BenchmarkDriver::Output
) -- -
config
(BenchmarkDriver::Config::RunnerConfig
) --
def initialize(config:, output:, contexts:) @config = config @output = output @contexts = contexts end
def parse(name:, command:, working_directory: nil, metrics_type:, stdout_to_metrics:)
-
stdout_to_metrics
(String
) -- -
metrics_type
(Hash
) -- -
working_directory
(String, NilClass
) -- -
command
(String
) -- -
name
(String
) --
def parse(name:, command:, working_directory: nil, metrics_type:, stdout_to_metrics:) Job.new( name: name, command: command.shellsplit, working_directory: working_directory, metrics: parse_metrics(**metrics_type), stdout_to_metrics: stdout_to_metrics, ) end
def parse_metrics(unit:, name: nil, larger_better: nil, worse_word: nil)
def parse_metrics(unit:, name: nil, larger_better: nil, worse_word: nil) name ||= unit metric = BenchmarkDriver::Metric.new( name: name, unit: unit, larger_better: larger_better, worse_word: worse_word, ) [metric] end
def run(jobs)
-
jobs
(Array
) --
def run(jobs) metric = jobs.first.metrics.first @output.with_benchmark do jobs.each do |job| @output.with_job(name: job.name) do @contexts.each do |context| exec = context.executable result = BenchmarkDriver::Repeater.with_repeat(config: @config, larger_better: metric.larger_better) do stdout = with_chdir(job.working_directory) do with_ruby_prefix(exec) { execute(*exec.command, *job.command) } end StdoutToMetrics.new( stdout: stdout, stdout_to_metrics: job.stdout_to_metrics, ).metrics_value end @output.with_context(name: exec.name, executable: exec) do @output.report(values: { metric => result.value }, all_values: { metric => result.all_values }) end end end end end end
def with_chdir(working_directory, &block)
def with_chdir(working_directory, &block) if working_directory Dir.chdir(working_directory) { block.call } else block.call end end
def with_ruby_prefix(executable, &block)
def with_ruby_prefix(executable, &block) env = ENV.to_h.dup ENV['PATH'] = "#{File.dirname(executable.command.first)}:#{ENV['PATH']}" block.call ensure ENV.replace(env) end