class BenchmarkDriver::Runner::Memory

Max resident set size

def extract_maxresident_from_time_output(output)

def extract_maxresident_from_time_output(output)
  case Etc.uname.fetch(:sysname)
  when 'Linux'
    pattern = /^(?<user>\d+.\d+)user\s+(?<system>\d+.\d+)system\s+(?<elapsed1>\d+):(?<elapsed2>\d+.\d+)elapsed.+\([^\s]+\s+(?<maxresident>\d+)maxresident\)k$/
    scale = 1000.0 # kilobytes -> bytes
  when 'Darwin'
    pattern = /^\s+(?<real>\d+\.\d+)\s+real\s+(?<user>\d+\.\d+)\s+user\s+(?<system>\d+\.\d+)\s+sys$\s+(?<maxresident>\d+)\s+maximum resident set size$/
    scale = 1.0
  end
  match_data = pattern.match(output)
  raise "Unexpected format given from /usr/bin/time:\n#{out}" unless match_data[:maxresident]
  Integer(match_data[:maxresident]) * scale
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)
  # Currently Linux's time(1) support only...
  case Etc.uname.fetch(:sysname)
  when 'Linux'
    @time_command = ['/usr/bin/time']
  when 'Darwin'
    @time_command = ['/usr/bin/time', '-l']
  else
    raise "memory output is not supported for '#{Etc.uname[:sysname]}' for now"
  end
  if jobs.any? { |job| job.loop_count.nil? }
    jobs = jobs.map do |job|
      job.loop_count ? job : Job.new(**job.to_h.merge(loop_count: 1))
    end
  end
  @output.with_benchmark do
    jobs.each do |job|
      @output.with_job(name: job.name) do
        job.runnable_contexts(@contexts).each do |context|
          result = BenchmarkDriver::Repeater.with_repeat(config: @config, larger_better: false) do
            run_benchmark(job, context: context)
          end
          @output.with_context(name: context.name, executable: context.executable, gems: context.gems, prelude: context.prelude) do
            @output.report(values: { METRIC => result.value }, all_values: { METRIC => result.all_values }, loop_count: job.loop_count)
          end
        end
      end
    end
  end
end

def run_benchmark(job, context:)

Returns:
  • (BenchmarkDriver::Metrics) -

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,
  )
  with_script(benchmark.render) do |path|
    output = IO.popen([*@time_command, *context.executable.command, path], err: [:child, :out], &:read)
    if $?.success?
      extract_maxresident_from_time_output(output)
    else
      $stdout.print(output)
      BenchmarkDriver::Result::ERROR
    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