Tests: calibrate before and after each round's frames

Each round set its budget from a calibration measured only before its
frames, so contention during that calibration could raise the budget up
to three times. The workload now also runs after the frames, and the
faster of the two sets the round's budget. The PERF lines show both.
This commit is contained in:
dotCooCoo
2026-11-03 16:31:58 -08:00
parent 3c438c9b0a
commit 17dca72d78
+22 -5
View File
@@ -63,16 +63,29 @@ fn calibration_average() -> std::time::Duration {
best best
} }
/// Measure calibration just before each frame round. The lowest average-to-budget ratio determines the result. /// A round's calibration: the faster of the runs before and after its frames.
fn round_calibration(before: std::time::Duration, after: std::time::Duration) -> std::time::Duration {
before.min(after)
}
#[test]
fn contention_during_one_calibration_does_not_raise_the_budget() {
let quiet = std::time::Duration::from_millis(17);
let contended = std::time::Duration::from_millis(60);
assert_eq!(budget_scale(round_calibration(contended, quiet)), 1.0);
assert_eq!(budget_scale(round_calibration(quiet, contended)), 1.0);
assert_eq!(budget_scale(round_calibration(contended, contended)), 3.0);
}
/// Measure calibration just before and just after each frame round, and set that round's budget from
/// the faster of the two. The lowest average-to-budget ratio determines the result.
/// Later rounds run only when the measured average exceeds that round's calibrated budget. /// Later rounds run only when the measured average exceeds that round's calibrated budget.
fn best_average(label: &str, frames: usize, mut step: impl FnMut(usize)) -> (std::time::Duration, std::time::Duration) { fn best_average(label: &str, frames: usize, mut step: impl FnMut(usize)) -> (std::time::Duration, std::time::Duration) {
let mut best = std::time::Duration::MAX; let mut best = std::time::Duration::MAX;
let mut best_budget = BUDGET; let mut best_budget = BUDGET;
let mut best_ratio = f64::INFINITY; let mut best_ratio = f64::INFINITY;
for round in 1..=3 { for round in 1..=3 {
let calibration = calibration_average(); let before = calibration_average();
let scale = budget_scale(calibration);
let budget = BUDGET.mul_f64(scale);
let start = std::time::Instant::now(); let start = std::time::Instant::now();
let mut worst = std::time::Duration::ZERO; let mut worst = std::time::Duration::ZERO;
for f in 0..frames { for f in 0..frames {
@@ -81,8 +94,12 @@ fn best_average(label: &str, frames: usize, mut step: impl FnMut(usize)) -> (std
worst = worst.max(t.elapsed()); worst = worst.max(t.elapsed());
} }
let avg = start.elapsed() / frames as u32; let avg = start.elapsed() / frames as u32;
let after = calibration_average();
let calibration = round_calibration(before, after);
let scale = budget_scale(calibration);
let budget = BUDGET.mul_f64(scale);
eprintln!( eprintln!(
"PERF {label} round {round}: calibration {calibration:?}, factor {scale:.2}, budget {budget:?}, avg frame {avg:?}, worst {worst:?}" "PERF {label} round {round}: calibration {before:?} before, {after:?} after, factor {scale:.2}, budget {budget:?}, avg frame {avg:?}, worst {worst:?}"
); );
let ratio = avg.as_secs_f64() / budget.as_secs_f64(); let ratio = avg.as_secs_f64() / budget.as_secs_f64();
if ratio < best_ratio { if ratio < best_ratio {