@@ -5,12 +5,16 @@ use crate::measure::benchmark_function;
5
5
use crate :: process:: raise_process_priority;
6
6
use std:: collections:: HashMap ;
7
7
8
- /// Create a new benchmark group. Use the closure argument to define individual benchmarks.
9
- pub fn run_benchmark_group < F : FnOnce ( & mut BenchmarkGroup ) > ( define_func : F ) {
8
+ /// Create and run a new benchmark group. Use the closure argument to register
9
+ /// the individual benchmarks.
10
+ pub fn run_benchmark_group < F > ( register : F )
11
+ where
12
+ F : FnOnce ( & mut BenchmarkGroup ) ,
13
+ {
10
14
env_logger:: init ( ) ;
11
15
12
16
let mut group = BenchmarkGroup :: new ( ) ;
13
- define_func ( & mut group) ;
17
+ register ( & mut group) ;
14
18
group. run ( ) . expect ( "Benchmark group execution has failed" ) ;
15
19
}
16
20
@@ -28,16 +32,21 @@ impl BenchmarkGroup {
28
32
}
29
33
30
34
/// Registers a single benchmark.
31
- /// `constructor` should return a closure that will be benchmarked.
32
- pub fn register < F : Fn ( ) -> Bench + Clone + ' static , R , Bench : FnOnce ( ) -> R + ' static > (
33
- & mut self ,
34
- name : & ' static str ,
35
- constructor : F ,
36
- ) {
35
+ ///
36
+ /// `constructor` returns a closure that will be benchmarked. This means
37
+ /// `constructor` can do initialization steps outside of the code that is
38
+ /// measured. `constructor` may be called multiple times (e.g. once for a
39
+ /// run with performance counters and once for a run without), but the
40
+ /// closure it produces each time will only be called once.
41
+ pub fn register_benchmark < Ctor , Bench , R > ( & mut self , name : & ' static str , constructor : Ctor )
42
+ where
43
+ Ctor : Fn ( ) -> Bench + Clone + ' static ,
44
+ Bench : FnOnce ( ) -> R + ' static ,
45
+ {
37
46
// We want to type-erase the target `func` by wrapping it in a Box.
38
47
let benchmark_fn = Box :: new ( move || benchmark_function ( constructor. clone ( ) ) ) ;
39
48
if self . benchmarks . insert ( name, benchmark_fn) . is_some ( ) {
40
- panic ! ( "Benchmark {} was registered twice" , name) ;
49
+ panic ! ( "Benchmark '{}' was registered twice" , name) ;
41
50
}
42
51
}
43
52
@@ -108,7 +117,7 @@ impl BenchmarkGroup {
108
117
macro_rules! define_benchmark {
109
118
( $group: expr, $name: ident, $fun: expr) => {
110
119
let func = move || $fun;
111
- $group. register ( stringify!( $name) , func) ;
120
+ $group. register_benchmark ( stringify!( $name) , func) ;
112
121
} ;
113
122
}
114
123
0 commit comments