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

fix(criterion): benchmark group generics breaking change #30

Merged
merged 3 commits into from
Nov 20, 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
1 change: 1 addition & 0 deletions crates/bencher_compat/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
[![CI](https://github.com/CodSpeedHQ/codspeed-rust/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/CodSpeedHQ/codspeed-rust/actions/workflows/ci.yml)
[![Crates.io](https://img.shields.io/crates/v/codspeed-bencher-compat)](https://crates.io/crates/codspeed-bencher-compat)
[![Discord](https://img.shields.io/badge/chat%20on-discord-7289da.svg)](https://discord.com/invite/MxpaCfKSqF)
[![CodSpeed Badge](https://img.shields.io/endpoint?url=https://codspeed.io/badge.json)](https://codspeed.io/CodSpeedHQ/codspeed-rust)

Bencher compatibility layer for CodSpeed

Expand Down
1 change: 1 addition & 0 deletions crates/cargo-codspeed/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
[![CI](https://github.com/CodSpeedHQ/codspeed-rust/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/CodSpeedHQ/codspeed-rust/actions/workflows/ci.yml)
[![Crates.io](https://img.shields.io/crates/v/cargo-codspeed)](https://crates.io/crates/cargo-codspeed)
[![Discord](https://img.shields.io/badge/chat%20on-discord-7289da.svg)](https://discord.com/invite/MxpaCfKSqF)
[![CodSpeed Badge](https://img.shields.io/endpoint?url=https://codspeed.io/badge.json)](https://codspeed.io/CodSpeedHQ/codspeed-rust)

A cargo subcommand for running CodSpeed on your project

Expand Down
1 change: 1 addition & 0 deletions crates/codspeed/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
[![CI](https://github.com/CodSpeedHQ/codspeed-rust/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/CodSpeedHQ/codspeed-rust/actions/workflows/ci.yml)
[![Crates.io](https://img.shields.io/crates/v/codspeed)](https://crates.io/crates/codspeed)
[![Discord](https://img.shields.io/badge/chat%20on-discord-7289da.svg)](https://discord.com/invite/MxpaCfKSqF)
[![CodSpeed Badge](https://img.shields.io/endpoint?url=https://codspeed.io/badge.json)](https://codspeed.io/CodSpeedHQ/codspeed-rust)

The core library used to integrate with Codspeed runners

Expand Down
1 change: 1 addition & 0 deletions crates/criterion_compat/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
[![CI](https://github.com/CodSpeedHQ/codspeed-rust/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/CodSpeedHQ/codspeed-rust/actions/workflows/ci.yml)
[![Crates.io](https://img.shields.io/crates/v/codspeed-criterion-compat)](https://crates.io/crates/codspeed-criterion-compat)
[![Discord](https://img.shields.io/badge/chat%20on-discord-7289da.svg)](https://discord.com/invite/MxpaCfKSqF)
[![CodSpeed Badge](https://img.shields.io/endpoint?url=https://codspeed.io/badge.json)](https://codspeed.io/CodSpeedHQ/codspeed-rust)

Criterion.rs compatibility layer for CodSpeed

Expand Down
43 changes: 41 additions & 2 deletions crates/criterion_compat/benches/test_benches.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use codspeed_criterion_compat::{criterion_group, criterion_main, Bencher, BenchmarkId, Criterion};
use codspeed_criterion_compat::{
criterion_group, criterion_main, Bencher, BenchmarkGroup, BenchmarkId, Criterion,
};
use criterion::measurement::WallTime;

fn bench(c: &mut Criterion) {
// Setup (construct data, allocate memory, etc)
Expand Down Expand Up @@ -31,6 +34,25 @@ fn bench_with_explicit_lifetime(c: &mut Criterion) {
);
}

#[cfg(codspeed)]
fn bench_using_group_without_explicit_measurement(c: &mut Criterion) {
let mut group = c.benchmark_group("group");
fn using_group(g: &mut BenchmarkGroup) {
g.bench_function("bench_without_explicit_measurement", |b| b.iter(|| 2 + 2));
}
using_group(&mut group);
group.finish();
}

fn bench_using_group_with_explicit_measurement(c: &mut Criterion) {
let mut group = c.benchmark_group("group");
fn using_group(g: &mut BenchmarkGroup<'_, WallTime>) {
g.bench_function("bench_explicit_measurement", |b| b.iter(|| 2 + 2));
}
using_group(&mut group);
group.finish();
}

mod nested {
use super::*;
pub fn bench(c: &mut Criterion) {
Expand All @@ -48,5 +70,22 @@ mod nested {
}
}

criterion_group!(benches, bench, bench_with_explicit_lifetime, nested::bench);
criterion_group!(
benches,
bench,
bench_with_explicit_lifetime,
nested::bench,
bench_using_group_with_explicit_measurement,
);

#[cfg(not(codspeed))]
criterion_main!(benches);

#[cfg(codspeed)]
criterion_group!(
only_codspeed,
bench_using_group_without_explicit_measurement
);

#[cfg(codspeed)]
criterion_main!(benches, only_codspeed);
13 changes: 8 additions & 5 deletions crates/criterion_compat/src/compat/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@ use std::marker::PhantomData;
use std::{cell::RefCell, rc::Rc, time::Duration};

use codspeed::{codspeed::CodSpeed, utils::get_git_relative_path};
use criterion::measurement::WallTime;
use criterion::{measurement::Measurement, PlotConfiguration, SamplingMode, Throughput};

use crate::{Bencher, Criterion};

pub struct BenchmarkGroup<M: Measurement> {
/// Deprecated: using the default measurement will be removed in the next major version.
/// Defaulting to WallTime differs from the original BenchmarkGroup implementation but avoids creating a breaking change
pub struct BenchmarkGroup<'a, M: Measurement = WallTime> {
codspeed: Rc<RefCell<CodSpeed>>,
current_file: String,
macro_group: String,
group_name: String,
phantom: PhantomData<*const M>,
_marker: PhantomData<&'a M>,
}

impl<M: Measurement> BenchmarkGroup<M> {
impl<'a, M: Measurement> BenchmarkGroup<'a, M> {
pub fn new(criterion: &mut Criterion<M>, group_name: String) -> BenchmarkGroup<M> {
BenchmarkGroup::<M> {
codspeed: criterion
Expand All @@ -25,7 +28,7 @@ impl<M: Measurement> BenchmarkGroup<M> {
current_file: criterion.current_file.clone(),
macro_group: criterion.macro_group.clone(),
group_name,
phantom: PhantomData,
_marker: PhantomData,
}
}

Expand Down Expand Up @@ -76,7 +79,7 @@ impl<M: Measurement> BenchmarkGroup<M> {

// Dummy methods
#[allow(unused_variables)]
impl<M: Measurement> BenchmarkGroup<M> {
impl<'a, M: Measurement> BenchmarkGroup<'a, M> {
pub fn sample_size(&mut self, n: usize) -> &mut Self {
self
}
Expand Down