Skip to content

Commit 7f154d2

Browse files
committed
refactor(perf): drive the harness CLI via runCmdSync, not spawnSync
Review (P2): repo rule is to spawn processes through src/utils/exec.ts, not node:child_process directly. Switch the perf harness's invokeCli to runCmdSync (allowFailure so non-zero exits are recorded as samples) and add a maxBuffer option to ExecOptions/runCmdSync (snapshot payloads exceed Node's ~1MB default).
1 parent 6b358d5 commit 7f154d2

2 files changed

Lines changed: 22 additions & 9 deletions

File tree

scripts/perf/cli.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { spawnSync } from 'node:child_process';
21
import { performance } from 'node:perf_hooks';
2+
import { runCmdSync } from '../../src/utils/exec.ts';
33
import { resolveCliArgv, REPO_ROOT } from './config.ts';
44
import type { BatchStepSpec } from './scenario.ts';
55
import type { CliResult } from './types.ts';
@@ -36,16 +36,26 @@ function jsonOk(json: unknown): boolean {
3636
export function invokeCli(args: string[], baseFlags: string[]): CliResult {
3737
const full = [...CLI_ARGV, ...args, ...baseFlags, '--json'];
3838
const t0 = performance.now();
39-
const r = spawnSync(process.execPath, full, {
40-
encoding: 'utf8',
41-
cwd: REPO_ROOT,
42-
maxBuffer: MAX_BUFFER,
43-
});
39+
let stdout = '';
40+
let stderr = '';
41+
let exitCode = -1;
42+
try {
43+
// allowFailure so non-zero exits are recorded as samples instead of thrown; maxBuffer
44+
// raised because snapshot payloads exceed Node's ~1MB default.
45+
const r = runCmdSync(process.execPath, full, {
46+
cwd: REPO_ROOT,
47+
maxBuffer: MAX_BUFFER,
48+
allowFailure: true,
49+
});
50+
stdout = r.stdout;
51+
stderr = r.stderr;
52+
exitCode = r.exitCode;
53+
} catch (error) {
54+
// Spawn-level failures (missing executable, timeout) — record as a failed sample.
55+
stderr = error instanceof Error ? error.message : String(error);
56+
}
4457
const wallClockMs = performance.now() - t0;
45-
const stdout = r.stdout ?? '';
46-
const stderr = r.stderr ?? '';
4758
const json = tryParseJson(stdout);
48-
const exitCode = r.status ?? -1;
4959
return { exitCode, wallClockMs, stdout, stderr, json, ok: exitCode === 0 && jsonOk(json) };
5060
}
5161

src/utils/exec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ export type ExecOptions = {
2323
timeoutMs?: number;
2424
detached?: boolean;
2525
signal?: AbortSignal;
26+
/** Max stdout/stderr bytes for synchronous runs (default Node ~1MB). */
27+
maxBuffer?: number;
2628
};
2729

2830
type ExecStreamOptions = ExecOptions & {
@@ -251,6 +253,7 @@ export function runCmdSync(cmd: string, args: string[], options: ExecOptions = {
251253
timeout: normalizeTimeoutMs(options.timeoutMs),
252254
windowsHide: true,
253255
shell: false,
256+
...(options.maxBuffer !== undefined ? { maxBuffer: options.maxBuffer } : {}),
254257
});
255258

256259
if (result.error) {

0 commit comments

Comments
 (0)