{"draft":null,"from":2,"to":318,"files":[{"path":"scripts/local-writers.ts","added":87,"removed":0,"diff":"--- /dev/null\n+++ b/scripts/local-writers.ts\n@@ -0,0 +1,87 @@\n+// A local project's store lock under load: 1, 4 and 16 writers pushing to one local project at once.\n+//\n+//   node scripts/local-writers.ts [writers ...]        (default: 1 4 16; PUSHES=10 pushes per writer)\n+//\n+// Each writer is its own agent, in its own folder, changing its own file, so nothing collides: what's measured is the\n+// store's lock (one SQLite write transaction per call). Every push is its own `node client/abso.ts push` process, as\n+// an agent runs it. It prints each push's wall time (median, 95th percentile, slowest), the pushes per second across\n+// all writers, and any push that failed.\n+\n+import { spawn } from \"node:child_process\";\n+import { mkdtempSync, rmSync, writeFileSync } from \"node:fs\";\n+import { tmpdir } from \"node:os\";\n+import { join, resolve } from \"node:path\";\n+import { fileURLToPath } from \"node:url\";\n+import { main } from \"../client/src/main.ts\";\n+\n+const ROOT = resolve(fileURLToPath(new URL(\"..\", import.meta.url)));\n+const ENTRY = join(ROOT, \"client\", \"abso.ts\");\n+const PUSHES = Number(process.env.PUSHES ?? 10);\n+const counts = process.argv.slice(2).map(Number);\n+\n+function push(cwd: string, store: string, message: string): Promise<{ ms: number; ok: boolean; out: string }> {\n+\tconst started = performance.now();\n+\treturn new Promise((done) => {\n+\t\tconst child = spawn(process.execPath, [ENTRY, \"push\", \"-m\", message], {\n+\t\t\tcwd,\n+\t\t\tenv: { ...process.env, ABSO_STORE: store, ABSO_SERVERS_FILE: \"no-servers.json\" },\n+\t\t});\n+\t\tlet out = \"\";\n+\t\tchild.stdout.on(\"data\", (d) => {\n+\t\t\tout += d;\n+\t\t});\n+\t\tchild.stderr.on(\"data\", (d) => {\n+\t\t\tout += d;\n+\t\t});\n+\t\tchild.on(\"close\", (code) => done({ ms: performance.now() - started, ok: code === 0, out }));\n+\t});\n+}\n+\n+const pct = (sorted: number[], p: number) => sorted[Math.min(sorted.length - 1, Math.floor(sorted.length * p))]!;\n+console.log(`Node ${process.versions.node}, ${PUSHES} pushes per writer, each writer its own agent and file.`);\n+console.log(\"writers | pushes | median   | p95      | slowest  | pushes/s | failed\");\n+for (const writers of counts.length > 0 ? counts : [1, 4, 16]) {\n+\tconst store = mkdtempSync(join(tmpdir(), \"abso-writers-store-\"));\n+\tconst folders: string[] = [];\n+\tconst env = { ABSO_STORE: store, ABSO_SERVERS_FILE: \"no-servers.json\" };\n+\tconst first = mkdtempSync(join(tmpdir(), \"abso-writers-w0-\"));\n+\twriteFileSync(join(first, \"readme.txt\"), \"writers\\n\");\n+\tconst init = await main([\"init\", \"--local\", \"--project\", \"writers\", \"--agent\", \"w0\", \"--as-is\"], {\n+\t\tcwd: first,\n+\t\tenv,\n+\t\twrite: () => {},\n+\t});\n+\tif (init.exit !== 0) throw new Error(init.stdout + init.stderr);\n+\tfolders.push(first);\n+\tfor (let w = 1; w < writers; w++) {\n+\t\tconst dir = mkdtempSync(join(tmpdir(), `abso-writers-w${w}-`));\n+\t\tconst joined = await main([\"join\", \"writers\", \"--local\", \"--agent\", `w${w}`, \"--dir\", dir], {\n+\t\t\tcwd: dir,\n+\t\t\tenv,\n+\t\t\twrite: () => {},\n+\t\t});\n+\t\tif (joined.exit !== 0) throw new Error(joined.stdout + joined.stderr);\n+\t\tfolders.push(dir);\n+\t}\n+\tconst times: number[] = [];\n+\tconst failures: string[] = [];\n+\tconst started = performance.now();\n+\tawait Promise.all(\n+\t\tfolders.map(async (dir, w) => {\n+\t\t\tfor (let i = 0; i < PUSHES; i++) {\n+\t\t\t\twriteFileSync(join(dir, `w${w}.txt`), `writer ${w}, push ${i}\\n`);\n+\t\t\t\tconst r = await push(dir, store, `w${w} push ${i}`);\n+\t\t\t\ttimes.push(r.ms);\n+\t\t\t\tif (!r.ok) failures.push(`w${w} push ${i}: ${r.out.trim().split(\"\\n\").slice(-2).join(\" / \")}`);\n+\t\t\t}\n+\t\t}),\n+\t);\n+\tconst total = (performance.now() - started) / 1000;\n+\ttimes.sort((a, b) => a - b);\n+\tconst ms = (n: number) => `${n.toFixed(0)} ms`.padStart(8);\n+\tconsole.log(\n+\t\t`${String(writers).padStart(7)} | ${String(times.length).padStart(6)} | ${ms(pct(times, 0.5))} | ${ms(pct(times, 0.95))} | ${ms(times.at(-1)!)} | ${(times.length / total).toFixed(1).padStart(8)} | ${failures.length}`,\n+\t);\n+\tfor (const f of failures.slice(0, 5)) console.log(`        ${f}`);\n+\tfor (const d of [store, ...folders]) rmSync(d, { recursive: true, force: true, maxRetries: 5 });\n+}"}],"unmatched":[]}