divan icon indicating copy to clipboard operation
divan copied to clipboard

Support for generic benchmark groups

Open nvzqz opened this issue 2 years ago • 1 comments

Generic types and consts options should be providable through #[divan::bench_group]:

#[divan::bench_group(types = [Vec<i32>, HashSet<i32>, BTreeSet<i32>, LinkedList<i32>])]
mod group {
    #[divan::bench]
    fn bench1<T>() {}

    #[divan::bench]
    fn bench2<T>() {}
}

This would be equivalent to:

#[divan::bench_group]
mod group {
    #[divan::bench(types = [Vec<i32>, HashSet<i32>, BTreeSet<i32>, LinkedList<i32>])]
    fn bench1<T>() {}

    #[divan::bench(types = [Vec<i32>, HashSet<i32>, BTreeSet<i32>, LinkedList<i32>])]
    fn bench2<T>() {}
}

This can be achieved by having #[divan::bench_group] rewrite each #[divan::bench] to include the appropriate types or consts option. If a benchmark already has its own types or consts option, we can skip it. To automatically make types visible within the group without importing from the super scope, we could create our own type aliases in the parent to then be used via super:: on rewrite.

nvzqz avatar Oct 06 '23 07:10 nvzqz

I plan for this to land in 0.2 in the following form:

#[divan::bench_group(types = [Vec<i32>, HashSet<i32>, BTreeSet<i32>, LinkedList<i32>])]
fn group<T>(group: divan::BenchGroup) {
    group
        .bench("bench1", || { ... })
        .bench("bench2", || { ... });
}

nvzqz avatar Mar 21 '25 05:03 nvzqz