Jeff Vander Stoep | d036b62 | 2020-12-17 19:59:02 +0100 | [diff] [blame] | 1 | |
| 2 | /// Defines a function called `$group_name` that returns the test description |
| 3 | /// values for the listed functions `$function`. |
| 4 | #[macro_export] |
| 5 | macro_rules! benchmark_group { |
| 6 | ($group_name:ident, $($function:path),+) => { |
| 7 | pub fn $group_name() -> ::std::vec::Vec<$crate::TestDescAndFn> { |
| 8 | use $crate::{TestDescAndFn, TestFn, TestDesc}; |
| 9 | use std::borrow::Cow; |
| 10 | let mut benches = ::std::vec::Vec::new(); |
| 11 | $( |
| 12 | benches.push(TestDescAndFn { |
| 13 | desc: TestDesc { |
| 14 | name: Cow::from(stringify!($function)), |
| 15 | ignore: false, |
| 16 | }, |
| 17 | testfn: TestFn::StaticBenchFn($function), |
| 18 | }); |
| 19 | )+ |
| 20 | benches |
| 21 | } |
| 22 | }; |
| 23 | ($group_name:ident, $($function:path,)+) => { |
| 24 | benchmark_group!($group_name, $($function),+); |
| 25 | }; |
| 26 | } |
| 27 | |
| 28 | |
| 29 | /// Define a `fn main()` that will run all benchmarks defined by the groups |
| 30 | /// in `$group_name`. |
| 31 | /// |
| 32 | /// The main function will read the first argument from the console and use |
| 33 | /// it to filter the benchmarks to run. |
| 34 | #[macro_export] |
| 35 | macro_rules! benchmark_main { |
| 36 | ($($group_name:path),+) => { |
| 37 | fn main() { |
| 38 | use $crate::TestOpts; |
| 39 | use $crate::run_tests_console; |
| 40 | let mut test_opts = TestOpts::default(); |
| 41 | // check to see if we should filter: |
| 42 | if let Some(arg) = ::std::env::args().skip(1).find(|arg| *arg != "--bench") { |
| 43 | test_opts.filter = Some(arg); |
| 44 | } |
| 45 | let mut benches = Vec::new(); |
| 46 | $( |
| 47 | benches.extend($group_name()); |
| 48 | )+ |
| 49 | run_tests_console(&test_opts, benches).unwrap(); |
| 50 | } |
| 51 | }; |
| 52 | ($($group_name:path,)+) => { |
| 53 | benchmark_main!($($group_name),+); |
| 54 | }; |
| 55 | } |