use libsql::{Builder, OpenFlags};
use macrame::prelude::*;
use std::time::{Duration, Instant};
const ROUNDS: usize = 200;
fn report(label: &str, best: Duration, total: Duration) {
println!(
" {label:<44} best {:>9.3} us mean {:>9.3} us",
best.as_secs_f64() * 1e6,
total.as_secs_f64() * 1e6 / ROUNDS as f64,
);
}
const DIRTY_ONE: &str = "SELECT (SELECT count(*) FROM temp.sqlite_master) \
+ (SELECT count(*) FROM pragma_database_list) - 2";
#[tokio::main]
async fn main() {
let dir = std::env::temp_dir().join(format!("macrame_hyg_probe_{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let path = dir.join("probe.db");
let db = Database::open_tuned(&path, Tuning::default().cadence(CadencePolicy::Disabled))
.await
.unwrap();
db.write_concepts(vec![ConceptUpsert::new("a", "A")
.content("body")
.valid_from("2026-01-01T00:00:00.000000Z")])
.await
.unwrap();
let conn = db.diagnostic_conn().await.unwrap();
println!("diagnostic hygiene, {ROUNDS} rounds, live WAL database, actor running");
println!();
let mut best = Duration::MAX;
let mut total = Duration::ZERO;
for _ in 0..ROUNDS {
let t = Instant::now();
let c = db.diagnostic_conn().await.unwrap();
let d = t.elapsed();
best = best.min(d);
total += d;
drop(c);
}
report("Database::diagnostic_conn() (warm, today)", best, total);
let mut best = Duration::MAX;
let mut total = Duration::ZERO;
for _ in 0..ROUNDS {
let t = Instant::now();
assert!(path.exists());
let d = t.elapsed();
best = best.min(d);
total += d;
}
report(" of which: path.exists()", best, total);
println!();
let mut best = Duration::MAX;
let mut total = Duration::ZERO;
for _ in 0..ROUNDS {
let t = Instant::now();
assert!(conn.is_autocommit());
let d = t.elapsed();
best = best.min(d);
total += d;
}
report("is_autocommit()", best, total);
let mut best = Duration::MAX;
let mut total = Duration::ZERO;
for _ in 0..ROUNDS {
let t = Instant::now();
let mut rows = conn.query("SELECT 1", ()).await.unwrap();
let row = rows.next().await.unwrap().unwrap();
let v: i64 = row.get(0).unwrap();
let d = t.elapsed();
assert_eq!(v, 1);
best = best.min(d);
total += d;
}
report(" floor: one trivial statement", best, total);
let mut best = Duration::MAX;
let mut total = Duration::ZERO;
for _ in 0..ROUNDS {
let t = Instant::now();
let mut rows = conn.query(DIRTY_ONE, ()).await.unwrap();
let row = rows.next().await.unwrap().unwrap();
let dirt: i64 = row.get(0).unwrap();
let d = t.elapsed();
assert_eq!(dirt, 0);
best = best.min(d);
total += d;
}
report("dirt check, one statement", best, total);
let mut best = Duration::MAX;
let mut total = Duration::ZERO;
for _ in 0..ROUNDS {
let t = Instant::now();
let mut rows = conn
.query("SELECT count(*) FROM temp.sqlite_master", ())
.await
.unwrap();
let a: i64 = rows.next().await.unwrap().unwrap().get(0).unwrap();
let mut rows = conn
.query("SELECT count(*) FROM pragma_database_list", ())
.await
.unwrap();
let b: i64 = rows.next().await.unwrap().unwrap().get(0).unwrap();
let d = t.elapsed();
assert_eq!((a, b), (0, 2));
best = best.min(d);
total += d;
}
report("dirt check, two statements", best, total);
let mut best = Duration::MAX;
let mut total = Duration::ZERO;
for _ in 0..ROUNDS {
let t = Instant::now();
let mut rows = conn.query("PRAGMA temp.schema_version", ()).await.unwrap();
let sv: i64 = rows.next().await.unwrap().unwrap().get(0).unwrap();
let mut rows = conn.query("PRAGMA database_list", ()).await.unwrap();
let mut n = 0;
while rows.next().await.unwrap().is_some() {
n += 1;
}
let d = t.elapsed();
assert_eq!(n, 2, "database_list should be main + temp");
let _ = sv;
best = best.min(d);
total += d;
}
report("dirt check, two pragmas", best, total);
let mut best = Duration::MAX;
let mut total = Duration::ZERO;
for _ in 0..ROUNDS {
let t = Instant::now();
let mut rows = conn.query("PRAGMA busy_timeout", ()).await.unwrap();
let v: i64 = rows.next().await.unwrap().unwrap().get(0).unwrap();
let d = t.elapsed();
assert_eq!(v, 5000);
best = best.min(d);
total += d;
}
report("PRAGMA busy_timeout (read back)", best, total);
let mut best = Duration::MAX;
let mut total = Duration::ZERO;
for _ in 0..ROUNDS {
let t = Instant::now();
let mut rows = conn.query("PRAGMA busy_timeout = 5000", ()).await.unwrap();
let v: i64 = rows.next().await.unwrap().unwrap().get(0).unwrap();
let d = t.elapsed();
assert_eq!(v, 5000);
best = best.min(d);
total += d;
}
report("PRAGMA busy_timeout = 5000 (restate)", best, total);
let slot = tokio::sync::Mutex::new(Some(conn.clone()));
let mut best = Duration::MAX;
let mut total = Duration::ZERO;
for _ in 0..ROUNDS {
let t = Instant::now();
let guard = slot.lock().await;
let c = guard.as_ref().unwrap();
assert!(c.is_autocommit());
let mut rows = c.query("PRAGMA temp.schema_version", ()).await.unwrap();
let _sv: i64 = rows.next().await.unwrap().unwrap().get(0).unwrap();
let mut rows = c.query("PRAGMA database_list", ()).await.unwrap();
let mut n = 0;
while rows.next().await.unwrap().is_some() {
n += 1;
}
assert_eq!(n, 2);
let mut rows = c.query("PRAGMA busy_timeout = 5000", ()).await.unwrap();
let _v: i64 = rows.next().await.unwrap().unwrap().get(0).unwrap();
let out = c.clone();
drop(guard);
let d = t.elapsed();
best = best.min(d);
total += d;
drop(out);
}
report("the whole scrub, in one block", best, total);
let mut best = Duration::MAX;
let mut total = Duration::ZERO;
for _ in 0..ROUNDS {
let t = Instant::now();
let guard = slot.lock().await;
let out = guard.as_ref().unwrap().clone();
drop(guard);
let d = t.elapsed();
best = best.min(d);
total += d;
drop(out);
}
report(" of which: lock + Connection::clone", best, total);
let mut best = Duration::MAX;
let mut total = Duration::ZERO;
for _ in 0..ROUNDS {
let t = Instant::now();
let _ = conn.query("PRAGMA busy_timeout = 5000", ()).await.unwrap();
let d = t.elapsed();
best = best.min(d);
total += d;
}
report("PRAGMA busy_timeout = 5000, Rows dropped", best, total);
let mut best = Duration::MAX;
let mut total = Duration::ZERO;
for _ in 0..ROUNDS {
let t = Instant::now();
let mut rows = conn.query("PRAGMA temp.schema_version", ()).await.unwrap();
let _sv: i64 = rows.next().await.unwrap().unwrap().get(0).unwrap();
drop(rows);
let d = t.elapsed();
best = best.min(d);
total += d;
}
report(" one pragma, stepped then dropped", best, total);
let mut best = Duration::MAX;
let mut total = Duration::ZERO;
for _ in 0..ROUNDS {
let t = Instant::now();
let rows = conn.query("PRAGMA temp.schema_version", ()).await.unwrap();
drop(rows);
let d = t.elapsed();
best = best.min(d);
total += d;
}
report(" one pragma, dropped unstepped", best, total);
let mut best = Duration::MAX;
let mut total = Duration::ZERO;
for _ in 0..ROUNDS {
let t = Instant::now();
assert!(path.exists());
let guard = slot.lock().await;
let c = guard.as_ref().unwrap();
assert!(c.is_autocommit());
let mut rows = c.query("PRAGMA temp.schema_version", ()).await.unwrap();
let _sv: i64 = rows.next().await.unwrap().unwrap().get(0).unwrap();
let mut rows = c.query("PRAGMA database_list", ()).await.unwrap();
let mut n = 0;
while rows.next().await.unwrap().is_some() {
n += 1;
}
assert_eq!(n, 2);
let _ = c.query("PRAGMA busy_timeout = 5000", ()).await.unwrap();
let out = c.clone();
drop(guard);
let d = t.elapsed();
best = best.min(d);
total += d;
drop(out);
}
report("stat + scrub, one loop (the whole call)", best, total);
let mut best = Duration::MAX;
let mut total = Duration::ZERO;
for _ in 0..ROUNDS {
let t = Instant::now();
assert!(path.exists());
let d = t.elapsed();
best = best.min(d);
total += d;
}
report(" path.exists(), measured again here", best, total);
let handle = Builder::new_local(&path)
.flags(OpenFlags::SQLITE_OPEN_READ_ONLY)
.build()
.await
.unwrap();
let mut best = Duration::MAX;
let mut total = Duration::ZERO;
for _ in 0..ROUNDS {
let t = Instant::now();
let fresh = handle.connect().unwrap();
let mut rows = fresh.query("PRAGMA busy_timeout = 5000", ()).await.unwrap();
let v: i64 = rows.next().await.unwrap().unwrap().get(0).unwrap();
let d = t.elapsed();
assert_eq!(v, 5000);
best = best.min(d);
total += d;
drop(fresh);
}
report("re-mint: connect() + configure_common", best, total);
println!();
let _ = conn.query("BEGIN", ()).await.unwrap();
println!(
" after BEGIN, is_autocommit() {}",
conn.is_autocommit()
);
let mut rows = conn.query(DIRTY_ONE, ()).await.unwrap();
let dirt: i64 = rows.next().await.unwrap().unwrap().get(0).unwrap();
println!(" after BEGIN, dirt counter {dirt}");
let t = Instant::now();
let _ = conn.query("ROLLBACK", ()).await.unwrap();
println!(
" ROLLBACK of a leaked read transaction {:>9.3} us",
t.elapsed().as_secs_f64() * 1e6
);
println!(
" after ROLLBACK, is_autocommit() {}",
conn.is_autocommit()
);
println!();
let _ = conn
.query("CREATE TEMP TABLE probe_t(x)", ())
.await
.unwrap();
let mut rows = conn.query(DIRTY_ONE, ()).await.unwrap();
let dirt: i64 = rows.next().await.unwrap().unwrap().get(0).unwrap();
println!(" dirt counter after CREATE TEMP TABLE {dirt}");
let mut rows = conn.query("PRAGMA temp.schema_version", ()).await.unwrap();
let sv: i64 = rows.next().await.unwrap().unwrap().get(0).unwrap();
println!(" temp.schema_version after TEMP TABLE {sv}");
let aux = dir.join("aux.db");
Database::open_tuned(&aux, Tuning::default().cadence(CadencePolicy::Disabled))
.await
.unwrap()
.close()
.await
.unwrap();
let _ = conn
.query(&format!("ATTACH DATABASE '{}' AS aux", aux.display()), ())
.await
.unwrap();
let mut rows = conn.query(DIRTY_ONE, ()).await.unwrap();
let dirt: i64 = rows.next().await.unwrap().unwrap().get(0).unwrap();
println!(" dirt counter after ATTACH {dirt}");
let mut rows = conn.query("PRAGMA database_list", ()).await.unwrap();
let mut n = 0;
while rows.next().await.unwrap().is_some() {
n += 1;
}
println!(" database_list rows after ATTACH {n}");
let _ = conn
.query("PRAGMA case_sensitive_like = ON", ())
.await
.unwrap();
let mut rows = conn.query(DIRTY_ONE, ()).await.unwrap();
let dirt: i64 = rows.next().await.unwrap().unwrap().get(0).unwrap();
println!(" dirt counter after a PRAGMA {dirt} <-- invisible");
drop(conn);
db.close().await.unwrap();
let _ = std::fs::remove_dir_all(&dir);
}