use std::hint::black_box;
use std::io;
use std::path::PathBuf;
use std::time::{Duration, Instant};
use sha2::{Digest, Sha256};
pub const BOOTSTRAP_RESAMPLES: usize = 2_000;
pub const NULL_FLOOR_MARGIN: f64 = 2.0;
pub const NULL_MEDIAN_TOLERANCE: f64 = 0.02;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BenchExecutableIdentity {
pub sha256: String,
pub bytes: usize,
pub path: PathBuf,
}
pub fn print_bench_elf_sha256() -> io::Result<BenchExecutableIdentity> {
let path = std::env::current_exe()?;
let executable = std::fs::read(&path)?;
let identity = BenchExecutableIdentity {
sha256: lower_hex(&Sha256::digest(&executable)),
bytes: executable.len(),
path,
};
println!(
"bench_elf_sha256={} ({} bytes) {}",
identity.sha256,
identity.bytes,
identity.path.display()
);
Ok(identity)
}
#[derive(Debug, Clone, Copy)]
pub struct PairedRatio {
pub median: f64,
pub median_ci95_low: f64,
pub median_ci95_high: f64,
pub p5: f64,
pub p95: f64,
pub rounds: usize,
}
impl PairedRatio {
#[must_use]
pub fn is_admissible_null(&self) -> bool {
self.rounds >= 10
&& self.median.is_finite()
&& self.median_ci95_low.is_finite()
&& self.median_ci95_high.is_finite()
&& self.median_ci95_low <= self.median_ci95_high
&& (self.median - 1.0).abs() <= NULL_MEDIAN_TOLERANCE
}
#[must_use]
pub fn null_half_width(&self) -> f64 {
(self.median_ci95_low - 1.0)
.abs()
.max((self.median_ci95_high - 1.0).abs())
}
#[must_use]
pub fn decidable_against(&self, null: &Self) -> bool {
null.is_admissible_null()
&& (self.median - 1.0).abs() >= NULL_FLOOR_MARGIN * null.null_half_width()
}
}
fn time_batch<F: FnMut()>(inner: u32, f: &mut F) -> Duration {
let t = Instant::now();
for _ in 0..inner {
f();
}
t.elapsed()
}
#[must_use]
pub fn paired_median_ratio<A: FnMut(), B: FnMut()>(
rounds: usize,
inner: u32,
mut a: A,
mut b: B,
) -> PairedRatio {
assert!(rounds > 0 && inner > 0, "rounds and inner must be non-zero");
for _ in 0..2 {
black_box(time_batch(inner, &mut a));
black_box(time_batch(inner, &mut b));
}
let mut ratios: Vec<f64> = Vec::with_capacity(rounds);
for r in 0..rounds {
let (ta, tb) = if r % 2 == 0 {
let ta = time_batch(inner, &mut a);
let tb = time_batch(inner, &mut b);
(ta, tb)
} else {
let tb = time_batch(inner, &mut b);
let ta = time_batch(inner, &mut a);
(ta, tb)
};
let ta = ta.as_secs_f64();
if ta > 0.0 {
ratios.push(tb.as_secs_f64() / ta);
}
}
assert!(
!ratios.is_empty(),
"no round produced a positive base timing"
);
ratios.sort_unstable_by(f64::total_cmp);
let n = ratios.len();
let (median_ci95_low, median_ci95_high) = bootstrap_median_ci95(&ratios);
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
let pct = |q: f64| ratios[((n - 1) as f64 * q).round() as usize];
PairedRatio {
median: pct(0.5),
median_ci95_low,
median_ci95_high,
p5: pct(0.05),
p95: pct(0.95),
rounds: n,
}
}
fn bootstrap_median_ci95(samples: &[f64]) -> (f64, f64) {
debug_assert!(!samples.is_empty());
let sample_count = u64::try_from(samples.len()).expect("sample count fits u64");
let mut seed = 0x6a09_e667_f3bc_c909_u64 ^ sample_count;
for sample in samples {
seed = splitmix64(seed ^ sample.to_bits());
}
let mut resample = Vec::with_capacity(samples.len());
let mut medians = Vec::with_capacity(BOOTSTRAP_RESAMPLES);
for _ in 0..BOOTSTRAP_RESAMPLES {
resample.clear();
for _ in 0..samples.len() {
seed = splitmix64(seed);
let index = usize::try_from(seed % sample_count).expect("sample modulus fits usize");
resample.push(samples[index]);
}
resample.sort_unstable_by(f64::total_cmp);
medians.push(percentile(&resample, 0.50));
}
medians.sort_unstable_by(f64::total_cmp);
(percentile(&medians, 0.025), percentile(&medians, 0.975))
}
fn percentile(sorted: &[f64], quantile: f64) -> f64 {
debug_assert!(!sorted.is_empty());
let upper = sorted.len() - 1;
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
let index = (upper as f64 * quantile).round() as usize;
sorted[index]
}
const fn splitmix64(mut value: u64) -> u64 {
value = value.wrapping_add(0x9e37_79b9_7f4a_7c15);
value = (value ^ (value >> 30)).wrapping_mul(0xbf58_476d_1ce4_e5b9);
value = (value ^ (value >> 27)).wrapping_mul(0x94d0_49bb_1331_11eb);
value ^ (value >> 31)
}
fn lower_hex(bytes: &[u8]) -> String {
const DIGITS: &[u8; 16] = b"0123456789abcdef";
let mut output = String::with_capacity(bytes.len() * 2);
for byte in bytes {
output.push(char::from(DIGITS[usize::from(byte >> 4)]));
output.push(char::from(DIGITS[usize::from(byte & 0x0f)]));
}
output
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn null_control_of_identical_work_is_near_one() {
let work = || {
let mut acc = 0u64;
for i in 0..2_000u64 {
acc = acc.wrapping_add(black_box(i).wrapping_mul(2_654_435_761));
}
black_box(acc);
};
let null = paired_median_ratio(41, 8, work, work);
assert!(
null.median > 0.75 && null.median < 1.33,
"A/A null median {} strayed far from 1.0",
null.median
);
assert!(null.median_ci95_low <= null.median);
assert!(null.median <= null.median_ci95_high);
assert!(null.p5 <= null.median && null.median <= null.p95);
assert_eq!(null.rounds, 41);
}
#[test]
fn a_large_effect_is_decidable_against_the_null() {
let base = || {
let mut acc = 0u64;
for i in 0..2_000u64 {
acc = acc.wrapping_add(black_box(i).wrapping_mul(2_654_435_761));
}
black_box(acc);
};
let cand = || {
let mut acc = 0u64;
for i in 0..8_000u64 {
acc = acc.wrapping_add(black_box(i).wrapping_mul(2_654_435_761));
}
black_box(acc);
};
let null = paired_median_ratio(41, 8, base, base);
let lever = paired_median_ratio(41, 8, base, cand);
assert!(lever.median > 2.0, "expected ~4x, got {}", lever.median);
if null.is_admissible_null() {
assert!(
lever.decidable_against(&null),
"4x effect (median {}) should clear the null median CI [{}, {}]",
lever.median,
null.median_ci95_low,
null.median_ci95_high
);
} else {
assert!(
!lever.decidable_against(&null),
"an inadmissible A/A control must quarantine even a large effect"
);
}
}
#[test]
fn a_precise_null_is_admissible_even_though_its_ci_excludes_one() {
let precise = PairedRatio {
median: 1.0004,
median_ci95_low: 1.0001,
median_ci95_high: 1.0008,
p5: 0.9990,
p95: 1.0020,
rounds: 641,
};
assert!(
!(precise.median_ci95_low <= 1.0 && 1.0 <= precise.median_ci95_high),
"fixture must exclude 1.0 or it does not exercise the retired clause"
);
assert!(precise.is_admissible_null());
let small_effect = PairedRatio {
median: 0.998,
..precise
};
assert!(small_effect.decidable_against(&precise));
}
#[test]
fn a_biased_null_is_inadmissible_even_though_its_ci_contains_one() {
let biased = PairedRatio {
median: 1.05,
median_ci95_low: 0.90,
median_ci95_high: 1.20,
p5: 0.85,
p95: 1.25,
rounds: 40,
};
assert!(
biased.median_ci95_low <= 1.0 && 1.0 <= biased.median_ci95_high,
"fixture must contain 1.0 or it does not exercise the retired clause"
);
assert!(!biased.is_admissible_null());
let large_effect = PairedRatio {
median: 4.0,
..biased
};
assert!(!large_effect.decidable_against(&biased));
let at_edge = PairedRatio {
median: 1.0 + NULL_MEDIAN_TOLERANCE * 0.99,
..biased
};
let past_edge = PairedRatio {
median: 1.0 + NULL_MEDIAN_TOLERANCE * 1.01,
..biased
};
assert!(at_edge.is_admissible_null());
assert!(!past_edge.is_admissible_null());
let too_few_rounds = PairedRatio {
median: 1.0,
rounds: 9,
..biased
};
let inverted_ci = PairedRatio {
median: 1.0,
median_ci95_low: 1.01,
median_ci95_high: 0.99,
..biased
};
assert!(!too_few_rounds.is_admissible_null());
assert!(!inverted_ci.is_admissible_null());
}
#[test]
#[ignore = "timing probe: run pinned under taskset, see bd-pjh09"]
fn null_gate_admissibility_moves_while_the_effect_reproduces() {
let identity = print_bench_elf_sha256().expect("bench ELF identity");
let cpu = observed_cpu();
let base = || {
let mut acc = 0u64;
for i in 0..2_000u64 {
acc = acc.wrapping_add(black_box(i).wrapping_mul(2_654_435_761));
}
black_box(acc);
};
let cand = || {
let mut acc = 0u64;
for i in 0..8_000u64 {
acc = acc.wrapping_add(black_box(i).wrapping_mul(2_654_435_761));
}
black_box(acc);
};
let mut effects = Vec::new();
let mut new_verdicts = Vec::new();
for &rounds in &[11usize, 41, 161, 641] {
let null = paired_median_ratio(rounds, 8, base, base);
let lever = paired_median_ratio(rounds, 8, base, cand);
let admissible_old =
null.rounds >= 10 && null.median_ci95_low <= 1.0 && 1.0 <= null.median_ci95_high;
let admissible_new = null.is_admissible_null();
println!(
"bd-pjh09 elf={} cpu={cpu} rounds={rounds} \
null_median={:.6} null_ci=[{:.6},{:.6}] null_half_width_pct={:.4} \
admissible_old={admissible_old} admissible_new={admissible_new} \
lever_median={:.6} decidable_new={}",
identity.sha256,
null.median,
null.median_ci95_low,
null.median_ci95_high,
null.null_half_width() * 100.0,
lever.median,
lever.decidable_against(&null),
);
effects.push(lever.median);
new_verdicts.push(admissible_new);
}
let low = effects.iter().copied().fold(f64::INFINITY, f64::min);
let high = effects.iter().copied().fold(f64::NEG_INFINITY, f64::max);
let spread_pct = (high / low - 1.0) * 100.0;
println!("bd-pjh09 effect_spread_pct={spread_pct:.4} cpu={cpu}");
assert!(
spread_pct <= 2.5,
"planted effect must reproduce across the precision sweep; spread {spread_pct:.4}% \
over {effects:?} — the host is too loaded for this probe to say anything"
);
assert!(
new_verdicts.iter().all(|&v| v == new_verdicts[0]),
"corrected admissibility must not depend on how precisely the null was sampled: \
{new_verdicts:?}"
);
}
fn observed_cpu() -> i64 {
let Ok(stat) = std::fs::read_to_string("/proc/self/stat") else {
return -1;
};
let Some((_, after_comm)) = stat.rsplit_once(')') else {
return -1;
};
after_comm
.split_whitespace()
.nth(36)
.and_then(|field| field.parse().ok())
.unwrap_or(-1)
}
#[test]
fn median_bootstrap_and_two_x_null_gate_are_deterministic() {
let null_samples = [0.99, 1.01, 1.0, 0.995, 1.005, 1.0, 0.998, 1.002, 1.0, 1.0];
let (low, high) = bootstrap_median_ci95(&null_samples);
let repeated = bootstrap_median_ci95(&null_samples);
assert_eq!(low.to_bits(), repeated.0.to_bits());
assert_eq!(high.to_bits(), repeated.1.to_bits());
let null = PairedRatio {
median: 1.0,
median_ci95_low: 0.99,
median_ci95_high: 1.01,
p5: 0.8,
p95: 1.2,
rounds: 10,
};
let below_floor = PairedRatio {
median: 1.019,
..null
};
let clears_floor = PairedRatio {
median: 1.021,
..null
};
assert!(!below_floor.decidable_against(&null));
assert!(clears_floor.decidable_against(&null));
}
}