Skip to content

Commit ec46807

Browse files
committed
test(criterion_compat): add custom main bench for URI formatting
Covers calling bench functions directly with a custom main, both with and without going through a criterion_group!-generated function.
1 parent dc08fe7 commit ec46807

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

crates/criterion_compat/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,7 @@ harness = false
6868
[[bench]]
6969
name = "test_benches"
7070
harness = false
71+
72+
[[bench]]
73+
name = "custom_main"
74+
harness = false
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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

Comments
 (0)