Skip to main content

bench_csv_quarter/
bench_csv_quarter.rs

1//! Write-only bench + golden for `write_csv_string` on the quarter-affine
2//! all-Float64 frame used by the high-ram CSV roundtrip profile
3//! (br-frankenpandas-uza04.84). Each column `io_col_{c}` holds
4//! `row * 1.25 + c`, exactly `build_numeric_frame` in
5//! `high_ram_perf_baseline.rs`, so every column is quarter-affine and routes
6//! through the typed quarter-plan writer whose integer Display residual this
7//! bead attacks.
8//!
9//! Run (timing):  cargo run -p fp-io --example bench_csv_quarter --release -- 100000 20 50
10//! Run (golden):  cargo run -p fp-io --example bench_csv_quarter --release -- 100000 20 1 emit /tmp/q.csv
11//!                then `sha256sum /tmp/q.csv`.
12
13use std::{collections::BTreeMap, time::Instant};
14
15use fp_columnar::Column;
16use fp_frame::DataFrame;
17use fp_index::{Index, IndexLabel};
18use fp_io::write_csv_string;
19
20fn build(rows: usize, cols: usize) -> DataFrame {
21    let labels: Vec<IndexLabel> = (0..rows as i64).map(IndexLabel::Int64).collect();
22    let index = Index::new(labels);
23    let mut map = BTreeMap::new();
24    let mut order = Vec::with_capacity(cols);
25    for c in 0..cols {
26        let v: Vec<f64> = (0..rows)
27            .map(|row| (row as f64 * 1.25) + c as f64)
28            .collect();
29        let name = format!("io_col_{c}");
30        map.insert(name.clone(), Column::from_f64_values(v));
31        order.push(name);
32    }
33    DataFrame::new_with_column_order(index, map, order).expect("frame")
34}
35
36fn main() {
37    let mut args = std::env::args().skip(1);
38    let rows: usize = args.next().and_then(|s| s.parse().ok()).unwrap_or(100_000);
39    let cols: usize = args.next().and_then(|s| s.parse().ok()).unwrap_or(20);
40    let iters: usize = args.next().and_then(|s| s.parse().ok()).unwrap_or(50);
41    let emit = args.next();
42    let emit_path = args.next();
43
44    let frame = build(rows, cols);
45
46    // Warmup (also the byte source for the optional golden emit).
47    let warm = write_csv_string(&frame).expect("csv");
48    println!("LEN {} rows={rows} cols={cols}", warm.len());
49
50    if emit.as_deref() == Some("emit") {
51        if let Some(path) = emit_path {
52            std::fs::write(&path, warm.as_bytes()).expect("emit");
53            println!("EMIT {path}");
54        }
55        return;
56    }
57
58    let t = Instant::now();
59    let mut sink = 0usize;
60    for _ in 0..iters {
61        let out = write_csv_string(&frame).expect("csv");
62        sink = sink.wrapping_add(out.len());
63    }
64    let d = t.elapsed();
65    std::hint::black_box(sink);
66    println!(
67        "TIMING rows={rows} cols={cols} iters={iters} per_iter={:.3}ms",
68        d.as_secs_f64() * 1e3 / iters as f64
69    );
70}