Skip to content

Commit 1e10c44

Browse files
committed
Add a benchmark-interface backend to run a fixed number of iterations
1 parent d0a35ba commit 1e10c44

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

bench/benchmark-interface/lib/benchmark-interface.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
require 'benchmark-interface/frontends/mri'
1313
require 'benchmark-interface/backends/simple'
1414
require 'benchmark-interface/backends/stable'
15+
require 'benchmark-interface/backends/fixed-iterations'
1516
require 'benchmark-interface/backends/benchmark'
1617
require 'benchmark-interface/backends/bips'
1718
require 'benchmark-interface/backends/bench9000'
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved. This
2+
# code is released under a tri EPL/GPL/LGPL license. You can use it,
3+
# redistribute it and/or modify it under the terms of the:
4+
#
5+
# Eclipse Public License version 2.0, or
6+
# GNU General Public License version 2, or
7+
# GNU Lesser General Public License version 2.1.
8+
9+
module BenchmarkInterface
10+
module Backends
11+
module FixedIterations
12+
13+
def self.get_time
14+
Process.clock_gettime(Process::CLOCK_MONOTONIC)
15+
end
16+
17+
def self.run(benchmark_set, names, options)
18+
print_iterations = options['--iterations']
19+
elapsed = options['--elapsed']
20+
print_ips = options['--ips']
21+
22+
fixed_iterations = options['--fixed-iterations'].sort
23+
total_iterations = fixed_iterations.last
24+
25+
benchmark_set.benchmarks(names).each do |benchmark|
26+
puts benchmark.name
27+
block = benchmark.block
28+
29+
next_iter = fixed_iterations.shift
30+
start_time = get_time
31+
(1..total_iterations).each do |iter|
32+
block.call
33+
34+
if iter == next_iter
35+
since_start = get_time - start_time
36+
puts iter if print_iterations
37+
puts since_start if elapsed
38+
puts iter / since_start if print_ips
39+
40+
next_iter = fixed_iterations.shift
41+
end
42+
end
43+
end
44+
end
45+
46+
end
47+
end
48+
end

bench/benchmark-interface/lib/benchmark-interface/run.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ def self.run(args)
3030
'--freq' => 1,
3131
'--elapsed' => false,
3232
'--iterations' => false,
33+
'--ips' => false,
34+
'--fixed-iterations' => [],
3335
'--log' => nil,
3436
'--tag' => [],
3537
'--prop' => {}
@@ -49,6 +51,9 @@ def self.run(args)
4951
backend = BenchmarkInterface::Backends::Simple
5052
when '--stable'
5153
backend = BenchmarkInterface::Backends::Stable
54+
when '--fixed-iterations'
55+
backend = BenchmarkInterface::Backends::FixedIterations
56+
options[arg] = args[n += 1].split(',').map { |iter| Integer(iter) }
5257
when '--bips'
5358
backend = BenchmarkInterface::Backends::Bips
5459
when '--bm'
@@ -72,6 +77,8 @@ def self.run(args)
7277
n += 1
7378
when '--elapsed'
7479
options[arg] = true
80+
when '--ips'
81+
options[arg] = true
7582
when '--log'
7683
options[arg] = args[n + 1]
7784
n += 1
@@ -148,9 +155,10 @@ def self.help
148155
puts ' list list benchmarks available in the files'
149156
puts
150157
puts 'Backends:'
151-
puts ' --simple'
158+
puts ' --simple (default)'
152159
puts ' --stable'
153-
puts ' --bips (default)'
160+
puts ' --fixed-iterations ITERS1,ITERS2,...'
161+
puts ' --bips'
154162
puts ' --bm'
155163
puts ' --bmbm'
156164
puts ' --bench9000'

0 commit comments

Comments
 (0)