Skip to content

Commit

Permalink
feat!: add warmup runs
Browse files Browse the repository at this point in the history
  • Loading branch information
art049 committed Jul 25, 2023
1 parent f9fbacd commit 9b955a7
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
5 changes: 4 additions & 1 deletion crates/bencher_compat/src/compat.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use codspeed::codspeed::{black_box, CodSpeed};
use codspeed::codspeed::{black_box, CodSpeed, WARMUP_RUNS};

pub struct Bencher {
pub bytes: u64,
Expand All @@ -24,6 +24,9 @@ impl Bencher {
F: FnMut() -> T,
{
let uri = self.current_uri.as_str();
for _ in 0..WARMUP_RUNS {
black_box(inner());
}
self.codspeed.start_benchmark(uri);
black_box(inner());
self.codspeed.end_benchmark();
Expand Down
2 changes: 2 additions & 0 deletions crates/codspeed/src/codspeed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use colored::Colorize;

use crate::measurement;

pub const WARMUP_RUNS: u32 = 5;

//TODO: use std::hint::black_box when it's stable
pub fn black_box<T>(dummy: T) -> T {
unsafe {
Expand Down
39 changes: 36 additions & 3 deletions crates/criterion_compat/src/compat/bencher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ impl Bencher {
R: FnMut() -> O,
{
let mut codspeed = self.codspeed.borrow_mut();
for _ in 0..codspeed::codspeed::WARMUP_RUNS {
black_box(routine());
}
codspeed.start_benchmark(self.uri.as_str());
black_box(routine());
codspeed.end_benchmark();
Expand All @@ -49,6 +52,11 @@ impl Bencher {
R: FnMut(I) -> O,
{
let mut codspeed = self.codspeed.borrow_mut();
for _ in 0..codspeed::codspeed::WARMUP_RUNS {
let input = black_box(setup());
let output = routine(input);
drop(black_box(output));
}
let input = black_box(setup());
codspeed.start_benchmark(self.uri.as_str());
let output = routine(input);
Expand Down Expand Up @@ -87,8 +95,15 @@ impl Bencher {
R: FnMut(&mut I) -> O,
{
let mut codspeed = self.codspeed.borrow_mut();
let mut input = black_box(setup());

for _ in 0..codspeed::codspeed::WARMUP_RUNS {
let mut input = black_box(setup());
let output = routine(&mut input);
drop(black_box(output));
drop(black_box(input));
}

let mut input = black_box(setup());
codspeed.start_benchmark(self.uri.as_str());
let output = routine(&mut input);
codspeed.end_benchmark();
Expand Down Expand Up @@ -121,6 +136,11 @@ impl<'b, A: AsyncExecutor> AsyncBencher<'b, A> {
let AsyncBencher { b, runner } = self;
runner.block_on(async {
let mut codspeed = b.codspeed.borrow_mut();

for _ in 0..codspeed::codspeed::WARMUP_RUNS {
black_box(routine().await);
}

codspeed.start_benchmark(b.uri.as_str());
black_box(routine().await);
codspeed.end_benchmark();
Expand Down Expand Up @@ -180,6 +200,13 @@ impl<'b, A: AsyncExecutor> AsyncBencher<'b, A> {
let AsyncBencher { b, runner } = self;
runner.block_on(async {
let mut codspeed = b.codspeed.borrow_mut();

for _ in 0..codspeed::codspeed::WARMUP_RUNS {
let input = black_box(setup());
let output = routine(input).await;
drop(black_box(output));
}

let input = black_box(setup());
codspeed.start_benchmark(b.uri.as_str());
let output = routine(input).await;
Expand All @@ -203,12 +230,18 @@ impl<'b, A: AsyncExecutor> AsyncBencher<'b, A> {
let AsyncBencher { b, runner } = self;
runner.block_on(async {
let mut codspeed = b.codspeed.borrow_mut();
let mut input = black_box(setup());

for _ in 0..codspeed::codspeed::WARMUP_RUNS {
let mut input = black_box(setup());
let output = routine(&mut input).await;
drop(black_box(output));
drop(black_box(input));
}

let mut input = black_box(setup());
codspeed.start_benchmark(b.uri.as_str());
let output = routine(&mut input).await;
codspeed.end_benchmark();

drop(black_box(output));
drop(black_box(input));
});
Expand Down

0 comments on commit 9b955a7

Please sign in to comment.