Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
macros.rs - source
[go: Go Back, main page]

criterion/
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!`](crate::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 be
12/// unique. Note also that this macro is not related to the
13/// [`Criterion::benchmark_group`](crate::Criterion::benchmark_group)  function
14/// or the [`BenchmarkGroup`](crate::BenchmarkGroup) type.
15///
16/// # Examples:
17///
18/// Complete form:
19///
20/// ```
21/// # use criterion::{criterion_group, Criterion};
22/// # fn bench_method1(c: &mut Criterion) {
23/// # }
24/// #
25/// # fn bench_method2(c: &mut Criterion) {
26/// # }
27/// #
28/// criterion_group!{
29///     name = benches;
30///     config = Criterion::default();
31///     targets = bench_method1, bench_method2
32/// }
33/// #
34/// # fn main() {}
35/// ```
36///
37/// In this form, all of the options are clearly spelled out. This expands to
38/// a function named `benches`, which uses the given config expression to create
39/// an instance of the [`Criterion`](crate::Criterion) struct. This is then
40/// passed by mutable reference to the targets.
41///
42/// Compact Form:
43///
44/// ```
45/// # use criterion::{criterion_group, Criterion};
46/// # fn bench_method1(c: &mut Criterion) {
47/// # }
48/// #
49/// # fn bench_method2(c: &mut Criterion) {
50/// # }
51/// #
52/// criterion_group!(benches, bench_method1, bench_method2);
53/// #
54/// # fn main() {}
55/// ```
56/// In this form, the first parameter is the name of the group and subsequent
57/// parameters are the target methods. The Criterion struct will be created using
58/// the `Criterion::default()` function. If you wish to customize the
59/// configuration, use the complete form and provide your own configuration
60/// function.
61#[macro_export]
62macro_rules! criterion_group {
63    (name = $name:ident; config = $config:expr; targets = $( $target:path ),+ $(,)*) => {
64        pub fn $name() {
65            let mut criterion: $crate::Criterion<_> = $config
66                .configure_from_args();
67            $(
68                $target(&mut criterion);
69            )+
70        }
71    };
72    ($name:ident, $( $target:path ),+ $(,)*) => {
73        $crate::criterion_group!{
74            name = $name;
75            config = $crate::Criterion::default();
76            targets = $( $target ),+
77        }
78    }
79}
80
81/// Macro which expands to a benchmark harness.
82///
83/// Currently, using Criterion.rs requires disabling the benchmark harness
84/// generated automatically by rustc. This can be done like so:
85///
86/// ```toml
87/// [[bench]]
88/// name = "my_bench"
89/// harness = false
90/// ```
91///
92/// In this case, `my_bench` must be a rust file inside the 'benches' directory,
93/// like so:
94///
95/// `benches/my_bench.rs`
96///
97/// Since we've disabled the default benchmark harness, we need to add our own:
98///
99/// ```no_run
100/// use criterion::{criterion_group, criterion_main, Criterion};
101/// fn bench_method1(c: &mut Criterion) {
102/// }
103///
104/// fn bench_method2(c: &mut Criterion) {
105/// }
106///
107/// criterion_group!(benches, bench_method1, bench_method2);
108/// criterion_main!(benches);
109/// ```
110///
111/// The `criterion_main` macro expands to a `main` function which runs all of the
112/// benchmarks in the given groups.
113///
114#[macro_export]
115macro_rules! criterion_main {
116    ( $( $group:path ),+ $(,)* ) => {
117        fn main() {
118            $(
119                $group();
120            )+
121
122            $crate::Criterion::default()
123                .configure_from_args()
124                .final_summary();
125        }
126    }
127}