|
| 1 | +//! CodSpeed addition: manual control over benchmark sampling. |
| 2 | +//! |
| 3 | +//! `iter_manual` lets the user pin down the exact number of measurement rounds |
| 4 | +//! and iterations per round, bypassing criterion's adaptive sampler. See |
| 5 | +//! `routine.rs::sample` for the short-circuit that picks up the result. |
| 6 | +
|
| 7 | +use std::time::{Duration, Instant}; |
| 8 | + |
| 9 | +use codspeed::instrument_hooks::InstrumentHooks; |
| 10 | + |
| 11 | +#[cfg(feature = "async")] |
| 12 | +use crate::async_executor::AsyncExecutor; |
| 13 | +use crate::black_box; |
| 14 | +use crate::measurement::Measurement; |
| 15 | +#[cfg(feature = "async")] |
| 16 | +use crate::AsyncBencher; |
| 17 | +use crate::Bencher; |
| 18 | + |
| 19 | +#[cfg(feature = "async")] |
| 20 | +use std::future::Future; |
| 21 | + |
| 22 | +/// Options for [`Bencher::iter_manual`]. |
| 23 | +#[derive(Debug, Clone, Copy)] |
| 24 | +pub struct IterManualOptions { |
| 25 | + rounds: u64, |
| 26 | + iters: u64, |
| 27 | + warmup_rounds: u64, |
| 28 | +} |
| 29 | + |
| 30 | +impl Default for IterManualOptions { |
| 31 | + fn default() -> Self { |
| 32 | + Self { |
| 33 | + rounds: 1, |
| 34 | + iters: 1, |
| 35 | + warmup_rounds: 0, |
| 36 | + } |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +impl IterManualOptions { |
| 41 | + /// Start with defaults: 1 round, 1 iteration per round, 0 warmup rounds. |
| 42 | + pub fn new() -> Self { |
| 43 | + Self::default() |
| 44 | + } |
| 45 | + |
| 46 | + /// Number of measurement rounds (each produces one sample). |
| 47 | + #[must_use] |
| 48 | + pub fn rounds(mut self, rounds: u64) -> Self { |
| 49 | + self.rounds = rounds; |
| 50 | + self |
| 51 | + } |
| 52 | + |
| 53 | + /// Number of routine invocations inside a measurement round. |
| 54 | + #[must_use] |
| 55 | + pub fn iters(mut self, iters: u64) -> Self { |
| 56 | + self.iters = iters; |
| 57 | + self |
| 58 | + } |
| 59 | + |
| 60 | + /// Number of unmeasured warmup rounds run before measurement starts. |
| 61 | + #[must_use] |
| 62 | + pub fn warmup(mut self, warmup_rounds: u64) -> Self { |
| 63 | + self.warmup_rounds = warmup_rounds; |
| 64 | + self |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +/// Captured output of a manual run. Stored on the `Bencher` and read by |
| 69 | +/// `routine.rs::sample` to short-circuit the adaptive sampler. |
| 70 | +pub(crate) struct ManualMeasurement { |
| 71 | + /// One entry per measurement round, in the units of `Measurement::to_f64`. |
| 72 | + pub samples: Vec<f64>, |
| 73 | + /// Number of routine invocations per round. |
| 74 | + pub iterations: u64, |
| 75 | +} |
| 76 | + |
| 77 | +impl<'a, M: Measurement> Bencher<'a, M> { |
| 78 | + /// Run `routine` with a precise schedule: `opts.rounds` measurement rounds |
| 79 | + /// (each producing one sample), each consisting of `opts.iters` calls to |
| 80 | + /// `routine`. Optionally preceded by `opts.warmup_rounds` unmeasured rounds |
| 81 | + /// of the same shape. |
| 82 | + /// |
| 83 | + /// Criterion's adaptive sampler is bypassed for this benchmark. |
| 84 | + #[inline(never)] |
| 85 | + pub fn iter_manual<O, R>(&mut self, opts: IterManualOptions, routine: R) |
| 86 | + where |
| 87 | + R: FnMut() -> O, |
| 88 | + { |
| 89 | + self.__codspeed_root_frame__iter_manual(opts, routine); |
| 90 | + } |
| 91 | + |
| 92 | + #[inline(never)] |
| 93 | + #[allow(missing_docs, non_snake_case)] |
| 94 | + pub fn __codspeed_root_frame__iter_manual<O, R>( |
| 95 | + &mut self, |
| 96 | + opts: IterManualOptions, |
| 97 | + mut routine: R, |
| 98 | + ) where |
| 99 | + R: FnMut() -> O, |
| 100 | + { |
| 101 | + self.iterated = true; |
| 102 | + |
| 103 | + for _ in 0..opts.warmup_rounds { |
| 104 | + for _ in 0..opts.iters { |
| 105 | + black_box(routine()); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + self.elapsed_time = Duration::ZERO; |
| 110 | + let mut samples = Vec::with_capacity(opts.rounds as usize); |
| 111 | + for _ in 0..opts.rounds { |
| 112 | + let bench_start = InstrumentHooks::current_timestamp(); |
| 113 | + let round_start = Instant::now(); |
| 114 | + let start = self.measurement.start(); |
| 115 | + for _ in 0..opts.iters { |
| 116 | + black_box(routine()); |
| 117 | + } |
| 118 | + let value = self.measurement.end(start); |
| 119 | + self.elapsed_time += round_start.elapsed(); |
| 120 | + let bench_end = InstrumentHooks::current_timestamp(); |
| 121 | + InstrumentHooks::instance().add_benchmark_timestamps(bench_start, bench_end); |
| 122 | + |
| 123 | + samples.push(self.measurement.to_f64(&value)); |
| 124 | + } |
| 125 | + |
| 126 | + self.codspeed_manual = Some(ManualMeasurement { |
| 127 | + samples, |
| 128 | + iterations: opts.iters, |
| 129 | + }); |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +#[cfg(feature = "async")] |
| 134 | +impl<'a, 'b, A: AsyncExecutor, M: Measurement> AsyncBencher<'a, 'b, A, M> { |
| 135 | + /// Async/await variant of [`Bencher::iter_manual`]. |
| 136 | + #[inline(never)] |
| 137 | + pub fn iter_manual<O, R, F>(&mut self, opts: IterManualOptions, routine: R) |
| 138 | + where |
| 139 | + R: FnMut() -> F, |
| 140 | + F: Future<Output = O>, |
| 141 | + { |
| 142 | + self.__codspeed_root_frame__iter_manual(opts, routine); |
| 143 | + } |
| 144 | + |
| 145 | + #[inline(never)] |
| 146 | + #[allow(missing_docs, non_snake_case)] |
| 147 | + pub fn __codspeed_root_frame__iter_manual<O, R, F>( |
| 148 | + &mut self, |
| 149 | + opts: IterManualOptions, |
| 150 | + mut routine: R, |
| 151 | + ) where |
| 152 | + R: FnMut() -> F, |
| 153 | + F: Future<Output = O>, |
| 154 | + { |
| 155 | + let AsyncBencher { b, runner } = self; |
| 156 | + runner.block_on(async { |
| 157 | + b.iterated = true; |
| 158 | + |
| 159 | + for _ in 0..opts.warmup_rounds { |
| 160 | + for _ in 0..opts.iters { |
| 161 | + black_box(routine().await); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + b.elapsed_time = Duration::ZERO; |
| 166 | + let mut samples = Vec::with_capacity(opts.rounds as usize); |
| 167 | + for _ in 0..opts.rounds { |
| 168 | + let bench_start = InstrumentHooks::current_timestamp(); |
| 169 | + let round_start = Instant::now(); |
| 170 | + let start = b.measurement.start(); |
| 171 | + for _ in 0..opts.iters { |
| 172 | + black_box(routine().await); |
| 173 | + } |
| 174 | + let value = b.measurement.end(start); |
| 175 | + b.elapsed_time += round_start.elapsed(); |
| 176 | + let bench_end = InstrumentHooks::current_timestamp(); |
| 177 | + InstrumentHooks::instance().add_benchmark_timestamps(bench_start, bench_end); |
| 178 | + |
| 179 | + samples.push(b.measurement.to_f64(&value)); |
| 180 | + } |
| 181 | + |
| 182 | + b.codspeed_manual = Some(ManualMeasurement { |
| 183 | + samples, |
| 184 | + iterations: opts.iters, |
| 185 | + }); |
| 186 | + }); |
| 187 | + } |
| 188 | +} |
0 commit comments