fix(bench): clean timed-out sample process groups

This commit is contained in:
Vincent Koc
2026-06-20 12:30:28 +02:00
parent a1d278b174
commit 602bc0baa9
2 changed files with 184 additions and 22 deletions

View File

@@ -29,6 +29,15 @@ function withEnv<T>(env: Record<string, string | undefined>, callback: () => T):
}
}
function isProcessAlive(pid: number): boolean {
try {
process.kill(pid, 0);
return true;
} catch {
return false;
}
}
describe("bench-cli-startup", () => {
it("rejects unknown CLI options before running benchmarks", () => {
expect(() => testing.validateCliArgs(["--wat"])).toThrow("Unknown argument: --wat");
@@ -49,6 +58,71 @@ describe("bench-cli-startup", () => {
expect(result.stderr).not.toContain("\n at ");
});
it.runIf(process.platform !== "win32")(
"cleans timed-out benchmark process groups when the leader exits first",
() => {
const tempDirs = createTempDirTracker();
const tmpDir = tempDirs.make("openclaw-cli-startup-timeout-group-");
const entryPath = join(tmpDir, "entry.mjs");
const childPidPath = join(tmpDir, "child.pid");
let childPid = 0;
try {
writeFileSync(
entryPath,
[
"import { spawn } from 'node:child_process';",
"import { writeFileSync } from 'node:fs';",
"process.on('SIGTERM', () => process.exit(0));",
"const child = spawn(process.execPath, [",
" '-e',",
" \"process.on('SIGTERM',()=>{});setInterval(()=>{},1000);\",",
"], { stdio: 'ignore' });",
`writeFileSync(${JSON.stringify(childPidPath)}, String(child.pid));`,
"setInterval(() => {}, 1000);",
"",
].join("\n"),
"utf8",
);
const result = spawnSync(
process.execPath,
[
"--import",
"tsx",
"scripts/bench-cli-startup.ts",
"--entry",
entryPath,
"--case",
"version",
"--runs",
"1",
"--warmup",
"0",
"--timeout-ms",
"100",
"--json",
],
{
cwd: join(__dirname, "../.."),
encoding: "utf8",
timeout: 8_000,
},
);
childPid = Number(readFileSync(childPidPath, "utf8"));
expect(result.status).toBe(1);
expect(result.signal).toBeNull();
expect(result.stderr).toContain("version sample 1: timed out");
expect(isProcessAlive(childPid)).toBe(false);
} finally {
if (childPid && isProcessAlive(childPid)) {
process.kill(childPid, "SIGKILL");
}
tempDirs.cleanup();
}
},
);
it("writes compare-mode JSON output and creates parent directories", () => {
const tempDirs = createTempDirTracker();
const tmpDir = tempDirs.make("openclaw-cli-startup-compare-output-");