bench_to_json_null_num/
bench_to_json_null_num.rs1use std::collections::BTreeMap;
8
9use fp_columnar::{Column, ValidityMask};
10use fp_frame::DataFrame;
11use fp_index::Index;
12use fp_io::JsonOrient;
13use fp_types::Scalar;
14
15fn main() {
16 let a: Vec<String> = std::env::args().collect();
17 let n: usize = a.get(1).and_then(|s| s.parse().ok()).unwrap_or(1_000_000);
18 let iters: usize = a.get(2).and_then(|s| s.parse().ok()).unwrap_or(6);
19
20 let ids: Vec<i64> = (0..n as i64).collect();
21 let price: Vec<f64> = (0..n).map(|i| (i % 100000) as f64 / 100.0).collect();
22 let mut price_v = ValidityMask::all_valid(n);
23 let qty: Vec<i64> = (0..n as i64).map(|i| i % 1000).collect();
24 let mut qty_v = ValidityMask::all_valid(n);
25 let active: Vec<bool> = (0..n).map(|i| i % 2 == 0).collect();
26 let mut active_v = ValidityMask::all_valid(n);
27 let mut nb: Vec<u8> = Vec::with_capacity(n * 8);
28 let mut no: Vec<usize> = Vec::with_capacity(n + 1);
29 no.push(0);
30 let mut names_eager: Vec<Scalar> = Vec::with_capacity(n);
31 for i in 0..n {
32 if i % 7 == 0 {
33 price_v.set(i, false);
34 }
35 if i % 5 == 0 {
36 qty_v.set(i, false);
37 }
38 if i % 3 == 0 {
39 active_v.set(i, false);
40 }
41 let s = format!("item_{}", i % 5000);
42 nb.extend_from_slice(s.as_bytes());
43 no.push(nb.len());
44 names_eager.push(Scalar::Utf8(s));
45 }
46
47 let mk = |name_col: Column| -> DataFrame {
48 let mut cols: BTreeMap<String, Column> = BTreeMap::new();
49 cols.insert("id".to_string(), Column::from_i64_values_owned(ids.clone()));
50 cols.insert(
51 "price".to_string(),
52 Column::from_f64_values_with_validity(price.clone(), price_v.clone()),
53 );
54 cols.insert(
55 "qty".to_string(),
56 Column::from_i64_values_with_validity(qty.clone(), qty_v.clone()),
57 );
58 cols.insert(
59 "active".to_string(),
60 Column::from_bool_values_with_validity(active.clone(), active_v.clone()),
61 );
62 cols.insert("name".to_string(), name_col);
63 DataFrame::new(Index::new_known_unique_int64_unit_range(0, n), cols).unwrap()
64 };
65 let frame_fast = mk(Column::from_utf8_contiguous(nb.clone(), no.clone()));
66 let frame_general = mk(Column::from_values(names_eager).unwrap());
67
68 let ctl = frame_general.column("name").unwrap();
69 assert!(
70 ctl.as_utf8_contiguous().is_none() && ctl.as_nullable_utf8_contiguous().is_none(),
71 "control name column must force the serde tree"
72 );
73
74 let mut best_t = u128::MAX;
75 for _ in 0..iters {
76 let t = std::time::Instant::now();
77 let s = fp_io::write_json_string(&frame_fast, JsonOrient::Records).unwrap();
78 best_t = best_t.min(t.elapsed().as_nanos());
79 std::hint::black_box(s.len());
80 }
81 let mut best_c = u128::MAX;
82 for _ in 0..iters {
83 let t = std::time::Instant::now();
84 let s = fp_io::write_json_string(&frame_general, JsonOrient::Records).unwrap();
85 best_c = best_c.min(t.elapsed().as_nanos());
86 std::hint::black_box(s.len());
87 }
88 let sf = fp_io::write_json_string(&frame_fast, JsonOrient::Records).unwrap();
89 let sg = fp_io::write_json_string(&frame_general, JsonOrient::Records).unwrap();
90 assert_eq!(sf, sg, "fast (FN/IN) JSON must byte-match the serde tree");
91 println!(
92 "to_json(records) nullable-numeric n={n} NEW(fast)={:>7.2}ms CONTROL(serde)={:>7.2}ms speedup={:.3}x",
93 best_t as f64 / 1e6,
94 best_c as f64 / 1e6,
95 best_c as f64 / best_t as f64,
96 );
97}