@@ -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,
0 commit comments