Skip to content

Commit bb36df0

Browse files
committed
fix(perf): retain startup benchmark warmups
1 parent a8d4642 commit bb36df0

3 files changed

Lines changed: 96 additions & 28 deletions

File tree

‎scripts/bench-cli-startup.ts‎

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,18 @@ type Sample = {
2525
maxRssMb: number | null;
2626
exitCode: number | null;
2727
signal: string | null;
28+
startedAt?: string;
29+
endedAt?: string;
2830
timedOut?: boolean;
2931
stdoutTail?: string;
3032
stderrTail?: string;
3133
};
3234

35+
type CaseRuns = {
36+
warmupSamples: Sample[];
37+
samples: Sample[];
38+
};
39+
3340
type SummaryStats = {
3441
avg: number;
3542
p50: number;
@@ -58,6 +65,7 @@ type SuiteResult = {
5865
firstOutputBudgetMs: number | null;
5966
exitBudgetMs: number | null;
6067
} | null;
68+
warmupSamples?: Sample[];
6169
samples: Sample[];
6270
summary: CaseSummary;
6371
}>;
@@ -754,6 +762,7 @@ async function runSample(params: {
754762
params.entry,
755763
...params.commandCase.args,
756764
];
765+
const startedAt = new Date();
757766
const started = process.hrtime.bigint();
758767
let firstOutputMs: number | null = null;
759768
let stdout = "";
@@ -798,6 +807,8 @@ async function runSample(params: {
798807
ms,
799808
firstOutputMs,
800809
maxRssMb: parseMaxRssMb(stderr),
810+
startedAt: startedAt.toISOString(),
811+
endedAt: new Date().toISOString(),
801812
...(timedOut ? { timedOut } : {}),
802813
...sample,
803814
});
@@ -956,7 +967,8 @@ async function runCase(params: {
956967
cpuProfDir?: string;
957968
heapProfDir?: string;
958969
rssHookPath: string;
959-
}): Promise<Sample[]> {
970+
}): Promise<CaseRuns> {
971+
const warmupSamples: Sample[] = [];
960972
const samples: Sample[] = [];
961973
const totalRuns = params.warmup + params.runs;
962974
const caseRunRoot =
@@ -967,11 +979,12 @@ async function runCase(params: {
967979
for (let i = 0; i < totalRuns; i += 1) {
968980
const sample = await runSample({ ...params, runRoot: caseRunRoot });
969981
if (i < params.warmup) {
982+
warmupSamples.push(sample);
970983
continue;
971984
}
972985
samples.push(sample);
973986
}
974-
return samples;
987+
return { warmupSamples, samples };
975988
} finally {
976989
if (caseRunRoot) {
977990
rmSync(caseRunRoot, { recursive: true, force: true });
@@ -1064,30 +1077,34 @@ export function collectFailedSamples(result: SuiteResult): string[] {
10641077
for (const commandCase of result.cases) {
10651078
if (commandCase.samples.length === 0) {
10661079
failures.push(`${result.entry} ${commandCase.id}: no measured samples`);
1067-
continue;
10681080
}
1069-
for (const [sampleIndex, sample] of commandCase.samples.entries()) {
1070-
const label = `${result.entry} ${commandCase.id} sample ${sampleIndex + 1}`;
1071-
const expectedExitCodes = new Set(commandCase.expectedExitCodes ?? [0]);
1072-
if (sample.timedOut === true) {
1073-
failures.push(`${label}: timed out`);
1074-
} else if (sample.signal !== null) {
1075-
failures.push(`${label}: exited via signal ${sample.signal}`);
1076-
} else if (!expectedExitCodes.has(sample.exitCode ?? -1)) {
1077-
failures.push(`${label}: exited with code ${String(sample.exitCode)}`);
1078-
} else if (sample.maxRssMb === null) {
1079-
failures.push(`${label}: did not report max RSS`);
1080-
} else if (sample.exitCode !== 0) {
1081-
const output = `${sample.stdoutTail ?? ""}\n${sample.stderrTail ?? ""}`;
1082-
const missing = (commandCase.expectedNonzeroOutputIncludes ?? []).filter(
1083-
(snippet) => !output.includes(snippet),
1084-
);
1085-
if (missing.length > 0) {
1086-
failures.push(
1087-
`${label}: exited with expected code ${String(
1088-
sample.exitCode,
1089-
)} but output did not match expected clean-state markers (${missing.join(", ")})`,
1081+
for (const [sampleKind, samples] of [
1082+
["warmup", commandCase.warmupSamples ?? []],
1083+
["sample", commandCase.samples],
1084+
] as const) {
1085+
for (const [sampleIndex, sample] of samples.entries()) {
1086+
const label = `${result.entry} ${commandCase.id} ${sampleKind} ${sampleIndex + 1}`;
1087+
const expectedExitCodes = new Set(commandCase.expectedExitCodes ?? [0]);
1088+
if (sample.timedOut === true) {
1089+
failures.push(`${label}: timed out`);
1090+
} else if (sample.signal !== null) {
1091+
failures.push(`${label}: exited via signal ${sample.signal}`);
1092+
} else if (!expectedExitCodes.has(sample.exitCode ?? -1)) {
1093+
failures.push(`${label}: exited with code ${String(sample.exitCode)}`);
1094+
} else if (sample.maxRssMb === null) {
1095+
failures.push(`${label}: did not report max RSS`);
1096+
} else if (sample.exitCode !== 0) {
1097+
const output = `${sample.stdoutTail ?? ""}\n${sample.stderrTail ?? ""}`;
1098+
const missing = (commandCase.expectedNonzeroOutputIncludes ?? []).filter(
1099+
(snippet) => !output.includes(snippet),
10901100
);
1101+
if (missing.length > 0) {
1102+
failures.push(
1103+
`${label}: exited with expected code ${String(
1104+
sample.exitCode,
1105+
)} but output did not match expected clean-state markers (${missing.join(", ")})`,
1106+
);
1107+
}
10911108
}
10921109
}
10931110
}
@@ -1102,7 +1119,7 @@ async function buildSuiteResult(params: {
11021119
}): Promise<SuiteResult> {
11031120
const cases = [];
11041121
for (const commandCase of params.options.cases) {
1105-
const samples = await runCase({
1122+
const { warmupSamples, samples } = await runCase({
11061123
entry: params.entry,
11071124
commandCase,
11081125
runs: params.options.runs,
@@ -1129,6 +1146,7 @@ async function buildSuiteResult(params: {
11291146
exitBudgetMs: commandCase.exitBudgetMs ?? null,
11301147
}
11311148
: null,
1149+
warmupSamples,
11321150
samples,
11331151
summary: summarizeSamples(samples),
11341152
});

‎test/scripts/bench-cli-startup.test.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,41 @@ describe("bench-cli-startup", () => {
310310
]);
311311
});
312312

313+
it("retains and validates warmup samples separately from measured samples", () => {
314+
const passingSample = {
315+
ms: 10,
316+
firstOutputMs: 5,
317+
maxRssMb: 50,
318+
exitCode: 0,
319+
signal: null,
320+
startedAt: "2026-08-01T20:00:00.000Z",
321+
endedAt: "2026-08-01T20:00:00.010Z",
322+
};
323+
324+
expect(
325+
testing.collectFailedSamples({
326+
entry: "dist/entry.js",
327+
cases: [
328+
{
329+
id: "gatewayHealthJsonConnected",
330+
name: "gateway health --json (connected)",
331+
args: ["gateway", "health", "--json"],
332+
contract: null,
333+
warmupSamples: [{ ...passingSample, exitCode: 1 }],
334+
samples: [passingSample],
335+
summary: {
336+
sampleCount: 1,
337+
durationMs: { avg: 10, p50: 10, p95: 10, min: 10, max: 10 },
338+
firstOutputMs: { avg: 5, p50: 5, p95: 5, min: 5, max: 5 },
339+
maxRssMb: { avg: 50, p50: 50, p95: 50, min: 50, max: 50 },
340+
exitSummary: "code:0x1",
341+
},
342+
},
343+
],
344+
}),
345+
).toEqual(["dist/entry.js gatewayHealthJsonConnected warmup 1: exited with code 1"]);
346+
});
347+
313348
it("fails reports with samples that did not report RSS", () => {
314349
expect(
315350
testing.collectFailedSamples({

‎test/scripts/cli-startup-bench-spawner.test.ts‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ describe("CLI startup benchmark script spawners", () => {
5151

5252
const runCase = (caseId: string) => {
5353
fs.rmSync(homeLogPath, { force: true });
54+
const reportPath = path.join(tmpDir, `${caseId}.json`);
5455
execFileSync(
5556
process.execPath,
5657
[
@@ -65,6 +66,8 @@ describe("CLI startup benchmark script spawners", () => {
6566
"2",
6667
"--warmup",
6768
"1",
69+
"--output",
70+
reportPath,
6871
],
6972
{
7073
cwd: process.cwd(),
@@ -75,16 +78,28 @@ describe("CLI startup benchmark script spawners", () => {
7578
stdio: "pipe",
7679
},
7780
);
78-
return fs.readFileSync(homeLogPath, "utf8").trim().split("\n");
81+
return {
82+
homes: fs.readFileSync(homeLogPath, "utf8").trim().split("\n"),
83+
report: JSON.parse(fs.readFileSync(reportPath, "utf8")),
84+
};
7985
};
8086

81-
const warmedHomes = runCase("gatewayHealthJsonConnected");
87+
const warmed = runCase("gatewayHealthJsonConnected");
88+
const warmedHomes = warmed.homes;
8289
expect(warmedHomes).toHaveLength(3);
8390
expect(new Set(warmedHomes).size).toBe(1);
8491
expect(warmedHomes.every((home) => !fs.existsSync(home))).toBe(true);
92+
const warmedCase = warmed.report.primary.cases[0];
93+
expect(warmedCase.warmupSamples).toHaveLength(1);
94+
expect(warmedCase.samples).toHaveLength(2);
95+
for (const sample of [...warmedCase.warmupSamples, ...warmedCase.samples]) {
96+
expect(new Date(sample.startedAt).toISOString()).toBe(sample.startedAt);
97+
expect(new Date(sample.endedAt).toISOString()).toBe(sample.endedAt);
98+
expect(Date.parse(sample.endedAt)).toBeGreaterThanOrEqual(Date.parse(sample.startedAt));
99+
}
85100

86101
for (const caseId of ["gatewayHealthJson", "gatewayHealthJsonFirstDevice"]) {
87-
const sampleHomes = runCase(caseId);
102+
const sampleHomes = runCase(caseId).homes;
88103
expect(sampleHomes).toHaveLength(3);
89104
expect(new Set(sampleHomes).size).toBe(3);
90105
expect(sampleHomes.every((home) => !fs.existsSync(home))).toBe(true);

0 commit comments

Comments
 (0)