// Ceiling test: P processes write hook-like transactions back to back, no pauses. // Far beyond Peppa's real load; shows where each backend starts to bend. // node contention.ts --backend bun --procs 20 --seconds 10 import { spawn } from "node:child_process"; import { homedir } from "node:os"; import { parseArgs } from "node:util"; import { openDb, openPg, openSqlite, type Backend } from "./lib/db.ts"; import { pgSchema, seedStmts, sqliteSchema } from "./lib/schema.ts"; const { values: a } = parseArgs({ options: { backend: { type: "string", default: "bun" }, procs: { type: "string", default: "20" }, seconds: { type: "string", default: "10" }, worker: { type: "boolean" } } }); const backend = a.backend as Backend; const now = () => performance.timeOrigin + performance.now(); if (a.worker) { const db = await openDb(backend); const sid = Number(process.env.BENCH_SESSION); const end = Number(process.env.BENCH_END); const ms: number[] = []; let busy = 0; let errors = 0; while (Date.now() < end) { const t = now(); try { await db.tx([ ["update sessions set status = 'running', last_activity_at = ? where id = ?", [Date.now(), sid]], ["insert into events (project_id, task_id, session_id, type, payload, created_at) values (1, ?, ?, 'turn_end', ?, ?) returning id", [sid, sid, JSON.stringify({ t0: t }), Date.now()]], ]); } catch { errors++; } busy += db.lastBusy; ms.push(now() - t); } await db.close(); // A file, not stdout: process.exit() can cut a large pipe write short. (await import("node:fs")).writeFileSync(`${process.env.BENCH_DB}.${sid}.json`, JSON.stringify({ ms, busy, errors })); process.exit(0); } const procs = Number(a.procs); const runtime = backend === "pg" || backend === "node" || backend === "better" ? process.execPath : `${homedir()}/.bun/bin/bun`; const work = `${process.env.BENCH_TMP ?? "/tmp"}/peppa-contention-${process.pid}`; const env = { ...process.env, BENCH_BACKEND: backend, BENCH_DB: `${work}.db` } as Record; if (backend === "pg") { if (!/\/peppa_bench(\?|$)/.test(env.BENCH_PG_URL ?? "")) throw new Error("BENCH_PG_URL must point at peppa_bench"); const db = await openPg(env.BENCH_PG_URL); await db.all("drop schema public cascade"); await db.all("create schema public"); for (const s of pgSchema) await db.all(s); await db.tx(seedStmts(procs)); await db.close(); } else { const db = await openSqlite("node", env.BENCH_DB); await db.all("PRAGMA journal_mode = WAL"); for (const s of sqliteSchema) await db.all(s); await db.tx(seedStmts(procs)); await db.close(); } const end = Date.now() + 1000 + Number(a.seconds) * 1000; const results = await Promise.all( Array.from({ length: procs }, (_, i) => new Promise<{ ms: number[]; busy: number; errors: number }>((resolve) => { const p = spawn(runtime, [new URL(import.meta.url).pathname, "--worker", "--backend", backend], { env: { ...env, BENCH_SESSION: String(i + 1), BENCH_END: String(end) }, stdio: ["ignore", "inherit", "inherit"] }); p.on("exit", async () => { const fs = await import("node:fs"); const f = `${env.BENCH_DB}.${i + 1}.json`; resolve(fs.existsSync(f) ? JSON.parse(fs.readFileSync(f, "utf8")) : { ms: [], busy: 0, errors: 1 }); fs.rmSync(f, { force: true }); }); }), ), ); const ms = results.flatMap((r) => r.ms).sort((x, y) => x - y); const pct = (p: number) => ms[Math.min(ms.length - 1, Math.floor((p / 100) * ms.length))].toFixed(2); console.log(JSON.stringify({ backend, procs, seconds: Number(a.seconds), txPerSec: Math.round(ms.length / Number(a.seconds)), p50: pct(50), p95: pct(95), p99: pct(99), max: ms.at(-1)!.toFixed(1), busyRetries: results.reduce((s, r) => s + r.busy, 0), errors: results.reduce((s, r) => s + r.errors, 0) })); for (const s of ["", "-wal", "-shm"]) (await import("node:fs")).rmSync(env.BENCH_DB + s, { force: true });