Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: add warmup runs #5

Merged
merged 3 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use codspeed_criterion_compat::{criterion_group, BenchmarkId, Criterion};
use codspeed_criterion_compat::{black_box, criterion_group, BenchmarkId, Criterion};

fn fibonacci_slow(n: u64) -> u64 {
match n {
Expand Down Expand Up @@ -27,14 +27,16 @@ fn fibonacci_fast(n: u64) -> u64 {
fn compare_fibonaccis(c: &mut Criterion) {
let mut group = c.benchmark_group("Fibonacci");

group.bench_with_input("Recursive", &20, |b, i| b.iter(|| fibonacci_slow(*i)));
group.bench_with_input("Recursive", &20, |b, i| {
b.iter(|| fibonacci_slow(black_box(*i)))
});
group.bench_with_input("Iterative", &20, |b, i| b.iter(|| fibonacci_fast(*i)));
}
fn compare_fibonaccis_group(c: &mut Criterion) {
let mut group = c.benchmark_group("Fibonacci3");
for i in 20..=21 {
group.bench_with_input(BenchmarkId::new("Recursive", i), &i, |b, i| {
b.iter(|| fibonacci_slow(*i))
b.iter(|| fibonacci_slow(black_box(*i)))
});
group.bench_with_input(BenchmarkId::new("Iterative", i), &i, |b, i| {
b.iter(|| fibonacci_fast(*i))
Expand Down
102 changes: 72 additions & 30 deletions crates/criterion_compat/src/compat/bencher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,17 @@ impl Bencher {
R: FnMut() -> O,
{
let mut codspeed = self.codspeed.borrow_mut();
codspeed.start_benchmark(self.uri.as_str());
black_box(routine());
codspeed.end_benchmark();
// NOTE: this structure hardens our benchmark against dead code elimination
// https://godbolt.org/z/KnYeKMd1o
for i in 0..codspeed::codspeed::WARMUP_RUNS + 1 {
if i < codspeed::codspeed::WARMUP_RUNS {
black_box(routine());
} else {
codspeed.start_benchmark(self.uri.as_str());
black_box(routine());
codspeed.end_benchmark();
}
}
}

#[inline(never)]
Expand All @@ -49,12 +57,20 @@ impl Bencher {
R: FnMut(I) -> O,
{
let mut codspeed = self.codspeed.borrow_mut();
let input = black_box(setup());
codspeed.start_benchmark(self.uri.as_str());
let output = routine(input);
codspeed.end_benchmark();

drop(black_box(output));
for i in 0..codspeed::codspeed::WARMUP_RUNS + 1 {
let input = black_box(setup());
let output = if i < codspeed::codspeed::WARMUP_RUNS {
black_box(routine(input))
} else {
let input = black_box(setup());
codspeed.start_benchmark(self.uri.as_str());
let output = black_box(routine(input));
codspeed.end_benchmark();
output
};
drop(black_box(output));
}
}

pub fn iter_with_setup<I, O, S, R>(&mut self, setup: S, routine: R)
Expand Down Expand Up @@ -87,14 +103,20 @@ impl Bencher {
R: FnMut(&mut I) -> O,
{
let mut codspeed = self.codspeed.borrow_mut();
let mut input = black_box(setup());

codspeed.start_benchmark(self.uri.as_str());
let output = routine(&mut input);
codspeed.end_benchmark();

drop(black_box(output));
drop(black_box(input));
for i in 0..codspeed::codspeed::WARMUP_RUNS + 1 {
let mut input = black_box(setup());
let output = if i < codspeed::codspeed::WARMUP_RUNS {
black_box(routine(&mut input))
} else {
codspeed.start_benchmark(self.uri.as_str());
let output = black_box(routine(&mut input));
codspeed.end_benchmark();
output
};
drop(black_box(output));
drop(black_box(input));
}
}

#[cfg(feature = "async")]
Expand All @@ -121,9 +143,15 @@ impl<'b, A: AsyncExecutor> AsyncBencher<'b, A> {
let AsyncBencher { b, runner } = self;
runner.block_on(async {
let mut codspeed = b.codspeed.borrow_mut();
codspeed.start_benchmark(b.uri.as_str());
black_box(routine().await);
codspeed.end_benchmark();
for i in 0..codspeed::codspeed::WARMUP_RUNS + 1 {
if i < codspeed::codspeed::WARMUP_RUNS {
black_box(routine().await);
} else {
codspeed.start_benchmark(b.uri.as_str());
black_box(routine().await);
codspeed.end_benchmark();
}
}
});
}

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

for i in 0..codspeed::codspeed::WARMUP_RUNS + 1 {
let input = black_box(setup());
let output = if i < codspeed::codspeed::WARMUP_RUNS {
black_box(routine(input).await)
} else {
codspeed.start_benchmark(b.uri.as_str());
let output = black_box(routine(input).await);
codspeed.end_benchmark();
output
};
drop(black_box(output));
}
})
}

Expand All @@ -203,14 +239,20 @@ 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());

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

drop(black_box(output));
drop(black_box(input));
for i in 0..codspeed::codspeed::WARMUP_RUNS + 1 {
let mut input = black_box(setup());
let output = if i < codspeed::codspeed::WARMUP_RUNS {
black_box(routine(&mut input).await)
} else {
codspeed.start_benchmark(b.uri.as_str());
let output = black_box(routine(&mut input).await);
codspeed.end_benchmark();
output
};
drop(black_box(output));
drop(black_box(input));
}
});
}
}
Loading