//! Fixed-seed, std-only benchmark for the split-factor lookup and un-adjustment hot loop. use std::hint::black_box; use std::time::Instant; use crate::split::{unadjust_volume, ymd_to_epoch_days, SplitError, SplitEvent, SplitTable}; pub const DEFAULT_BENCH_ROWS: usize = 10_000_000; pub const BENCH_KEY_COUNT: usize = 20_000; pub const BENCH_EVENT_COUNT: usize = 19_000; const BENCH_SEED: u64 = 0x570C_4B07_5171_7E57; const SAMPLE_RATIOS: [f64; 9] = [2.0, 3.0, 4.0, 5.0, 10.0, 0.5, 0.25, 0.1, 0.05]; /// Deterministic SplitMix64 generator (zero external dependencies). #[derive(Debug, Clone, Copy)] struct SplitMix64 { state: u64, } impl SplitMix64 { #[inline] fn new(seed: u64) -> Self { Self { state: seed } } #[inline] fn next_u64(&mut self) -> u64 { self.state = self.state.wrapping_add(0x9E37_79B9_7F4A_7C15); let mut z = self.state; z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9); z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB); z ^ (z >> 31) } } /// Summary metrics produced by [`run_split_benchmark`]. #[derive(Debug, Clone, Copy, PartialEq)] pub struct BenchReport { pub rows: usize, pub key_count: usize, pub table_keys_with_splits: usize, pub table_event_count: usize, pub build_elapsed_ms: f64, pub elapsed_ms: f64, pub rows_per_sec: f64, pub ns_per_row: f64, pub checksum: f64, } #[inline] fn compute_rates(rows: usize, elapsed_secs: f64) -> (f64, f64) { if rows == 0 || elapsed_secs <= 0.0 || !elapsed_secs.is_finite() { (0.0, 0.0) } else { (rows as f64 / elapsed_secs, elapsed_secs * 1e9 / rows as f64) } } /// Runs the fixed-seed split lookup + `unadjust_volume` benchmark for `rows` bars. /// /// Builds a [`SplitTable`] with `20_000` symbol keys and `19_000` deterministic forward /// and reverse splits spanning `1990-01-01..=2025-12-31`, then executes `rows` zero-allocation /// lookups and volume un-adjustments. pub fn run_split_benchmark(rows: usize) -> Result { let min_date = ymd_to_epoch_days(1990, 1, 1)?; let max_date = ymd_to_epoch_days(2025, 12, 31)?; let date_span = (max_date - min_date + 1) as u64; let keys: Vec = (0..BENCH_KEY_COUNT) .map(|i| format!("SYM{i:05}.US")) .collect(); let mut rng = SplitMix64::new(BENCH_SEED); let mut events = Vec::with_capacity(BENCH_EVENT_COUNT); for _ in 0..BENCH_EVENT_COUNT { let key_idx = (rng.next_u64() as usize) % keys.len(); let ex_date = min_date + (rng.next_u64() % date_span) as i32; let ratio = SAMPLE_RATIOS[(rng.next_u64() as usize) % SAMPLE_RATIOS.len()]; events.push((keys[key_idx].clone(), SplitEvent { ex_date, ratio })); } let build_start = Instant::now(); let table = SplitTable::from_events(events)?; let build_elapsed_ms = build_start.elapsed().as_secs_f64() * 1_000.0; let key_count = keys.len(); let mut acc = 0.0_f64; let loop_start = Instant::now(); for _ in 0..rows { let key_idx = (rng.next_u64() as usize) % key_count; let bar_date = min_date + (rng.next_u64() % date_span) as i32; let adj_volume = ((rng.next_u64() % 10_000_000) + 100) as f64; let factor = table.cum_factor_after(keys[key_idx].as_str(), bar_date); let raw_volume = unadjust_volume(adj_volume, factor); acc += raw_volume; } let checksum = black_box(acc); let elapsed = loop_start.elapsed(); let elapsed_secs = elapsed.as_secs_f64(); let elapsed_ms = elapsed_secs * 1_000.0; let (rows_per_sec, ns_per_row) = compute_rates(rows, elapsed_secs); Ok(BenchReport { rows, key_count, table_keys_with_splits: table.len(), table_event_count: table.event_count(), build_elapsed_ms, elapsed_ms, rows_per_sec, ns_per_row, checksum, }) } #[cfg(test)] mod tests { use super::*; const EXPECTED_KEYS_WITH_SPLITS: usize = 12_153; const EXPECTED_EVENT_COUNT: usize = 18_999; #[test] fn benchmark_small_run_is_deterministic() { let a = run_split_benchmark(1_000).unwrap(); let b = run_split_benchmark(1_000).unwrap(); assert_eq!(a.rows, 1_000); assert_eq!(a.key_count, BENCH_KEY_COUNT); assert_eq!(a.table_keys_with_splits, EXPECTED_KEYS_WITH_SPLITS); assert_eq!(a.table_event_count, EXPECTED_EVENT_COUNT); assert_eq!(a.table_keys_with_splits, b.table_keys_with_splits); assert_eq!(a.table_event_count, b.table_event_count); assert_eq!(a.checksum.to_bits(), b.checksum.to_bits()); assert!(a.checksum.is_finite() && a.checksum > 0.0); assert!(a.rows_per_sec.is_finite() && a.rows_per_sec >= 0.0); assert!(a.ns_per_row.is_finite() && a.ns_per_row >= 0.0); } #[test] fn compute_rates_uses_fixed_duration() { let (rows_per_sec, ns_per_row) = compute_rates(1_000, 0.5); assert_eq!(rows_per_sec, 2_000.0); assert_eq!(ns_per_row, 500_000.0); assert_eq!(compute_rates(0, 0.5), (0.0, 0.0)); assert_eq!(compute_rates(1_000, 0.0), (0.0, 0.0)); assert_eq!(compute_rates(1_000, -1.0), (0.0, 0.0)); } #[test] fn benchmark_zero_rows_handles_cleanly() { let report = run_split_benchmark(0).unwrap(); assert_eq!(report.rows, 0); assert_eq!(report.key_count, BENCH_KEY_COUNT); assert_eq!(report.table_keys_with_splits, EXPECTED_KEYS_WITH_SPLITS); assert_eq!(report.table_event_count, EXPECTED_EVENT_COUNT); assert_eq!(report.checksum, 0.0); assert_eq!(report.rows_per_sec, 0.0); assert_eq!(report.ns_per_row, 0.0); } }