@@ -5,8 +5,8 @@ use crate::measure::benchmark_function;
55use crate :: process:: raise_process_priority;
66use std:: collections:: HashMap ;
77
8- /// Create a new benchmark group. Use the closure argument to define benchmarks.
9- pub fn benchmark_group < F : FnOnce ( & mut BenchmarkGroup ) > ( define_func : F ) {
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 ) {
1010 env_logger:: init ( ) ;
1111
1212 let mut group = BenchmarkGroup :: new ( ) ;
@@ -19,11 +19,9 @@ struct BenchmarkWrapper {
1919 func : Box < dyn Fn ( ) -> anyhow:: Result < BenchmarkStats > > ,
2020}
2121
22- type BenchmarkMap = HashMap < & ' static str , BenchmarkWrapper > ;
23-
2422#[ derive( Default ) ]
2523pub struct BenchmarkGroup {
26- benchmarks : BenchmarkMap ,
24+ benchmarks : HashMap < & ' static str , BenchmarkWrapper > ,
2725}
2826
2927impl BenchmarkGroup {
@@ -32,7 +30,7 @@ impl BenchmarkGroup {
3230 }
3331
3432 /// Registers a single benchmark.
35- /// `func ` should return a closure that will be benchmarked.
33+ /// `constructor ` should return a closure that will be benchmarked.
3634 pub fn register < F : Fn ( ) -> Bench + Clone + ' static , R , Bench : FnOnce ( ) -> R + ' static > (
3735 & mut self ,
3836 name : & ' static str ,
@@ -55,49 +53,52 @@ impl BenchmarkGroup {
5553
5654 let args = parse_cli ( ) ?;
5755 match args {
58- Args :: Benchmark ( args) => {
59- run_benchmark ( args , self . benchmarks ) ?;
56+ Args :: Run ( args) => {
57+ self . run_benchmarks ( args ) ?;
6058 }
61- Args :: ListBenchmarks => list_benchmarks ( self . benchmarks ) ?,
59+ Args :: List => self . list_benchmarks ( ) ?,
6260 }
6361
6462 Ok ( ( ) )
6563 }
66- }
67-
68- fn list_benchmarks ( benchmarks : BenchmarkMap ) -> anyhow:: Result < ( ) > {
69- let benchmark_list: Vec < & str > = benchmarks. into_keys ( ) . collect ( ) ;
70- serde_json:: to_writer ( std:: io:: stdout ( ) , & benchmark_list) ?;
7164
72- Ok ( ( ) )
73- }
74-
75- fn run_benchmark ( args : BenchmarkArgs , benchmarks : BenchmarkMap ) -> anyhow:: Result < ( ) > {
76- let mut items: Vec < ( & ' static str , BenchmarkWrapper ) > = benchmarks
77- . into_iter ( )
78- . filter ( |( name, _) | passes_filter ( name, args. exclude . as_deref ( ) , args. include . as_deref ( ) ) )
79- . collect ( ) ;
80- items. sort_unstable_by_key ( |item| item. 0 ) ;
81-
82- let mut stdout = std:: io:: stdout ( ) . lock ( ) ;
83-
84- for ( name, def) in items {
85- let mut stats: Vec < BenchmarkStats > = Vec :: with_capacity ( args. iterations as usize ) ;
86- for i in 0 ..args. iterations {
87- let benchmark_stats = ( def. func ) ( ) ?;
88- log:: info!( "Benchmark (run {i}) `{name}` completed: {benchmark_stats:?}" ) ;
89- stats. push ( benchmark_stats) ;
65+ fn run_benchmarks ( self , args : BenchmarkArgs ) -> anyhow:: Result < ( ) > {
66+ let mut items: Vec < ( & ' static str , BenchmarkWrapper ) > = self
67+ . benchmarks
68+ . into_iter ( )
69+ . filter ( |( name, _) | {
70+ passes_filter ( name, args. exclude . as_deref ( ) , args. include . as_deref ( ) )
71+ } )
72+ . collect ( ) ;
73+ items. sort_unstable_by_key ( |item| item. 0 ) ;
74+
75+ let mut stdout = std:: io:: stdout ( ) . lock ( ) ;
76+
77+ for ( name, def) in items {
78+ let mut stats: Vec < BenchmarkStats > = Vec :: with_capacity ( args. iterations as usize ) ;
79+ for i in 0 ..args. iterations {
80+ let benchmark_stats = ( def. func ) ( ) ?;
81+ log:: info!( "Benchmark (run {i}) `{name}` completed: {benchmark_stats:?}" ) ;
82+ stats. push ( benchmark_stats) ;
83+ }
84+ output_message (
85+ & mut stdout,
86+ BenchmarkMessage :: Result ( BenchmarkResult {
87+ name : name. to_string ( ) ,
88+ stats,
89+ } ) ,
90+ ) ?;
9091 }
91- output_message (
92- & mut stdout,
93- BenchmarkMessage :: Result ( BenchmarkResult {
94- name : name. to_string ( ) ,
95- stats,
96- } ) ,
97- ) ?;
92+
93+ Ok ( ( ) )
9894 }
9995
100- Ok ( ( ) )
96+ fn list_benchmarks ( self ) -> anyhow:: Result < ( ) > {
97+ let benchmark_list: Vec < & str > = self . benchmarks . into_keys ( ) . collect ( ) ;
98+ serde_json:: to_writer ( std:: io:: stdout ( ) , & benchmark_list) ?;
99+
100+ Ok ( ( ) )
101+ }
101102}
102103
103104/// Adds a single benchmark to the benchmark group.
0 commit comments