|
| 1 | +#!/usr/bin/env bun |
| 2 | +/** |
| 3 | + * CL-10666 before/after measurement: session-store per-turn I/O. |
| 4 | + * |
| 5 | + * One machine, one scenario: a fresh session dir plus N simulated reactor |
| 6 | + * cycles (load, writePrompt, writeTurns, commit) through the optimized |
| 7 | + * context store. Every node fs.promises call the store stack makes is |
| 8 | + * counted (split by whether the path is under .git), along with wall time, |
| 9 | + * so the per-turn walks, re-parses, and re-stringified comparisons become an |
| 10 | + * honest delta between two builds on the same host. |
| 11 | + * |
| 12 | + * Usage: |
| 13 | + * bun scripts/context-store-perf.ts [turnCount] |
| 14 | + * bun scripts/context-store-perf.ts [turnCount] --json |
| 15 | + */ |
| 16 | + |
| 17 | +import fs from "node:fs"; |
| 18 | +import path from "node:path"; |
| 19 | +import type { ConversationTurn } from "@intx/types/runtime"; |
| 20 | +import { createOptimizedContextStore } from "../src/session/optimized-context-store.js"; |
| 21 | + |
| 22 | +const COUNTED_OPS = [ |
| 23 | + "access", |
| 24 | + "copyFile", |
| 25 | + "lstat", |
| 26 | + "mkdir", |
| 27 | + "open", |
| 28 | + "opendir", |
| 29 | + "readFile", |
| 30 | + "readdir", |
| 31 | + "readlink", |
| 32 | + "rename", |
| 33 | + "rmdir", |
| 34 | + "stat", |
| 35 | + "truncate", |
| 36 | + "unlink", |
| 37 | + "writeFile", |
| 38 | +] as const; |
| 39 | + |
| 40 | +const counts: Record<string, number> = {}; |
| 41 | +const gitPaths: Record<string, number> = {}; |
| 42 | + |
| 43 | +function bump(table: Record<string, number>, op: string): void { |
| 44 | + table[op] = (table[op] ?? 0) + 1; |
| 45 | +} |
| 46 | + |
| 47 | +function installCounters(): void { |
| 48 | + const target = fs.promises as unknown as Record< |
| 49 | + string, |
| 50 | + (...args: unknown[]) => unknown |
| 51 | + >; |
| 52 | + for (const op of COUNTED_OPS) { |
| 53 | + const original = target[op]; |
| 54 | + if (typeof original !== "function") continue; |
| 55 | + target[op] = function counted(this: unknown, ...args: unknown[]) { |
| 56 | + const first = args[0]; |
| 57 | + if (typeof first === "string" && first.includes(`/.git/`)) { |
| 58 | + bump(gitPaths, op); |
| 59 | + } |
| 60 | + bump(counts, op); |
| 61 | + return original.apply(this, args); |
| 62 | + }; |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +function turn(text: string): ConversationTurn { |
| 67 | + return { role: "user", content: [{ type: "text", text }], timestamp: 1 }; |
| 68 | +} |
| 69 | + |
| 70 | +function assistantTurn(text: string): ConversationTurn { |
| 71 | + return { |
| 72 | + role: "assistant", |
| 73 | + content: [{ type: "text", text }], |
| 74 | + timestamp: 2, |
| 75 | + }; |
| 76 | +} |
| 77 | + |
| 78 | +function turnBody(index: number): string { |
| 79 | + // ~1.5 KB of filler plus a marker line, close to a small unit of context. |
| 80 | + return ( |
| 81 | + `turn ${index}: ` + |
| 82 | + "boundary-check padding. ".repeat(60) + |
| 83 | + `end-of-turn ${index}` |
| 84 | + ); |
| 85 | +} |
| 86 | + |
| 87 | +async function runScenario(dir: string, turnCount: number): Promise<number> { |
| 88 | + const store = await createOptimizedContextStore(dir); |
| 89 | + const history: ConversationTurn[] = []; |
| 90 | + for (let index = 0; index < turnCount; index++) { |
| 91 | + history.push(turn(turnBody(index))); |
| 92 | + if (index % 4 === 1) history.push(assistantTurn(`reply to ${index}`)); |
| 93 | + await store.load(); |
| 94 | + await store.writePrompt(history); |
| 95 | + await store.writeTurns(history); |
| 96 | + await store.commit({ message: `checkpoint ${index}` }); |
| 97 | + } |
| 98 | + const fresh = await createOptimizedContextStore(dir); |
| 99 | + return (await fresh.load()).turns.length; |
| 100 | +} |
| 101 | + |
| 102 | +function sortedEntries(table: Record<string, number>): [string, number][] { |
| 103 | + return Object.entries(table).sort((a, b) => b[1] - a[1]); |
| 104 | +} |
| 105 | + |
| 106 | +async function main(argv: string[]): Promise<number> { |
| 107 | + const arg = argv[2]; |
| 108 | + const turnCount = |
| 109 | + arg !== undefined && !arg.startsWith("-") ? Number(arg) : 40; |
| 110 | + if (!Number.isFinite(turnCount) || turnCount < 1) { |
| 111 | + console.error( |
| 112 | + "usage: bun scripts/context-store-perf.ts [turnCount] [--json]", |
| 113 | + ); |
| 114 | + return 1; |
| 115 | + } |
| 116 | + installCounters(); |
| 117 | + |
| 118 | + const dir = await fs.promises.mkdtemp( |
| 119 | + path.join(process.cwd(), "context-store-perf-"), |
| 120 | + ); |
| 121 | + const started = performance.now(); |
| 122 | + const loadedTurns = await runScenario(dir, turnCount); |
| 123 | + const wallMs = Math.round(performance.now() - started); |
| 124 | + |
| 125 | + const totalOps = Object.values(counts).reduce((sum, n) => sum + n, 0); |
| 126 | + const gitOps = Object.values(gitPaths).reduce((sum, n) => sum + n, 0); |
| 127 | + |
| 128 | + const asJson = argv.includes("--json"); |
| 129 | + if (asJson) { |
| 130 | + console.log( |
| 131 | + JSON.stringify( |
| 132 | + { |
| 133 | + turnCount, |
| 134 | + wallMs, |
| 135 | + loadedTurns, |
| 136 | + totalFsOps: totalOps, |
| 137 | + gitPathFsOps: gitOps, |
| 138 | + fsOps: counts, |
| 139 | + gitPathFsOpsByName: gitPaths, |
| 140 | + perTurn: { |
| 141 | + wallMs: +(wallMs / turnCount).toFixed(2), |
| 142 | + fsOps: +(totalOps / turnCount).toFixed(2), |
| 143 | + }, |
| 144 | + }, |
| 145 | + null, |
| 146 | + 2, |
| 147 | + ), |
| 148 | + ); |
| 149 | + } else { |
| 150 | + console.log( |
| 151 | + `scenario: ${turnCount} turns, one store instance (fresh dir); verified ${loadedTurns} turns at the end`, |
| 152 | + ); |
| 153 | + console.log( |
| 154 | + `wall: ${wallMs} ms (${(wallMs / turnCount).toFixed(2)} ms/turn)`, |
| 155 | + ); |
| 156 | + console.log( |
| 157 | + `fs ops: ${totalOps} total (${(totalOps / turnCount).toFixed(2)}/turn), of which ${gitOps} under .git`, |
| 158 | + ); |
| 159 | + for (const [op, n] of sortedEntries(counts)) { |
| 160 | + console.log(` ${op}: ${n}`); |
| 161 | + } |
| 162 | + for (const [op, n] of sortedEntries(gitPaths)) { |
| 163 | + console.log(` (.git) ${op}: ${n}`); |
| 164 | + } |
| 165 | + } |
| 166 | + await fs.promises.rm(dir, { recursive: true, force: true }); |
| 167 | + return 0; |
| 168 | +} |
| 169 | + |
| 170 | +process.exit(await main(process.argv)); |
0 commit comments