codspeed_criterion_compat_walltime/macros.rs
1//! Contains macros which together define a benchmark harness that can be used
2//! in place of the standard benchmark harness. This allows the user to run
3//! Criterion.rs benchmarks with `cargo bench`.
4
5/// Macro used to define a function group for the benchmark harness; see the
6/// `criterion_main!` macro for more details.
7///
8/// This is used to define a function group; a collection of functions to call with a common
9/// Criterion configuration. Accepts two forms which can be seen below.
10///
11/// Note that the group name given here is not important, it must simply
12/// be unique. Note also that this macro is not related to the `Criterion::benchmark_group` function
13/// or the `BenchmarkGroup` type.
14///
15/// # Examples:
16///
17/// Complete form:
18///
19/// ```
20/// # #[macro_use]
21/// # extern crate criterion;
22/// # use criterion::Criterion;
23/// # fn bench_method1(c: &mut Criterion) {
24/// # }
25/// #
26/// # fn bench_method2(c: &mut Criterion) {
27/// # }
28/// #
29/// criterion_group!{
30/// name = benches;
31/// config = Criterion::default();
32/// targets = bench_method1, bench_method2
33/// }
34/// #
35/// # fn main() {}
36/// ```
37///
38/// In this form, all of the options are clearly spelled out. This expands to
39/// a function named benches, which uses the given config expression to create
40/// an instance of the Criterion struct. This is then passed by mutable
41/// reference to the targets.
42///
43/// Compact Form:
44///
45/// ```
46/// # #[macro_use]
47/// # extern crate criterion;
48/// # use criterion::Criterion;
49/// # fn bench_method1(c: &mut Criterion) {
50/// # }
51/// #
52/// # fn bench_method2(c: &mut Criterion) {
53/// # }
54/// #
55/// criterion_group!(benches, bench_method1, bench_method2);
56/// #
57/// # fn main() {}
58/// ```
59/// In this form, the first parameter is the name of the group and subsequent
60/// parameters are the target methods. The Criterion struct will be created using
61/// the `Criterion::default()` function. If you wish to customize the
62/// configuration, use the complete form and provide your own configuration
63/// function.
64#[macro_export]
65macro_rules! criterion_group {
66 (name = $name:ident; config = $config:expr; targets = $( $target:path ),+ $(,)*) => {
67 pub fn $name() {
68 let mut criterion: $crate::Criterion<_> = $config
69 .configure_from_args();
70 $(
71 if std::env::var("CODSPEED_ENV").is_ok() {
72 criterion.set_current_file($crate::abs_file!());
73 criterion.set_macro_group(format!("{}::{}", stringify!($name), stringify!($target)));
74 }
75 $target(&mut criterion);
76 )+
77 }
78 };
79 ($name:ident, $( $target:path ),+ $(,)*) => {
80 $crate::criterion_group!{
81 name = $name;
82 config = $crate::Criterion::default();
83 targets = $( $target ),+
84 }
85 }
86}
87
88/// Macro which expands to a benchmark harness.
89///
90/// Currently, using Criterion.rs requires disabling the benchmark harness
91/// generated automatically by rustc. This can be done like so:
92///
93/// ```toml
94/// [[bench]]
95/// name = "my_bench"
96/// harness = false
97/// ```
98///
99/// In this case, `my_bench` must be a rust file inside the 'benches' directory,
100/// like so:
101///
102/// `benches/my_bench.rs`
103///
104/// Since we've disabled the default benchmark harness, we need to add our own:
105///
106/// ```ignore
107/// #[macro_use]
108/// extern crate criterion;
109/// use criterion::Criterion;
110/// fn bench_method1(c: &mut Criterion) {
111/// }
112///
113/// fn bench_method2(c: &mut Criterion) {
114/// }
115///
116/// criterion_group!(benches, bench_method1, bench_method2);
117/// criterion_main!(benches);
118/// ```
119///
120/// The `criterion_main` macro expands to a `main` function which runs all of the
121/// benchmarks in the given groups.
122///
123#[macro_export]
124macro_rules! criterion_main {
125 ( $( $group:path ),+ $(,)* ) => {
126 fn main() {
127 $(
128 $group();
129 )+
130
131 $crate::Criterion::default()
132 .configure_from_args()
133 .final_summary();
134 }
135 }
136}