- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/scripts/local-writers.ts?format=json
87 linespinch 100%Last change pinch in version 289, 1 version changed it
1// A local project's store lock under load: 1, 4 and 16 writers pushing to one local project at once.
2//
3// node scripts/local-writers.ts [writers ...] (default: 1 4 16; PUSHES=10 pushes per writer)
4//
5// Each writer is its own agent, in its own folder, changing its own file, so nothing collides: what's measured is the
6// store's lock (one SQLite write transaction per call). Every push is its own `node client/abso.ts push` process, as
7// an agent runs it. It prints each push's wall time (median, 95th percentile, slowest), the pushes per second across
8// all writers, and any push that failed.
10import { spawn } from "node:child_process";
11import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
12import { tmpdir } from "node:os";
13import { join, resolve } from "node:path";
14import { fileURLToPath } from "node:url";
15import { main } from "../client/src/main.ts";
17const ROOT = resolve(fileURLToPath(new URL("..", import.meta.url)));
18const ENTRY = join(ROOT, "client", "abso.ts");
19const PUSHES = Number(process.env.PUSHES ?? 10);
20const counts = process.argv.slice(2).map(Number);
22function push(cwd: string, store: string, message: string): Promise<{ ms: number; ok: boolean; out: string }> {
23 const started = performance.now();
24 return new Promise((done) => {
25 const child = spawn(process.execPath, [ENTRY, "push", "-m", message], {
26 cwd,
27 env: { ...process.env, ABSO_STORE: store, ABSO_SERVERS_FILE: "no-servers.json" },
28 });
29 let out = "";
30 child.stdout.on("data", (d) => {
31 out += d;
32 });
33 child.stderr.on("data", (d) => {
34 out += d;
35 });
36 child.on("close", (code) => done({ ms: performance.now() - started, ok: code === 0, out }));
37 });
38}
40const pct = (sorted: number[], p: number) => sorted[Math.min(sorted.length - 1, Math.floor(sorted.length * p))]!;
41console.log(`Node ${process.versions.node}, ${PUSHES} pushes per writer, each writer its own agent and file.`);
42console.log("writers | pushes | median | p95 | slowest | pushes/s | failed");
43for (const writers of counts.length > 0 ? counts : [1, 4, 16]) {
44 const store = mkdtempSync(join(tmpdir(), "abso-writers-store-"));
45 const folders: string[] = [];
46 const env = { ABSO_STORE: store, ABSO_SERVERS_FILE: "no-servers.json" };
47 const first = mkdtempSync(join(tmpdir(), "abso-writers-w0-"));
48 writeFileSync(join(first, "readme.txt"), "writers\n");
49 const init = await main(["init", "--local", "--project", "writers", "--agent", "w0", "--as-is"], {
50 cwd: first,
51 env,
52 write: () => {},
53 });
54 if (init.exit !== 0) throw new Error(init.stdout + init.stderr);
55 folders.push(first);
56 for (let w = 1; w < writers; w++) {
57 const dir = mkdtempSync(join(tmpdir(), `abso-writers-w${w}-`));
58 const joined = await main(["join", "writers", "--local", "--agent", `w${w}`, "--dir", dir], {
59 cwd: dir,
60 env,
61 write: () => {},
62 });
63 if (joined.exit !== 0) throw new Error(joined.stdout + joined.stderr);
64 folders.push(dir);
65 }
66 const times: number[] = [];
67 const failures: string[] = [];
68 const started = performance.now();
69 await Promise.all(
70 folders.map(async (dir, w) => {
71 for (let i = 0; i < PUSHES; i++) {
72 writeFileSync(join(dir, `w${w}.txt`), `writer ${w}, push ${i}\n`);
73 const r = await push(dir, store, `w${w} push ${i}`);
74 times.push(r.ms);
75 if (!r.ok) failures.push(`w${w} push ${i}: ${r.out.trim().split("\n").slice(-2).join(" / ")}`);
76 }
77 }),
78 );
79 const total = (performance.now() - started) / 1000;
80 times.sort((a, b) => a - b);
81 const ms = (n: number) => `${n.toFixed(0)} ms`.padStart(8);
82 console.log(
83 `${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}`,
84 );
85 for (const f of failures.slice(0, 5)) console.log(` ${f}`);
86 for (const d of [store, ...folders]) rmSync(d, { recursive: true, force: true, maxRetries: 5 });
87}