use spg_engine::Engine;
use std::time::Instant;
const ROWS: i64 = 50_000;
const REPS_DEFAULT: usize = 200;
fn main() {
let which = std::env::args().nth(1).unwrap_or_else(|| "both".into());
let reps: usize = std::env::args()
.nth(2)
.and_then(|s| s.parse().ok())
.unwrap_or(REPS_DEFAULT);
let mut e = Engine::new();
e.set_autovacuum(false);
e.execute("CREATE TABLE f (id BIGINT PRIMARY KEY, k BIGINT, pad TEXT)")
.unwrap();
for chunk in 0..(ROWS / 1000) {
let mut sql = String::from("INSERT INTO f VALUES ");
for i in 0..1000 {
let id = chunk * 1000 + i + 1;
if i > 0 {
sql.push(',');
}
sql.push_str(&format!(
"({id},{},'{}')",
(id * 7919) % 50_000,
"x".repeat(60)
));
}
e.execute(&sql).unwrap();
}
let run = |e: &mut Engine, sql: &str, label: &str| {
let t0 = Instant::now();
for _ in 0..reps {
e.execute(sql).unwrap();
}
let per_row = t0.elapsed().as_secs_f64() * 1e9 / (ROWS as f64 * reps as f64);
println!("{label:<28} {per_row:6.1} ns/row");
};
let fast = "SELECT count(*) FROM f WHERE id > 0";
let slow = "SELECT count(*) FROM f WHERE id % 3 = 0";
match which.as_str() {
"fast" => run(&mut e, fast, "column cmp literal"),
"slow" => run(&mut e, slow, "column arith literal cmp"),
_ => {
run(&mut e, fast, "column cmp literal");
run(&mut e, slow, "column arith literal cmp");
}
}
}