// Time real, uninstrumented CLI processes and a complete stdio MCP request. A separate launch reports // each CLI process's maximum RSS. MCP's parent and tool process overlap, so report bounds and a sampled total. import { spawnSync } from 'node:child_process'; import { mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; import os from 'node:os'; import path from 'node:path'; import { coin, sizes } from './bench-sources.mjs'; const measure = 'import { writeSync } from "node:fs"; process.on("exit", () => writeSync(3, String(process.resourceUsage().maxRSS)));'; const preload = 'data:text/javascript,' + encodeURIComponent(measure); // Each OS high-water mark includes that process's worker threads. The sum is a conservative // upper bound, since peaks need not coincide. For the sampled total, carry each reading until // the next one from that process, and remove it at exit. Sampling is outside all timed calls. function processMemory(directory, coordinator) { const events = [], processes = []; for (const file of readdirSync(directory)) { const { pid, ppid, peakKB, samples } = JSON.parse(readFileSync(path.join(directory, file), 'utf8')); if (!(peakKB > 0) || !samples?.length) throw new Error('incomplete benchmark memory record for PID ' + pid); processes.push({ pid, ppid, peakRssMB: peakKB / 1024 }); let previous = 0; for (const [time, bytes] of samples) { events.push([time, bytes - previous]); previous = bytes; } events.push([samples.at(-1)[0], -previous]); } if (!processes.some(({ pid }) => pid === coordinator)) throw new Error('missing coordinator memory record'); let current = 0, sampled = 0; for (const [, delta] of events.sort((a, b) => a[0] - b[0])) { current += delta; sampled = Math.max(sampled, current); } const peaks = processes.map(item => item.peakRssMB); return { coordinator, processes, lowerBoundMB: Math.max(...peaks), sampledPeakMB: sampled / 2 ** 20, upperBoundMB: peaks.reduce((total, peak) => total + peak, 0) }; } function execute(state, memory) { const dir = !memory && process.env.PIXELBOLT_BENCH_PROFILE_DIR; const flags = dir ? ['--cpu-prof', '--heap-prof', '--cpu-prof-dir=' + dir, '--heap-prof-dir=' + dir] : []; const tree = memory && state.mcp ? mkdtempSync(path.join(state.cwd, 'memory-')) : undefined; const single = memory && !tree; const env = tree ? { ...process.env, PIXELBOLT_BENCH_MEMORY_DIR: tree, NODE_OPTIONS: [process.env.NODE_OPTIONS, '--import=' + new URL('./bench-process-memory.mjs', import.meta.url).href].filter(Boolean).join(' ') } : process.env; const args = [...flags, ...(single ? ['--import', preload] : []), state.cli, ...state.args]; const child = spawnSync(process.execPath, args, { input: state.input, cwd: state.cwd, env, encoding: 'utf8', windowsHide: true, stdio: single ? ['pipe', 'pipe', 'pipe', 'pipe'] : ['pipe', 'pipe', 'pipe'], maxBuffer: 32 * 1024 * 1024 }); if (child.error) throw child.error; if (child.status !== 0 || child.stderr) throw new Error(child.stderr || 'unexpected CLI exit ' + child.status); if (state.mcp) { const [init, call] = child.stdout.trim().split(/\r?\n/).map(line => JSON.parse(line)); if (init?.result?.protocolVersion !== '2025-11-25' || call?.id !== 2 || !call.result || call.result.isError) throw new Error(child.stdout); } const memoryTree = tree ? processMemory(tree, child.pid) : undefined; const peak = single ? Number(child.output[3]) / 1024 : memoryTree?.upperBoundMB; if (memory && !(peak > 0)) throw new Error('missing target process maximum RSS'); return { stdout: child.stdout, stderr: child.stderr, status: child.status, peak, memoryTree }; } function interfaceCase(kind, size) { const mcp = kind === 'mcp', help = kind === 'help'; return { id: help ? 'cli/help' : (mcp ? 'mcp/validate' : 'cli/validate') + '/size-' + size, area: mcp ? 'mcp' : 'cli', size, what: mcp ? 'stdio initialize and pixel_validate (' + size + ')' : help ? 'cold CLI help' : 'cold CLI validate (' + size + ')', setup: async (_load, root) => { const cwd = mkdtempSync(path.join(os.tmpdir(), 'pixelbolt-bench-interface-')); try { const recipe = coin(size), cli = path.join(root, 'bin/pixelbolt.js'); const args = mcp ? ['mcp', '--root', '.', '--out', 'out', '--no-skill'] : help ? ['help'] : ['validate', 'coin.json']; const input = mcp ? [ { jsonrpc: '2.0', id: 1, method: 'initialize', params: { protocolVersion: '2025-11-25', capabilities: {}, clientInfo: { name: 'bench', version: '1' } } }, { jsonrpc: '2.0', id: 2, method: 'tools/call', params: { name: 'pixel_validate', arguments: { project: recipe } } } ].map(message => JSON.stringify(message)).join('\n') + '\n' : ''; if (!mcp && !help) writeFileSync(path.join(cwd, 'coin.json'), JSON.stringify(recipe)); const state = { cwd, cli, args, input, mcp, frames: help ? 1 : recipe.frames.length }; const { peak, memoryTree } = execute(state, true); return { ...state, childPeakMB: peak, memoryTree }; } catch (error) { rmSync(cwd, { recursive: true, force: true }); throw error; } }, run: state => { const { stdout, stderr, status } = execute(state, false); return { frames: state.frames, value: { status, stdout, stderr }, childPeakMB: state.childPeakMB }; }, cleanup: ({ cwd }) => rmSync(cwd, { recursive: true, force: true }) }; } export const interfaceCases = () => [interfaceCase('help', 'shipped'), ...sizes.flatMap(size => ['validate', 'mcp'].map(kind => interfaceCase(kind, size)))];