{"draft":null,"from":203,"to":318,"files":[{"path":"scripts/local-measure.ts","added":123,"removed":0,"diff":"--- /dev/null\n+++ b/scripts/local-measure.ts\n@@ -0,0 +1,123 @@\n+// Time and memory of a local project's commands (abso init --local) on a small and a large history:\n+//\n+//   node scripts/local-measure.ts [versions ...]        (default: 20 1000)\n+//\n+// Each history is built in this process through the client's main(); then every command is run as its own\n+// `node client/abso.ts` process, the way a person or an agent runs it, five times. It prints the median and slowest\n+// wall time, and the largest peak memory (maxRSS) of those runs, with the store's size on disk.\n+\n+import { spawnSync } from \"node:child_process\";\n+import { mkdtempSync, readdirSync, rmSync, statSync, writeFileSync } from \"node:fs\";\n+import { tmpdir } from \"node:os\";\n+import { join, resolve } from \"node:path\";\n+import { fileURLToPath, pathToFileURL } 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 FILES = 40;\n+const RUNS = 5;\n+\n+const sizes = process.argv.slice(2).map(Number);\n+const histories = sizes.length > 0 ? sizes : [20, 1000];\n+\n+function sizeOf(dir: string): number {\n+\tlet total = 0;\n+\tfor (const name of readdirSync(dir)) {\n+\t\tconst path = join(dir, name);\n+\t\tconst s = statSync(path);\n+\t\ttotal += s.isDirectory() ? sizeOf(path) : s.size;\n+\t}\n+\treturn total;\n+}\n+\n+const lines = (file: number, version: number) =>\n+\tArray.from({ length: 30 }, (_, i) =>\n+\t\ti === version % 30 ? `line ${i} of file ${file}, version ${version}` : `line ${i} of file ${file}`,\n+\t)\n+\t\t.join(\"\\n\")\n+\t\t.concat(\"\\n\");\n+\n+async function build(versions: number): Promise<{ store: string; alice: string; bob: string }> {\n+\tconst store = mkdtempSync(join(tmpdir(), \"abso-measure-store-\"));\n+\tconst alice = mkdtempSync(join(tmpdir(), \"abso-measure-alice-\"));\n+\tconst bob = mkdtempSync(join(tmpdir(), \"abso-measure-bob-\"));\n+\tconst env = { ABSO_STORE: store, ABSO_SERVERS_FILE: \"no-servers.json\" };\n+\tconst run = async (cwd: string, ...argv: string[]) => {\n+\t\tconst r = await main(argv, { cwd, env, write: () => {} });\n+\t\tif (r.exit !== 0) throw new Error(`${argv.join(\" \")}: ${r.stdout}${r.stderr}`);\n+\t};\n+\tfor (let f = 0; f < FILES; f++) writeFileSync(join(alice, `f${f}.txt`), lines(f, 0));\n+\tawait run(alice, \"init\", \"--local\", \"--project\", \"measure\", \"--agent\", \"alice\", \"--as-is\");\n+\tfor (let v = 1; v < versions; v++) {\n+\t\t// Each version changes three files, as a small piece of work does.\n+\t\tfor (const f of [v % FILES, (v * 7) % FILES, (v * 13) % FILES])\n+\t\t\twriteFileSync(join(alice, `f${f}.txt`), lines(f, v));\n+\t\tawait run(alice, \"push\", \"-m\", `Version ${v}`);\n+\t}\n+\tawait run(bob, \"join\", \"measure\", \"--local\", \"--agent\", \"bob\", \"--dir\", bob);\n+\treturn { store, alice, bob };\n+}\n+\n+function measure(cwd: string, store: string, argv: string[]): { median: number; slowest: number; rss: number } {\n+\tconst times: number[] = [];\n+\tlet rss = 0;\n+\tconst preload = join(tmpdir(), `abso-measure-rss-${process.pid}.mjs`);\n+\tconst out = join(tmpdir(), `abso-measure-rss-${process.pid}.json`);\n+\twriteFileSync(\n+\t\tpreload,\n+\t\t`import { writeFileSync } from \"node:fs\";\\nprocess.on(\"exit\", () => writeFileSync(${JSON.stringify(out)}, String(process.resourceUsage().maxRSS)));\\n`,\n+\t);\n+\tfor (let i = 0; i < RUNS; i++) {\n+\t\tconst started = performance.now();\n+\t\tconst r = spawnSync(process.execPath, [\"--import\", pathToFileURL(preload).href, ENTRY, ...argv], {\n+\t\t\tcwd,\n+\t\t\tenv: { ...process.env, ABSO_STORE: store, ABSO_SERVERS_FILE: \"no-servers.json\" },\n+\t\t\tencoding: \"utf8\",\n+\t\t});\n+\t\ttimes.push(performance.now() - started);\n+\t\tif (r.status !== 0 && argv[0] !== \"push\") throw new Error(`${argv.join(\" \")}: ${r.stdout}${r.stderr}`);\n+\t\trss = Math.max(\n+\t\t\trss,\n+\t\t\tNumber(\n+\t\t\t\tspawnSync(\n+\t\t\t\t\tprocess.execPath,\n+\t\t\t\t\t[\"-e\", `process.stdout.write(require(\"fs\").readFileSync(${JSON.stringify(out)}, \"utf8\"))`],\n+\t\t\t\t\t{ encoding: \"utf8\" },\n+\t\t\t\t).stdout,\n+\t\t\t),\n+\t\t);\n+\t}\n+\ttimes.sort((a, b) => a - b);\n+\treturn { median: times[Math.floor(RUNS / 2)]!, slowest: times[RUNS - 1]!, rss };\n+}\n+\n+const kb = (n: number) => `${(n / 1024).toFixed(0)} MB`;\n+const ms = (n: number) => `${n.toFixed(0)} ms`;\n+console.log(\n+\t`Node ${process.versions.node}, ${FILES} files of 30 lines, three files changed per version, ${RUNS} runs each.`,\n+);\n+console.log(\"history   | command              | median   | slowest  | peak memory\");\n+for (const versions of histories) {\n+\tconst started = performance.now();\n+\tconst { store, alice, bob } = await build(versions);\n+\tconst built = performance.now() - started;\n+\tconst steps: [string, string, string[]][] = [\n+\t\t[\"status\", alice, [\"status\"]],\n+\t\t[\"log\", alice, [\"log\"]],\n+\t\t[\"blame\", alice, [\"blame\", \"f0.txt\"]],\n+\t\t[\"pull (no change)\", bob, [\"pull\"]],\n+\t\t[\"record + push\", alice, [\"push\", \"-m\", \"One more\"]],\n+\t];\n+\tfor (const [name, cwd, argv] of steps) {\n+\t\tif (argv[0] === \"push\") writeFileSync(join(cwd, \"f1.txt\"), lines(1, versions + Math.random()));\n+\t\tconst m = measure(cwd, store, argv);\n+\t\tconsole.log(\n+\t\t\t`${String(versions).padStart(5)} v   | ${name.padEnd(20)} | ${ms(m.median).padStart(8)} | ${ms(m.slowest).padStart(8)} | ${kb(m.rss)}`,\n+\t\t);\n+\t}\n+\tconsole.log(\n+\t\t`${String(versions).padStart(5)} v   | store on disk ${(sizeOf(store) / 1048576).toFixed(1)} MB, built in-process in ${(built / 1000).toFixed(1)} s`,\n+\t);\n+\tfor (const d of [store, alice, bob]) rmSync(d, { recursive: true, force: true, maxRetries: 5 });\n+}"}],"unmatched":[]}