
# Objective - Please see #16647 for the full reasoning behind this change. ## Solution - Create the `bench!` macro, which generates the name of the benchmark at compile time. Migrating is a single line change, and it will automatically update if you move the benchmark to a different module: ```diff + use benches::bench; fn my_benchmark(c: &mut Criterion) { - c.bench_function("my_benchmark", |b| {}); + c.bench_function(bench!("my_benchmark"), |b| {}); } ``` - Migrate all reflection benchmarks to use `bench!`. - Fix a few places where `black_box()` or Criterion is misused. ## Testing ```sh cd benches # Will take a long time! cargo bench --bench reflect # List out the names of all reflection benchmarks, to ensure I didn't miss anything. cargo bench --bench reflect -- --list # Check for linter warnings. cargo clippy --bench reflect # Run each benchmark once. cargo test --bench reflect ```
45 lines
1.6 KiB
Rust
45 lines
1.6 KiB
Rust
/// Automatically generates the qualified name of a benchmark given its function name and module
|
|
/// path.
|
|
///
|
|
/// This macro takes a single string literal as input and returns a [`&'static str`](str). Its
|
|
/// result is determined at compile-time. If you need to create variations of a benchmark name
|
|
/// based on its input, use this in combination with [`BenchmarkId`](criterion::BenchmarkId).
|
|
///
|
|
/// # When to use this
|
|
///
|
|
/// Use this macro to name benchmarks that are not within a group and benchmark groups themselves.
|
|
/// You'll most commonly use this macro with:
|
|
///
|
|
/// - [`Criterion::bench_function()`](criterion::Criterion::bench_function)
|
|
/// - [`Criterion::bench_with_input()`](criterion::Criterion::bench_with_input)
|
|
/// - [`Criterion::benchmark_group()`](criterion::Criterion::benchmark_group)
|
|
///
|
|
/// You do not want to use this macro with
|
|
/// [`BenchmarkGroup::bench_function()`](criterion::BenchmarkGroup::bench_function) or
|
|
/// [`BenchmarkGroup::bench_with_input()`](criterion::BenchmarkGroup::bench_with_input), because
|
|
/// the group they are in already has the qualified path in it.
|
|
///
|
|
/// # Example
|
|
///
|
|
/// ```
|
|
/// mod ecs {
|
|
/// mod query {
|
|
/// use criterion::Criterion;
|
|
/// use benches::bench;
|
|
///
|
|
/// fn iter(c: &mut Criterion) {
|
|
/// // Benchmark name ends in `ecs::query::iter`.
|
|
/// c.bench_function(bench!("iter"), |b| {
|
|
/// // ...
|
|
/// });
|
|
/// }
|
|
/// }
|
|
/// }
|
|
/// ```
|
|
#[macro_export]
|
|
macro_rules! bench {
|
|
($name:literal) => {
|
|
concat!(module_path!(), "::", $name)
|
|
};
|
|
}
|