Skip to content

Commit c70dc9b

Browse files
committed
Minor clarifications.
Add some comments, use `where` clauses, and rename some things.
1 parent 48a9a91 commit c70dc9b

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

collector/benchlib/src/benchmark.rs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@ use crate::measure::benchmark_function;
55
use crate::process::raise_process_priority;
66
use std::collections::HashMap;
77

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+
{
1014
env_logger::init();
1115

1216
let mut group = BenchmarkGroup::new();
13-
define_func(&mut group);
17+
register(&mut group);
1418
group.run().expect("Benchmark group execution has failed");
1519
}
1620

@@ -28,16 +32,21 @@ impl BenchmarkGroup {
2832
}
2933

3034
/// 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+
{
3746
// We want to type-erase the target `func` by wrapping it in a Box.
3847
let benchmark_fn = Box::new(move || benchmark_function(constructor.clone()));
3948
if self.benchmarks.insert(name, benchmark_fn).is_some() {
40-
panic!("Benchmark {} was registered twice", name);
49+
panic!("Benchmark '{}' was registered twice", name);
4150
}
4251
}
4352

@@ -108,7 +117,7 @@ impl BenchmarkGroup {
108117
macro_rules! define_benchmark {
109118
($group:expr, $name:ident, $fun:expr) => {
110119
let func = move || $fun;
111-
$group.register(stringify!($name), func);
120+
$group.register_benchmark(stringify!($name), func);
112121
};
113122
}
114123

0 commit comments

Comments
 (0)