use std::fmt;
use std::time::{Duration, Instant};
const SIZE_FACTOR: u32 = 8;
const SAMPLES: usize = 3;
const MEASUREMENTS: usize = 2;
const NOISE_FLOOR: Duration = Duration::from_millis(100);
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
struct TimeRatio(f64);
impl TimeRatio {
fn of(small: Duration, full: Duration) -> Self {
Self(full.as_secs_f64() / small.as_secs_f64().max(f64::EPSILON))
}
}
impl fmt::Display for TimeRatio {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:.1}x", self.0)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ExpectedGrowth {
Linear,
Linearithmic,
Bounded,
}
impl ExpectedGrowth {
fn max_ratio(self) -> TimeRatio {
let size_factor = f64::from(SIZE_FACTOR);
TimeRatio(match self {
Self::Linear => size_factor * 3.0,
Self::Linearithmic => size_factor * 5.0,
Self::Bounded => size_factor / 2.0,
})
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Size {
Small,
Full,
}
#[derive(Debug, Clone, Copy, PartialEq)]
enum Verdict {
Pass,
BelowFloor,
Fail {
small: Duration,
full: Duration,
ratio: TimeRatio,
},
}
fn judge(
expected: ExpectedGrowth,
noise_floor: Duration,
mut sample: impl FnMut(Size) -> Duration,
) -> Verdict {
let mut best: Option<Verdict> = None;
for _ in 0..MEASUREMENTS {
let mut small = Duration::MAX;
let mut full = Duration::MAX;
for _ in 0..SAMPLES {
small = small.min(sample(Size::Small));
full = full.min(sample(Size::Full));
}
if full < noise_floor {
return Verdict::BelowFloor;
}
let ratio = TimeRatio::of(small, full);
if ratio <= expected.max_ratio() {
return Verdict::Pass;
}
let candidate = Verdict::Fail { small, full, ratio };
best = match best {
Some(Verdict::Fail { ratio: kept, .. }) if kept <= ratio => best,
_ => Some(candidate),
};
}
best.expect("MEASUREMENTS is non-zero")
}
pub fn assert_scaling<I, R>(
label: &str,
expected: ExpectedGrowth,
full_units: usize,
prepare: impl Fn(usize) -> I,
run: impl Fn(&I) -> R,
) -> R {
let small_input = prepare((full_units / SIZE_FACTOR as usize).max(1));
let full_input = prepare(full_units);
let mut result = None;
let verdict = judge(expected, NOISE_FLOOR, |size| {
let input = match size {
Size::Small => &small_input,
Size::Full => &full_input,
};
let start = Instant::now();
let out = run(input);
let elapsed = start.elapsed();
if size == Size::Full {
result = Some(out);
}
elapsed
});
if let Verdict::Fail { small, full, ratio } = verdict {
panic!(
"{label}: {SIZE_FACTOR}x the input took {ratio} the time ({small:?} -> {full:?}), \
expected {expected:?} growth (max ratio {})",
expected.max_ratio()
);
}
result.expect("the full-size input is always sampled")
}
#[cfg(test)]
mod tests {
use super::*;
use std::assert_matches;
fn scripted(
rounds: &[(u64, u64)],
) -> (
impl FnMut(Size) -> Duration + '_,
std::rc::Rc<std::cell::RefCell<Vec<Size>>>,
) {
let calls = std::rc::Rc::new(std::cell::RefCell::new(Vec::new()));
let log = std::rc::Rc::clone(&calls);
let sample = move |size: Size| {
let index = log.borrow().len();
log.borrow_mut().push(size);
let round = (index / (2 * SAMPLES)).min(rounds.len() - 1);
let (small, full) = rounds[round];
Duration::from_millis(match size {
Size::Small => small,
Size::Full => full,
})
};
(sample, calls)
}
fn verdict(expected: ExpectedGrowth, rounds: &[(u64, u64)]) -> Verdict {
judge(expected, Duration::from_millis(1), scripted(rounds).0)
}
#[test]
fn samples_are_interleaved_small_then_full() {
let (sample, calls) = scripted(&[(10, 80)]);
judge(ExpectedGrowth::Linear, Duration::from_millis(1), sample);
let expected: Vec<Size> = (0..SAMPLES)
.flat_map(|_| [Size::Small, Size::Full])
.collect();
assert_eq!(*calls.borrow(), expected);
}
#[test]
fn healthy_ratio_passes_each_growth() {
assert_eq!(verdict(ExpectedGrowth::Linear, &[(10, 80)]), Verdict::Pass);
assert_eq!(
verdict(ExpectedGrowth::Linearithmic, &[(10, 130)]),
Verdict::Pass
);
assert_eq!(verdict(ExpectedGrowth::Bounded, &[(10, 12)]), Verdict::Pass);
}
#[test]
fn a_ratio_over_the_limit_on_every_measurement_fails() {
let quadratic = [(10, 640)];
assert_matches!(
verdict(ExpectedGrowth::Linear, &quadratic),
Verdict::Fail { .. }
);
assert_matches!(
verdict(ExpectedGrowth::Linearithmic, &[(10, 1000)]),
Verdict::Fail { .. }
);
assert_matches!(
verdict(ExpectedGrowth::Bounded, &[(10, 80)]),
Verdict::Fail { .. }
);
}
#[test]
fn a_transient_spike_is_remeasured_and_passes() {
assert_eq!(
verdict(ExpectedGrowth::Linear, &[(10, 400), (10, 80)]),
Verdict::Pass
);
}
#[test]
fn the_smaller_failing_ratio_is_reported() {
let Verdict::Fail { ratio, .. } = verdict(ExpectedGrowth::Linear, &[(10, 900), (10, 640)])
else {
panic!("expected a failure");
};
assert_eq!(ratio, TimeRatio(64.0));
}
#[test]
fn a_full_run_below_the_noise_floor_is_not_compared() {
assert_eq!(
judge(
ExpectedGrowth::Bounded,
Duration::from_secs(60),
scripted(&[(1, 900)]).0
),
Verdict::BelowFloor
);
}
#[test]
fn assert_scaling_returns_the_full_run_result() {
let result = assert_scaling(
"tiny",
ExpectedGrowth::Linear,
100,
|units| units,
|&units| units * 2,
);
assert_eq!(result, 200);
}
}