Skip to content

Commit dd3ee19

Browse files
committed
fix(perf): give each concurrency sample a full deadline
1 parent b6a445c commit dd3ee19

2 files changed

Lines changed: 59 additions & 18 deletions

File tree

‎scripts/bench-gateway-concurrency.ts‎

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ Options:
190190
--runs <n> Measured gateway runs (default: ${DEFAULT_RUNS})
191191
--warmup <n> Warmup gateway runs (default: ${DEFAULT_WARMUP})
192192
--cadence-ms <ms> Probe cadence (default: ${DEFAULT_CADENCE_MS})
193-
--timeout-ms <ms> Whole benchmark cap, excluding probe warmup (default: ${DEFAULT_TIMEOUT_MS})
193+
--timeout-ms <ms> Per-run cap, excluding probe warmup (default: ${DEFAULT_TIMEOUT_MS})
194194
--entry <path> Gateway CLI entry file (default: ${DEFAULT_ENTRY})
195195
--output <path> Write machine-readable JSON to a file
196196
--json Emit machine-readable JSON
@@ -814,31 +814,43 @@ function summarizeRuns(runs: readonly BenchmarkRun[]) {
814814
};
815815
}
816816

817-
async function main(): Promise<void> {
818-
const argv = process.argv.slice(2);
819-
if (hasHelpFlag(argv)) {
820-
printUsage();
821-
return;
822-
}
823-
const options = parseOptions(argv);
824-
let deadlineAt = performance.now() + options.timeoutMs;
817+
async function runBenchmarkSamples(params: {
818+
now?: () => number;
819+
onProgress?: (message: string) => void;
820+
options: CliOptions;
821+
runSample?: typeof runGatewaySample;
822+
}): Promise<BenchmarkRun[]> {
823+
const now = params.now ?? performance.now.bind(performance);
824+
const runSample = params.runSample ?? runGatewaySample;
825825
const runs: BenchmarkRun[] = [];
826-
const total = options.runs + options.warmup;
826+
const total = params.options.runs + params.options.warmup;
827827
for (let index = 0; index < total; index += 1) {
828-
requireRemainingMs(deadlineAt, "starting gateway run");
829-
const run = await runGatewaySample({ ...options, deadlineAt });
830-
deadlineAt += run.probeWarmup.durationMs;
831-
if (index >= options.warmup) {
828+
// Each sample gets the same budget so earlier runs cannot shrink later agent waits.
829+
// runGatewaySample extends this deadline by its probe warmup before load starts.
830+
const deadlineAt = now() + params.options.timeoutMs;
831+
const run = await runSample({ ...params.options, deadlineAt });
832+
if (index >= params.options.warmup) {
832833
runs.push(run);
833-
console.error(
834-
`[bench-gateway-concurrency] run ${runs.length}/${options.runs}: turns=${run.turnCount} samples=${run.readyz.length} duration=${run.durationMs.toFixed(1)}ms`,
834+
params.onProgress?.(
835+
`[bench-gateway-concurrency] run ${runs.length}/${params.options.runs}: turns=${run.turnCount} samples=${run.readyz.length} duration=${run.durationMs.toFixed(1)}ms`,
835836
);
836837
} else {
837-
console.error(
838-
`[bench-gateway-concurrency] warmup ${index + 1}/${options.warmup}: duration=${run.durationMs.toFixed(1)}ms`,
838+
params.onProgress?.(
839+
`[bench-gateway-concurrency] warmup ${index + 1}/${params.options.warmup}: duration=${run.durationMs.toFixed(1)}ms`,
839840
);
840841
}
841842
}
843+
return runs;
844+
}
845+
846+
async function main(): Promise<void> {
847+
const argv = process.argv.slice(2);
848+
if (hasHelpFlag(argv)) {
849+
printUsage();
850+
return;
851+
}
852+
const options = parseOptions(argv);
853+
const runs = await runBenchmarkSamples({ onProgress: console.error, options });
842854
const payload = {
843855
cadenceMs: options.cadenceMs,
844856
concurrency: options.concurrency,
@@ -862,6 +874,7 @@ export const testing = {
862874
formatProbeFailure,
863875
formatRunFailure,
864876
requestHttp,
877+
runBenchmarkSamples,
865878
runTurn,
866879
sampleGateway,
867880
summarizeNumbers,

‎test/scripts/bench-gateway-concurrency.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,34 @@ describe("gateway concurrency benchmark script", () => {
7676
expect(wait?.timeoutMs).toBeLessThanOrEqual(2_000);
7777
});
7878

79+
it("gives every gateway sample a fresh pre-warmup timeout budget", async () => {
80+
const deadlines: number[] = [];
81+
const sample = {
82+
controlUi: [],
83+
durationMs: 10,
84+
probeWarmup: { durationMs: 2, samples: [] },
85+
readyz: [],
86+
sessionsList: [],
87+
turnCount: 8,
88+
turnsDurationMs: 5,
89+
};
90+
91+
const runs = await testing.runBenchmarkSamples({
92+
now: (() => {
93+
const values = [1_000, 9_000];
94+
return () => values.shift() ?? 9_000;
95+
})(),
96+
options: testing.parseOptions(["--runs", "1", "--warmup", "1", "--timeout-ms", "5000"]),
97+
runSample: async ({ deadlineAt }) => {
98+
deadlines.push(deadlineAt);
99+
return sample;
100+
},
101+
});
102+
103+
expect(deadlines).toEqual([6_000, 14_000]);
104+
expect(runs).toEqual([sample]);
105+
});
106+
79107
it("preserves HTTP and RPC failures in baseline probe diagnostics", async () => {
80108
const probeOrder: string[] = [];
81109
const server = createHttpServer((req, res) => {

0 commit comments

Comments
 (0)