T-47: M1.2a — Scaffold tools/curate-etl Rust crate: split-factor table + un-adjustment core with local benchmark (no new crates)
This commit is contained in:
parent
ce4b8fa91c
commit
4c8a621e48
4 changed files with 161 additions and 41 deletions
|
|
@ -3,7 +3,7 @@
|
|||
use std::hint::black_box;
|
||||
use std::time::Instant;
|
||||
|
||||
use crate::split::{unadjust_volume, ymd_to_epoch_days, SplitEvent, SplitTable};
|
||||
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;
|
||||
|
|
@ -48,14 +48,23 @@ pub struct BenchReport {
|
|||
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<String>`] 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) -> BenchReport {
|
||||
let min_date = ymd_to_epoch_days(1990, 1, 1).unwrap_or(7_305);
|
||||
let max_date = ymd_to_epoch_days(2025, 12, 31).unwrap_or(20_453);
|
||||
pub fn run_split_benchmark(rows: usize) -> Result<BenchReport, SplitError> {
|
||||
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<String> = (0..BENCH_KEY_COUNT)
|
||||
|
|
@ -72,7 +81,7 @@ pub fn run_split_benchmark(rows: usize) -> BenchReport {
|
|||
}
|
||||
|
||||
let build_start = Instant::now();
|
||||
let table = SplitTable::from_events(events).unwrap_or_default();
|
||||
let table = SplitTable::from_events(events)?;
|
||||
let build_elapsed_ms = build_start.elapsed().as_secs_f64() * 1_000.0;
|
||||
|
||||
let key_count = keys.len();
|
||||
|
|
@ -93,13 +102,9 @@ pub fn run_split_benchmark(rows: usize) -> BenchReport {
|
|||
let elapsed_secs = elapsed.as_secs_f64();
|
||||
let elapsed_ms = elapsed_secs * 1_000.0;
|
||||
|
||||
let (rows_per_sec, ns_per_row) = if rows == 0 || elapsed_secs == 0.0 {
|
||||
(0.0, 0.0)
|
||||
} else {
|
||||
(rows as f64 / elapsed_secs, elapsed_secs * 1e9 / rows as f64)
|
||||
};
|
||||
let (rows_per_sec, ns_per_row) = compute_rates(rows, elapsed_secs);
|
||||
|
||||
BenchReport {
|
||||
Ok(BenchReport {
|
||||
rows,
|
||||
key_count,
|
||||
table_keys_with_splits: table.len(),
|
||||
|
|
@ -109,35 +114,53 @@ pub fn run_split_benchmark(rows: usize) -> BenchReport {
|
|||
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);
|
||||
let b = run_split_benchmark(1_000);
|
||||
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 > 0.0);
|
||||
assert!(a.ns_per_row > 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);
|
||||
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);
|
||||
assert!(report.table_keys_with_splits > 0);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue