Skip to content

Commit 9b955a7

Browse files
committed
feat!: add warmup runs
1 parent f9fbacd commit 9b955a7

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

crates/bencher_compat/src/compat.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use codspeed::codspeed::{black_box, CodSpeed};
1+
use codspeed::codspeed::{black_box, CodSpeed, WARMUP_RUNS};
22

33
pub struct Bencher {
44
pub bytes: u64,
@@ -24,6 +24,9 @@ impl Bencher {
2424
F: FnMut() -> T,
2525
{
2626
let uri = self.current_uri.as_str();
27+
for _ in 0..WARMUP_RUNS {
28+
black_box(inner());
29+
}
2730
self.codspeed.start_benchmark(uri);
2831
black_box(inner());
2932
self.codspeed.end_benchmark();

crates/codspeed/src/codspeed.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ use colored::Colorize;
44

55
use crate::measurement;
66

7+
pub const WARMUP_RUNS: u32 = 5;
8+
79
//TODO: use std::hint::black_box when it's stable
810
pub fn black_box<T>(dummy: T) -> T {
911
unsafe {

crates/criterion_compat/src/compat/bencher.rs

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ impl Bencher {
2525
R: FnMut() -> O,
2626
{
2727
let mut codspeed = self.codspeed.borrow_mut();
28+
for _ in 0..codspeed::codspeed::WARMUP_RUNS {
29+
black_box(routine());
30+
}
2831
codspeed.start_benchmark(self.uri.as_str());
2932
black_box(routine());
3033
codspeed.end_benchmark();
@@ -49,6 +52,11 @@ impl Bencher {
4952
R: FnMut(I) -> O,
5053
{
5154
let mut codspeed = self.codspeed.borrow_mut();
55+
for _ in 0..codspeed::codspeed::WARMUP_RUNS {
56+
let input = black_box(setup());
57+
let output = routine(input);
58+
drop(black_box(output));
59+
}
5260
let input = black_box(setup());
5361
codspeed.start_benchmark(self.uri.as_str());
5462
let output = routine(input);
@@ -87,8 +95,15 @@ impl Bencher {
8795
R: FnMut(&mut I) -> O,
8896
{
8997
let mut codspeed = self.codspeed.borrow_mut();
90-
let mut input = black_box(setup());
9198

99+
for _ in 0..codspeed::codspeed::WARMUP_RUNS {
100+
let mut input = black_box(setup());
101+
let output = routine(&mut input);
102+
drop(black_box(output));
103+
drop(black_box(input));
104+
}
105+
106+
let mut input = black_box(setup());
92107
codspeed.start_benchmark(self.uri.as_str());
93108
let output = routine(&mut input);
94109
codspeed.end_benchmark();
@@ -121,6 +136,11 @@ impl<'b, A: AsyncExecutor> AsyncBencher<'b, A> {
121136
let AsyncBencher { b, runner } = self;
122137
runner.block_on(async {
123138
let mut codspeed = b.codspeed.borrow_mut();
139+
140+
for _ in 0..codspeed::codspeed::WARMUP_RUNS {
141+
black_box(routine().await);
142+
}
143+
124144
codspeed.start_benchmark(b.uri.as_str());
125145
black_box(routine().await);
126146
codspeed.end_benchmark();
@@ -180,6 +200,13 @@ impl<'b, A: AsyncExecutor> AsyncBencher<'b, A> {
180200
let AsyncBencher { b, runner } = self;
181201
runner.block_on(async {
182202
let mut codspeed = b.codspeed.borrow_mut();
203+
204+
for _ in 0..codspeed::codspeed::WARMUP_RUNS {
205+
let input = black_box(setup());
206+
let output = routine(input).await;
207+
drop(black_box(output));
208+
}
209+
183210
let input = black_box(setup());
184211
codspeed.start_benchmark(b.uri.as_str());
185212
let output = routine(input).await;
@@ -203,12 +230,18 @@ impl<'b, A: AsyncExecutor> AsyncBencher<'b, A> {
203230
let AsyncBencher { b, runner } = self;
204231
runner.block_on(async {
205232
let mut codspeed = b.codspeed.borrow_mut();
206-
let mut input = black_box(setup());
207233

234+
for _ in 0..codspeed::codspeed::WARMUP_RUNS {
235+
let mut input = black_box(setup());
236+
let output = routine(&mut input).await;
237+
drop(black_box(output));
238+
drop(black_box(input));
239+
}
240+
241+
let mut input = black_box(setup());
208242
codspeed.start_benchmark(b.uri.as_str());
209243
let output = routine(&mut input).await;
210244
codspeed.end_benchmark();
211-
212245
drop(black_box(output));
213246
drop(black_box(input));
214247
});

0 commit comments

Comments
 (0)