use macrame::graph::EdgeAssertion;
use macrame::metrics::CommandKind;
use macrame::{ConceptUpsert, Database};
const EPOCH: &str = "2026-01-01T00:00:00.000000Z";
#[derive(Clone, Copy, PartialEq, Eq)]
enum Shape {
Fanout,
History,
}
fn batch(shape: Shape, n: usize) -> Vec<EdgeAssertion> {
(0..n)
.map(|i| {
let from = format!("2026-01-01T00:00:00.{:06}Z", i);
let to = format!("2026-01-01T00:00:00.{:06}Z", i + 1);
let target = match shape {
Shape::Fanout => format!("t{i:07}"),
Shape::History => "t0000000".to_string(),
};
EdgeAssertion::new("src", target, "LINKS")
.valid_from(from)
.valid_to(to)
})
.collect()
}
async fn hold_ms(shape: Shape, n: usize) -> f64 {
let dir = tempfile::TempDir::new().unwrap();
let path = dir.path().join("b.db");
let db = Database::open_with_cadence(&path, None).await.unwrap();
let mut ids: Vec<String> = (0..n).map(|i| format!("t{i:07}")).collect();
ids.push("src".to_string());
for chunk in ids.chunks(2_000) {
db.write_concepts(
chunk
.iter()
.map(|id| ConceptUpsert::new(id, "n").valid_from(EPOCH))
.collect(),
)
.await
.unwrap();
}
db.write_bulk_atomic(batch(shape, n)).await.unwrap();
let k = db
.metrics()
.kinds
.iter()
.find(|k| k.kind == CommandKind::WriteBulkAtomic)
.unwrap()
.clone();
assert_eq!(k.turns, 1, "the batch did not reach the actor as one turn");
db.close().await.unwrap();
k.longest.as_secs_f64() * 1000.0
}
#[tokio::main]
async fn main() {
println!("libSQL 0.9.30, best of 3. `hold` is the actor turn (T1.4).");
println!("`predicted` is macrame::connection::estimated_bulk_hold.\n");
println!(
"{:>8} {:>10} {:>11} {:>11} {:>11} {:>11}",
"rows", "shape", "hold ms", "us/row", "predicted", "ratio"
);
for shape in [Shape::Fanout, Shape::History] {
let label = match shape {
Shape::Fanout => "fanout",
Shape::History => "history",
};
for n in [100usize, 500, 2_000, 5_000, 10_000, 20_000] {
let mut best = f64::MAX;
for _ in 0..3 {
best = best.min(hold_ms(shape, n).await);
}
let predicted =
macrame::connection::estimated_bulk_hold(&batch(shape, n)).as_secs_f64() * 1000.0;
println!(
"{:>8} {:>10} {:>11.1} {:>11.1} {:>11.1} {:>10.2}x",
n,
label,
best,
best * 1000.0 / n as f64,
predicted,
predicted / best
);
}
println!();
}
}