Skip to content

Commit 304fa86

Browse files
committed
fix(criterion): send benchmark markers around iter_manual rounds
1 parent f174b9c commit 304fa86

2 files changed

Lines changed: 25 additions & 7 deletions

File tree

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use codspeed::instrument_hooks::InstrumentHooks;
1212
use crate::async_executor::AsyncExecutor;
1313
use crate::black_box;
1414
use crate::measurement::Measurement;
15+
use crate::routine::BenchmarkMarkers;
1516
#[cfg(feature = "async")]
1617
use crate::AsyncBencher;
1718
use crate::Bencher;
@@ -112,6 +113,7 @@ impl<'a, M: Measurement> Bencher<'a, M> {
112113

113114
self.elapsed_time = Duration::ZERO;
114115
let mut samples = Vec::with_capacity(opts.rounds as usize);
116+
let markers = BenchmarkMarkers::start();
115117
for _ in 0..opts.rounds {
116118
let bench_start = InstrumentHooks::current_timestamp();
117119
let round_start = Instant::now();
@@ -126,6 +128,7 @@ impl<'a, M: Measurement> Bencher<'a, M> {
126128

127129
samples.push(self.measurement.to_f64(&value));
128130
}
131+
drop(markers);
129132

130133
self.codspeed_manual = Some(ManualMeasurement {
131134
samples,
@@ -172,6 +175,7 @@ impl<'a, 'b, A: AsyncExecutor, M: Measurement> AsyncBencher<'a, 'b, A, M> {
172175

173176
b.elapsed_time = Duration::ZERO;
174177
let mut samples = Vec::with_capacity(opts.rounds as usize);
178+
let markers = BenchmarkMarkers::start();
175179
for _ in 0..opts.rounds {
176180
let bench_start = InstrumentHooks::current_timestamp();
177181
let round_start = Instant::now();
@@ -186,6 +190,7 @@ impl<'a, 'b, A: AsyncExecutor, M: Measurement> AsyncBencher<'a, 'b, A, M> {
186190

187191
samples.push(b.measurement.to_f64(&value));
188192
}
193+
drop(markers);
189194

190195
b.codspeed_manual = Some(ManualMeasurement {
191196
samples,

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

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,24 @@ use crate::{black_box, ActualSamplingMode, Bencher, Criterion};
99
use std::marker::PhantomData;
1010
use std::time::Duration;
1111

12+
/// Sends CodSpeed's benchmark start marker when created and the stop marker
13+
/// when dropped, so every exit path closes the measured region.
14+
pub(crate) struct BenchmarkMarkers(&'static InstrumentHooks);
15+
16+
impl BenchmarkMarkers {
17+
pub(crate) fn start() -> Self {
18+
let hooks = InstrumentHooks::instance();
19+
let _ = hooks.start_benchmark();
20+
Self(hooks)
21+
}
22+
}
23+
24+
impl Drop for BenchmarkMarkers {
25+
fn drop(&mut self) {
26+
let _ = self.0.stop_benchmark();
27+
}
28+
}
29+
1230
/// PRIVATE
1331
pub(crate) trait Routine<M: Measurement, T: ?Sized> {
1432
/// PRIVATE
@@ -238,13 +256,8 @@ pub(crate) trait Routine<M: Measurement, T: ?Sized> {
238256
}
239257

240258
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
259+
let _markers = BenchmarkMarkers::start();
260+
self.bench(measurement, &m_iters, parameter)
248261
};
249262
let m_iters_f: Vec<f64> = m_iters.iter().map(|&x| x as f64).collect();
250263

0 commit comments

Comments
 (0)