Skip to content

Commit d07bbd0

Browse files
authored
docs(cargo-codspeed): clarify BENCHNAME and --bench help (#195)
* docs(cargo-codspeed): clarify BENCHNAME and --bench help The BENCHNAME help claimed a substring match on benchmark names. It is an unanchored regex forwarded to each bench binary, and the matched string differs by mode: the CodSpeed URI in simulation/memory, the framework's own name in walltime. Document that, how to use --exact, and that an empty selection still exits successfully. The --bench help now mentions it can be repeated. * fix(criterion): send benchmark markers around iter_manual rounds
1 parent 832f252 commit d07bbd0

4 files changed

Lines changed: 37 additions & 11 deletions

File tree

‎crates/cargo-codspeed/src/app.rs‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ pub(crate) struct PackageFilters {
133133

134134
#[derive(Args, Clone)]
135135
pub(crate) struct BenchTargetFilters {
136-
/// Select only the specified benchmark target (all benchmark targets by default)
136+
/// Select only the specified benchmark target; can be repeated (all benchmark targets by default)
137137
#[arg(long, help_heading = TARGET_HELP)]
138138
pub(crate) bench: Option<Vec<String>>,
139139
}
@@ -200,7 +200,16 @@ enum Commands {
200200
},
201201
/// Run the previously built benchmarks
202202
Run {
203-
/// If specified, only run benches containing this string in their names
203+
/// Only run benchmarks whose name matches this unanchored regex.
204+
///
205+
/// The string matched against depends on the measurement mode:
206+
/// - **simulation/memory**: the CodSpeed URI
207+
/// - **walltime**: the benchmark framework's own name (can be listed with `-- --list`,
208+
/// and validated with `-- --list '<filter>'`)
209+
///
210+
/// To require the whole name to match instead of a regex, pass `-- --exact`. The name format
211+
/// still differs between modes.
212+
#[arg(verbatim_doc_comment)]
204213
benchname: Option<String>,
205214

206215
#[command(flatten)]

‎crates/codspeed/src/instrument_hooks/mod.rs‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,24 @@ pub use linux_impl::InstrumentHooks;
279279
#[cfg(not(use_instrument_hooks))]
280280
pub use other_impl::InstrumentHooks;
281281

282+
/// Sends the benchmark start marker when created and the stop marker when
283+
/// dropped, so every exit path closes the measured region.
284+
pub struct BenchmarkMarkers(&'static InstrumentHooks);
285+
286+
impl BenchmarkMarkers {
287+
pub fn start() -> Self {
288+
let hooks = InstrumentHooks::instance();
289+
let _ = hooks.start_benchmark();
290+
Self(hooks)
291+
}
292+
}
293+
294+
impl Drop for BenchmarkMarkers {
295+
fn drop(&mut self) {
296+
let _ = self.0.stop_benchmark();
297+
}
298+
}
299+
282300
#[cfg(test)]
283301
mod tests {
284302
use super::InstrumentHooks;

‎crates/criterion_compat/criterion_fork/src/codspeed_iter_manual.rs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
77
use std::time::{Duration, Instant};
88

9-
use codspeed::instrument_hooks::InstrumentHooks;
9+
use codspeed::instrument_hooks::{BenchmarkMarkers, InstrumentHooks};
1010

1111
#[cfg(feature = "async")]
1212
use crate::async_executor::AsyncExecutor;
@@ -112,6 +112,7 @@ impl<'a, M: Measurement> Bencher<'a, M> {
112112

113113
self.elapsed_time = Duration::ZERO;
114114
let mut samples = Vec::with_capacity(opts.rounds as usize);
115+
let markers = BenchmarkMarkers::start();
115116
for _ in 0..opts.rounds {
116117
let bench_start = InstrumentHooks::current_timestamp();
117118
let round_start = Instant::now();
@@ -126,6 +127,7 @@ impl<'a, M: Measurement> Bencher<'a, M> {
126127

127128
samples.push(self.measurement.to_f64(&value));
128129
}
130+
drop(markers);
129131

130132
self.codspeed_manual = Some(ManualMeasurement {
131133
samples,
@@ -172,6 +174,7 @@ impl<'a, 'b, A: AsyncExecutor, M: Measurement> AsyncBencher<'a, 'b, A, M> {
172174

173175
b.elapsed_time = Duration::ZERO;
174176
let mut samples = Vec::with_capacity(opts.rounds as usize);
177+
let markers = BenchmarkMarkers::start();
175178
for _ in 0..opts.rounds {
176179
let bench_start = InstrumentHooks::current_timestamp();
177180
let round_start = Instant::now();
@@ -186,6 +189,7 @@ impl<'a, 'b, A: AsyncExecutor, M: Measurement> AsyncBencher<'a, 'b, A, M> {
186189

187190
samples.push(b.measurement.to_f64(&value));
188191
}
192+
drop(markers);
189193

190194
b.codspeed_manual = Some(ManualMeasurement {
191195
samples,

‎crates/criterion_compat/criterion_fork/src/routine.rs‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use codspeed::instrument_hooks::InstrumentHooks;
1+
use codspeed::instrument_hooks::BenchmarkMarkers;
22

33
use crate::benchmark::BenchmarkConfig;
44
use crate::codspeed_iter_manual::ManualMeasurement;
@@ -238,13 +238,8 @@ pub(crate) trait Routine<M: Measurement, T: ?Sized> {
238238
}
239239

240240
let m_elapsed = {
241-
let hooks = InstrumentHooks::instance();
242-
243-
let _ = hooks.start_benchmark();
244-
let value = self.bench(measurement, &m_iters, parameter);
245-
let _ = hooks.stop_benchmark();
246-
247-
value
241+
let _markers = BenchmarkMarkers::start();
242+
self.bench(measurement, &m_iters, parameter)
248243
};
249244
let m_iters_f: Vec<f64> = m_iters.iter().map(|&x| x as f64).collect();
250245

0 commit comments

Comments
 (0)