Skip to content

Commit

Permalink
Ensure to always run at least 1 iteration for both warmup and for mea…
Browse files Browse the repository at this point in the history
…surement

* Fixes #120
* Using do-while loops to ensure the loops run at least once.
  • Loading branch information
eregon committed Jan 17, 2022
1 parent fb8010c commit b406648
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
11 changes: 5 additions & 6 deletions lib/benchmark/ips/job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,7 @@ def run_warmup
target = Timing.add_second before, @warmup / 2.0

cycles = 1
warmup_iter = 1
warmup_time_us = 0.0
while Timing.now + warmup_time_us * 2 < target
begin
t0 = Timing.now
item.call_times cycles
t1 = Timing.now
Expand All @@ -289,7 +287,7 @@ def run_warmup
# then exit the loop to avoid overflows and start the 100ms warmup runs
break if cycles >= POW_2_30
cycles *= 2
end
end while Timing.now + warmup_time_us * 2 < target

cycles = cycles_per_100ms warmup_time_us, warmup_iter
@timing[item] = cycles
Expand Down Expand Up @@ -326,7 +324,8 @@ def run_benchmark

target = Timing.add_second Timing.now, @time

while (before = Timing.now) < target
begin
before = Timing.now
item.call_times cycles
after = Timing.now

Expand All @@ -338,7 +337,7 @@ def run_benchmark
iter += cycles

measurements_us << iter_us
end
end while Timing.now < target

final_time = before

Expand Down
32 changes: 32 additions & 0 deletions test/test_benchmark_ips.rb
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,36 @@ def test_hold!
assert File.exist?(temp_file_name)
File.unlink(temp_file_name)
end

def test_small_warmup_and_time
report = Benchmark.ips do |x|
x.config(:warmup => 0.0000000001, :time => 0.001)
x.report("addition") { 1 + 2 }
end
assert_operator report.entries[0].iterations, :>=, 1

report = Benchmark.ips do |x|
x.config(:warmup => 0, :time => 0.0000000001)
x.report("addition") { 1 + 2 }
end
assert_equal 1, report.entries[0].iterations

report = Benchmark.ips do |x|
x.config(:warmup => 0.001, :time => 0.0000000001)
x.report("addition") { 1 + 2 }
end
assert_operator report.entries[0].iterations, :>=, 1

report = Benchmark.ips do |x|
x.config(:warmup => 0.0000000001, :time => 0.0000000001)
x.report("addition") { 1 + 2 }
end
assert_operator report.entries[0].iterations, :>=, 1

report = Benchmark.ips do |x|
x.config(:warmup => 0, :time => 0)
x.report("addition") { 1 + 2 }
end
assert_equal 1, report.entries[0].iterations
end
end

0 comments on commit b406648

Please sign in to comment.