Skip to content

Commit 98dc856

Browse files
committed
Address review comments
1 parent 1a633b0 commit 98dc856

File tree

8 files changed

+56
-53
lines changed

8 files changed

+56
-53
lines changed

collector/benchlib/src/benchmark.rs

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ 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 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)]
2523
pub struct BenchmarkGroup {
26-
benchmarks: BenchmarkMap,
24+
benchmarks: HashMap<&'static str, BenchmarkWrapper>,
2725
}
2826

2927
impl 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.

collector/benchlib/src/cli.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ use clap::{FromArgMatches, IntoApp};
33
#[derive(clap::Parser, Debug)]
44
pub enum Args {
55
/// Benchmark all benchmarks in this benchmark group and print the results as JSON.
6-
Benchmark(BenchmarkArgs),
6+
Run(BenchmarkArgs),
77
/// List benchmarks that are defined in the current group as a JSON array.
8-
ListBenchmarks,
8+
List,
99
}
1010

1111
#[derive(clap::Parser, Debug)]

collector/benchlib/src/comm/messages.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub enum BenchmarkMessage {
88
Result(BenchmarkResult),
99
}
1010

11-
/// Stats gathered by several executions of a single benchmark.
11+
/// Stats gathered by several iterations of a single benchmark.
1212
#[derive(Debug, serde::Serialize, serde::Deserialize)]
1313
pub struct BenchmarkResult {
1414
pub name: String,

collector/runtime-benchmarks/bufreader/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::io::{BufRead, BufReader, Write};
22

33
use snap::{read::FrameDecoder, write::FrameEncoder};
44

5-
use benchlib::benchmark::{benchmark_group, black_box};
5+
use benchlib::benchmark::{black_box, run_benchmark_group};
66
use benchlib::define_benchmark;
77

88
const BYTES: usize = 64 * 1024 * 1024;
@@ -11,7 +11,7 @@ fn main() {
1111
// Inspired by https://github.com/rust-lang/rust/issues/102727
1212
// The pattern we want is a BufReader which wraps a Read impl where one Read::read call will
1313
// never fill the whole BufReader buffer.
14-
benchmark_group(|group| {
14+
run_benchmark_group(|group| {
1515
define_benchmark!(group, bufreader_snappy, {
1616
let data = vec![0u8; BYTES];
1717
move || {

collector/runtime-benchmarks/hashmap/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use benchlib;
2-
use benchlib::benchmark::benchmark_group;
2+
use benchlib::benchmark::run_benchmark_group;
33
use benchlib::define_benchmark;
44

55
fn main() {
6-
benchmark_group(|group| {
6+
run_benchmark_group(|group| {
77
// Measures how long does it take to insert 10 thousand numbers into a `hashbrown` hashmap.
88
define_benchmark!(group, hashmap_insert_10k, {
99
let mut map = hashbrown::HashMap::with_capacity_and_hasher(

collector/runtime-benchmarks/nbody/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
//! Calculates the N-body simulation.
22
//! Code taken from https://github.com/prestontw/rust-nbody
33
4-
use benchlib::benchmark::benchmark_group;
4+
use benchlib::benchmark::run_benchmark_group;
55
use benchlib::define_benchmark;
66

77
mod nbody;
88

99
fn main() {
10-
benchmark_group(|group| {
10+
run_benchmark_group(|group| {
1111
define_benchmark!(group, nbody_10k, {
1212
let mut nbody_10k = nbody::init(10000);
1313
|| {

collector/src/runtime/benchmark.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ use core::result::Result::Ok;
66
use std::path::{Path, PathBuf};
77
use std::process::Command;
88

9-
/// A binary that defines several benchmarks using the `benchmark_group` function from `benchlib`.
9+
/// A binary that defines several benchmarks using the `run_benchmark_group` function from
10+
/// `benchlib`.
1011
#[derive(Debug)]
1112
pub struct BenchmarkGroup {
1213
pub binary: PathBuf,
@@ -29,6 +30,7 @@ impl BenchmarkSuite {
2930
pub fn total_benchmark_count(&self) -> u64 {
3031
self.benchmark_names().count() as u64
3132
}
33+
3234
pub fn filtered_benchmark_count(&self, filter: &BenchmarkFilter) -> u64 {
3335
self.benchmark_names()
3436
.filter(|benchmark| {
@@ -96,9 +98,9 @@ pub fn discover_benchmarks(cargo_stdout: &[u8]) -> anyhow::Result<BenchmarkSuite
9698
Ok(BenchmarkSuite { groups })
9799
}
98100

99-
/// Uses the `list-benchmarks` command from `benchlib` to find the benchmark names from the given
101+
/// Uses a command from `benchlib` to find the benchmark names from the given
100102
/// benchmark binary.
101103
fn gather_benchmarks(binary: &Path) -> anyhow::Result<Vec<String>> {
102-
let output = Command::new(binary).arg("list-benchmarks").output()?;
104+
let output = Command::new(binary).arg("list").output()?;
103105
Ok(serde_json::from_slice(&output.stdout)?)
104106
}

collector/src/runtime/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ fn execute_runtime_benchmark_binary(
7171
command.arg(std::env::consts::ARCH);
7272
command.arg("-R");
7373
command.arg(binary);
74-
command.arg("benchmark");
74+
command.arg("run");
7575

7676
if let Some(ref exclude) = filter.exclude {
7777
command.args(&["--exclude", exclude]);

0 commit comments

Comments
 (0)