|
| 1 | +/// Test bench for custom main patterns (COD-2324). |
| 2 | +/// Verifies URI formatting when users bypass criterion_group!/criterion_main!. |
| 3 | +use codspeed_criterion_compat::{criterion_group, BenchmarkId, Criterion}; |
| 4 | + |
| 5 | +fn bench_with_group(c: &mut Criterion) { |
| 6 | + let mut group = c.benchmark_group("my_group"); |
| 7 | + group.bench_function("my_bench", |b| b.iter(|| 2 + 2)); |
| 8 | + group.bench_function(BenchmarkId::new("parameterized", 42), |b| b.iter(|| 2 + 2)); |
| 9 | + group.finish(); |
| 10 | +} |
| 11 | + |
| 12 | +criterion_group!(benches, bench_with_group); |
| 13 | + |
| 14 | +#[cfg(codspeed)] |
| 15 | +fn main() { |
| 16 | + // Pattern A: Using new_instrumented() but calling bench functions directly (not through criterion_group!) |
| 17 | + // Since criterion_group! is bypassed, there is no macro context (group/function name), |
| 18 | + // so the URI only contains the file, benchmark group, and benchmark name. |
| 19 | + // |
| 20 | + // Expected URIs: |
| 21 | + // - crates/criterion_compat/benches/custom_main.rs::my_group::my_bench |
| 22 | + // - crates/criterion_compat/benches/custom_main.rs::my_group::parameterized[42] |
| 23 | + let mut criterion = Criterion::new_instrumented(); |
| 24 | + bench_with_group(&mut criterion); |
| 25 | + |
| 26 | + // Pattern B: Calling through criterion_group!-generated function (should work correctly) |
| 27 | + // The macro captures the criterion_group! name (`benches`) and the bench function name |
| 28 | + // (`bench_with_group`), so both are part of the URI. |
| 29 | + // |
| 30 | + // Expected URIs: |
| 31 | + // - crates/criterion_compat/benches/custom_main.rs::benches::bench_with_group::my_group::my_bench |
| 32 | + // - crates/criterion_compat/benches/custom_main.rs::benches::bench_with_group::my_group::parameterized[42] |
| 33 | + let mut criterion2 = Criterion::new_instrumented(); |
| 34 | + benches(&mut criterion2); |
| 35 | +} |
| 36 | + |
| 37 | +#[cfg(not(codspeed))] |
| 38 | +fn main() { |
| 39 | + // Without codspeed, just run through upstream criterion directly |
| 40 | + let mut criterion = Criterion::default().configure_from_args(); |
| 41 | + bench_with_group(&mut criterion); |
| 42 | +} |
0 commit comments