Skip to content

feat(criterion): make Bencher implement Send + Sync #116

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
61 changes: 29 additions & 32 deletions crates/criterion_compat/src/compat/bencher.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::{cell::RefCell, rc::Rc};

use codspeed::codspeed::{black_box, CodSpeed};
use colored::Colorize;
use criterion::BatchSize;
Expand All @@ -10,36 +8,30 @@ use criterion::async_executor::AsyncExecutor;
use std::future::Future;

pub struct Bencher<'a> {
codspeed: Rc<RefCell<CodSpeed>>,
codspeed: &'a mut CodSpeed,
uri: String,
_marker: std::marker::PhantomData<&'a ()>,
}

#[allow(clippy::needless_lifetimes)]
impl<'a> Bencher<'a> {
pub fn new(codspeed: Rc<RefCell<CodSpeed>>, uri: String) -> Self {
Bencher {
codspeed,
uri,
_marker: std::marker::PhantomData,
}
pub(crate) fn new(codspeed: &'a mut CodSpeed, uri: String) -> Self {
Bencher { codspeed, uri }
}

#[inline(never)]
pub fn iter<O, R>(&mut self, mut routine: R)
where
R: FnMut() -> O,
{
let mut codspeed = self.codspeed.borrow_mut();
// 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());
self.codspeed.start_benchmark(self.uri.as_str());
black_box(routine());
codspeed.end_benchmark();
self.codspeed.end_benchmark();
}
}
}
Expand All @@ -62,17 +54,15 @@ impl<'a> Bencher<'a> {
S: FnMut() -> I,
R: FnMut(I) -> O,
{
let mut codspeed = self.codspeed.borrow_mut();

for i in 0..codspeed::codspeed::WARMUP_RUNS + 1 {
let input = black_box(setup());
let output = if i < codspeed::codspeed::WARMUP_RUNS {
routine(input)
} else {
let input = black_box(setup());
codspeed.start_benchmark(self.uri.as_str());
self.codspeed.start_benchmark(self.uri.as_str());
let output = routine(input);
codspeed.end_benchmark();
self.codspeed.end_benchmark();
output
};
drop(black_box(output));
Expand Down Expand Up @@ -108,16 +98,14 @@ impl<'a> Bencher<'a> {
S: FnMut() -> I,
R: FnMut(&mut I) -> O,
{
let mut codspeed = self.codspeed.borrow_mut();

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());
self.codspeed.start_benchmark(self.uri.as_str());
let output = black_box(routine(&mut input));
codspeed.end_benchmark();
self.codspeed.end_benchmark();
output
};
drop(black_box(output));
Expand Down Expand Up @@ -149,14 +137,13 @@ impl<'a, 'b, A: AsyncExecutor> AsyncBencher<'a, 'b, A> {
{
let AsyncBencher { b, runner } = self;
runner.block_on(async {
let mut codspeed = b.codspeed.borrow_mut();
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());
b.codspeed.start_benchmark(b.uri.as_str());
black_box(routine().await);
codspeed.end_benchmark();
b.codspeed.end_benchmark();
}
}
});
Expand Down Expand Up @@ -214,16 +201,14 @@ impl<'a, 'b, A: AsyncExecutor> AsyncBencher<'a, 'b, A> {
{
let AsyncBencher { b, runner } = self;
runner.block_on(async {
let mut codspeed = b.codspeed.borrow_mut();

for i in 0..codspeed::codspeed::WARMUP_RUNS + 1 {
let input = black_box(setup());
let output = if i < codspeed::codspeed::WARMUP_RUNS {
routine(input).await
} else {
codspeed.start_benchmark(b.uri.as_str());
b.codspeed.start_benchmark(b.uri.as_str());
let output = routine(input).await;
codspeed.end_benchmark();
b.codspeed.end_benchmark();
output
};
drop(black_box(output));
Expand All @@ -245,16 +230,14 @@ impl<'a, 'b, A: AsyncExecutor> AsyncBencher<'a, 'b, A> {
{
let AsyncBencher { b, runner } = self;
runner.block_on(async {
let mut codspeed = b.codspeed.borrow_mut();

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());
b.codspeed.start_benchmark(b.uri.as_str());
let output = black_box(routine(&mut input).await);
codspeed.end_benchmark();
b.codspeed.end_benchmark();
output
};
drop(black_box(output));
Expand All @@ -263,3 +246,17 @@ impl<'a, 'b, A: AsyncExecutor> AsyncBencher<'a, 'b, A> {
});
}
}

#[cfg(test)]
mod tests {
use super::*;

fn assert_send<T: Send>() {}
fn assert_sync<T: Sync>() {}

#[test]
fn test_auto_traits() {
assert_send::<Bencher>();
assert_sync::<Bencher>();
}
}
7 changes: 4 additions & 3 deletions crates/criterion_compat/src/compat/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ pub struct BenchmarkGroup<'a, M: Measurement = WallTime> {

#[allow(clippy::needless_lifetimes)]
impl<'a, M: Measurement> BenchmarkGroup<'a, M> {
pub fn new(criterion: &mut Criterion<M>, group_name: String) -> BenchmarkGroup<M> {
BenchmarkGroup::<M> {
pub(crate) fn new(criterion: &'a mut Criterion<M>, group_name: String) -> Self {
Self {
codspeed: criterion
.codspeed
.as_ref()
Expand Down Expand Up @@ -73,7 +73,8 @@ impl<'a, M: Measurement> BenchmarkGroup<'a, M> {
if let Some(parameter) = id.parameter {
uri = format!("{uri}[{parameter}]");
}
let mut b = Bencher::new(self.codspeed.clone(), uri);
let mut codspeed = self.codspeed.borrow_mut();
let mut b = Bencher::new(&mut codspeed, uri);
f(&mut b, input);
}
}
Expand Down
Loading