1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

/// Defines a function called `$group_name` that returns the test description
/// values for the listed functions `$function`.
#[macro_export]
macro_rules! benchmark_group {
    ($group_name:ident, $($function:path),+) => {
        pub fn $group_name() -> ::std::vec::Vec<$crate::TestDescAndFn> {
            use $crate::{TestDescAndFn, TestFn, TestDesc};
            use std::borrow::Cow;
            let mut benches = ::std::vec::Vec::new();
            $(
                benches.push(TestDescAndFn {
                    desc: TestDesc {
                        name: Cow::from(stringify!($function)),
                        ignore: false,
                    },
                    testfn: TestFn::StaticBenchFn($function),
                });
            )+
            benches
        }
    };
    ($group_name:ident, $($function:path,)+) => {
        benchmark_group!($group_name, $($function),+);
    };
}


/// Define a `fn main()` that will run all benchmarks defined by the groups
/// in `$group_name`.
///
/// The main function will read the first argument from the console and use
/// it to filter the benchmarks to run.
#[macro_export]
macro_rules! benchmark_main {
    ($($group_name:path),+) => {
        fn main() {
            use $crate::TestOpts;
            use $crate::run_tests_console;
            let mut test_opts = TestOpts::default();
            // check to see if we should filter:
            if let Some(arg) = ::std::env::args().skip(1).find(|arg| *arg != "--bench") {
                test_opts.filter = Some(arg);
            }
            let mut benches = Vec::new();
            $(
                benches.extend($group_name());
            )+
            run_tests_console(&test_opts, benches).unwrap();
        }
    };
    ($($group_name:path,)+) => {
        benchmark_main!($($group_name),+);
    };
}