|
| 1 | +//! CodSpeed addition: manual control over benchmark iteration counts. |
| 2 | +//! |
| 3 | +//! `iter_manual*` lets the user pin down the exact number of measurement |
| 4 | +//! rounds, inner iterations per round, and warmup rounds. It bypasses |
| 5 | +//! Criterion's adaptive sampler entirely — see `routine.rs::sample` for the |
| 6 | +//! short-circuit. |
| 7 | +
|
| 8 | +use std::time::Instant; |
| 9 | + |
| 10 | +use codspeed::instrument_hooks::InstrumentHooks; |
| 11 | + |
| 12 | +#[cfg(feature = "async")] |
| 13 | +use crate::async_executor::AsyncExecutor; |
| 14 | +use crate::black_box; |
| 15 | +use crate::measurement::Measurement; |
| 16 | +#[cfg(feature = "async")] |
| 17 | +use crate::AsyncBencher; |
| 18 | +use crate::Bencher; |
| 19 | + |
| 20 | +#[cfg(feature = "async")] |
| 21 | +use std::future::Future; |
| 22 | + |
| 23 | +/// Options for the [`iter_manual`](Bencher::iter_manual) family. |
| 24 | +#[derive(Debug, Clone, Copy)] |
| 25 | +pub struct IterManualOptions { |
| 26 | + /// Number of measurement rounds (each produces one sample). |
| 27 | + pub rounds: u64, |
| 28 | + /// Number of routine invocations inside the measured region of each round. |
| 29 | + pub iterations: u64, |
| 30 | + /// Number of unmeasured warmup rounds run before measurement starts. |
| 31 | + pub warmup_rounds: u64, |
| 32 | +} |
| 33 | + |
| 34 | +impl IterManualOptions { |
| 35 | + /// Build options with the given rounds and iterations; `warmup_rounds` defaults to 0. |
| 36 | + pub fn new(rounds: u64, iterations: u64) -> Self { |
| 37 | + Self { |
| 38 | + rounds, |
| 39 | + iterations, |
| 40 | + warmup_rounds: 0, |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +/// Captured output of a manual run. Stored on the `Bencher` and read by |
| 46 | +/// `routine.rs::sample` to short-circuit the adaptive sampler. |
| 47 | +pub(crate) struct ManualMeasurement { |
| 48 | + /// One entry per measurement round, in nanoseconds (or whatever the |
| 49 | + /// measurement's `to_f64` returns). |
| 50 | + pub samples: Vec<f64>, |
| 51 | + /// Number of inner iterations per round. |
| 52 | + pub iterations: u64, |
| 53 | +} |
| 54 | + |
| 55 | +impl<'a, M: Measurement> Bencher<'a, M> { |
| 56 | + /// Run `routine` exactly `opts.iterations` times inside each of `opts.rounds` |
| 57 | + /// measurement rounds, optionally preceded by `opts.warmup_rounds` unmeasured rounds. |
| 58 | + /// |
| 59 | + /// Criterion's adaptive sampler is bypassed for this benchmark. |
| 60 | + #[inline(never)] |
| 61 | + pub fn iter_manual<O, R>(&mut self, opts: IterManualOptions, mut routine: R) |
| 62 | + where |
| 63 | + R: FnMut() -> O, |
| 64 | + { |
| 65 | + self.iter_manual_setup_teardown(opts, || (), |_| routine(), |_| ()); |
| 66 | + } |
| 67 | + |
| 68 | + /// Like [`iter_manual`](Self::iter_manual), with a `setup` closure producing |
| 69 | + /// fresh input for each round. The routine borrows the input mutably. |
| 70 | + #[inline(never)] |
| 71 | + pub fn iter_manual_setup<I, O, S, R>(&mut self, opts: IterManualOptions, setup: S, routine: R) |
| 72 | + where |
| 73 | + S: FnMut() -> I, |
| 74 | + R: FnMut(&mut I) -> O, |
| 75 | + { |
| 76 | + self.iter_manual_setup_teardown(opts, setup, routine, |_| ()); |
| 77 | + } |
| 78 | + |
| 79 | + /// Like [`iter_manual_setup`](Self::iter_manual_setup), with a `teardown` |
| 80 | + /// closure called after each round, outside the measured region. |
| 81 | + #[inline(never)] |
| 82 | + pub fn iter_manual_setup_teardown<I, O, S, R, T>( |
| 83 | + &mut self, |
| 84 | + opts: IterManualOptions, |
| 85 | + mut setup: S, |
| 86 | + mut routine: R, |
| 87 | + mut teardown: T, |
| 88 | + ) where |
| 89 | + S: FnMut() -> I, |
| 90 | + R: FnMut(&mut I) -> O, |
| 91 | + T: FnMut(I), |
| 92 | + { |
| 93 | + self.iterated = true; |
| 94 | + |
| 95 | + let bench_start = InstrumentHooks::current_timestamp(); |
| 96 | + let time_start = Instant::now(); |
| 97 | + |
| 98 | + for _ in 0..opts.warmup_rounds { |
| 99 | + let mut input = black_box(setup()); |
| 100 | + for _ in 0..opts.iterations { |
| 101 | + black_box(routine(&mut input)); |
| 102 | + } |
| 103 | + teardown(input); |
| 104 | + } |
| 105 | + |
| 106 | + let mut samples = Vec::with_capacity(opts.rounds as usize); |
| 107 | + for _ in 0..opts.rounds { |
| 108 | + let mut input = black_box(setup()); |
| 109 | + let start = self.measurement.start(); |
| 110 | + for _ in 0..opts.iterations { |
| 111 | + black_box(routine(&mut input)); |
| 112 | + } |
| 113 | + let value = self.measurement.end(start); |
| 114 | + teardown(input); |
| 115 | + samples.push(self.measurement.to_f64(&value)); |
| 116 | + } |
| 117 | + |
| 118 | + self.elapsed_time = time_start.elapsed(); |
| 119 | + let bench_end = InstrumentHooks::current_timestamp(); |
| 120 | + InstrumentHooks::instance().add_benchmark_timestamps(bench_start, bench_end); |
| 121 | + |
| 122 | + self.codspeed_manual = Some(ManualMeasurement { |
| 123 | + samples, |
| 124 | + iterations: opts.iterations, |
| 125 | + }); |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +#[cfg(feature = "async")] |
| 130 | +impl<'a, 'b, A: AsyncExecutor, M: Measurement> AsyncBencher<'a, 'b, A, M> { |
| 131 | + /// Async/await variant of [`Bencher::iter_manual`]. |
| 132 | + #[inline(never)] |
| 133 | + pub fn iter_manual<O, R, F>(&mut self, opts: IterManualOptions, mut routine: R) |
| 134 | + where |
| 135 | + R: FnMut() -> F, |
| 136 | + F: Future<Output = O>, |
| 137 | + { |
| 138 | + self.iter_manual_setup_teardown(opts, || (), |_| routine(), |_| std::future::ready(())); |
| 139 | + } |
| 140 | + |
| 141 | + /// Async/await variant of [`Bencher::iter_manual_setup`]. |
| 142 | + #[inline(never)] |
| 143 | + pub fn iter_manual_setup<I, O, S, R, F>( |
| 144 | + &mut self, |
| 145 | + opts: IterManualOptions, |
| 146 | + setup: S, |
| 147 | + routine: R, |
| 148 | + ) where |
| 149 | + S: FnMut() -> I, |
| 150 | + R: FnMut(&mut I) -> F, |
| 151 | + F: Future<Output = O>, |
| 152 | + { |
| 153 | + self.iter_manual_setup_teardown(opts, setup, routine, |_| std::future::ready(())); |
| 154 | + } |
| 155 | + |
| 156 | + /// Async/await variant of [`Bencher::iter_manual_setup_teardown`]. |
| 157 | + #[inline(never)] |
| 158 | + pub fn iter_manual_setup_teardown<I, O, S, R, T, RF, TF>( |
| 159 | + &mut self, |
| 160 | + opts: IterManualOptions, |
| 161 | + mut setup: S, |
| 162 | + mut routine: R, |
| 163 | + mut teardown: T, |
| 164 | + ) where |
| 165 | + S: FnMut() -> I, |
| 166 | + R: FnMut(&mut I) -> RF, |
| 167 | + T: FnMut(I) -> TF, |
| 168 | + RF: Future<Output = O>, |
| 169 | + TF: Future<Output = ()>, |
| 170 | + { |
| 171 | + let AsyncBencher { b, runner } = self; |
| 172 | + runner.block_on(async { |
| 173 | + b.iterated = true; |
| 174 | + |
| 175 | + let bench_start = InstrumentHooks::current_timestamp(); |
| 176 | + let time_start = Instant::now(); |
| 177 | + |
| 178 | + for _ in 0..opts.warmup_rounds { |
| 179 | + let mut input = black_box(setup()); |
| 180 | + for _ in 0..opts.iterations { |
| 181 | + black_box(routine(&mut input).await); |
| 182 | + } |
| 183 | + teardown(input).await; |
| 184 | + } |
| 185 | + |
| 186 | + let mut samples = Vec::with_capacity(opts.rounds as usize); |
| 187 | + for _ in 0..opts.rounds { |
| 188 | + let mut input = black_box(setup()); |
| 189 | + let start = b.measurement.start(); |
| 190 | + for _ in 0..opts.iterations { |
| 191 | + black_box(routine(&mut input).await); |
| 192 | + } |
| 193 | + let value = b.measurement.end(start); |
| 194 | + teardown(input).await; |
| 195 | + samples.push(b.measurement.to_f64(&value)); |
| 196 | + } |
| 197 | + |
| 198 | + b.elapsed_time = time_start.elapsed(); |
| 199 | + let bench_end = InstrumentHooks::current_timestamp(); |
| 200 | + InstrumentHooks::instance().add_benchmark_timestamps(bench_start, bench_end); |
| 201 | + |
| 202 | + b.codspeed_manual = Some(ManualMeasurement { |
| 203 | + samples, |
| 204 | + iterations: opts.iterations, |
| 205 | + }); |
| 206 | + }); |
| 207 | + } |
| 208 | +} |
0 commit comments