@@ -7,7 +7,7 @@ import { tmpdir } from "node:os";
77import path from "node:path" ;
88import { performance } from "node:perf_hooks" ;
99import { pathToFileURL } from "node:url" ;
10- import { PROTOCOL_VERSION } from "../packages/gateway-protocol/src/index.js " ;
10+ import { PROTOCOL_VERSION } from "../packages/gateway-protocol/src/version.ts " ;
1111import { applyMockOpenAiModelConfig } from "./e2e/lib/fixtures/mock-openai-config.mjs" ;
1212import { delay , stopChild } from "./lib/gateway-bench-child.ts" ;
1313import { getFreePort } from "./lib/gateway-bench-probes.ts" ;
@@ -39,6 +39,7 @@ type MetricSummary = {
3939
4040type TimedProbe = {
4141 atMs : number ;
42+ error : string | null ;
4243 latencyMs : number ;
4344 ok : boolean ;
4445} ;
@@ -52,10 +53,20 @@ type ReadyProbe = TimedProbe & {
5253 utilization : number | null ;
5354} ;
5455
56+ type ControlUiProbe = TimedProbe & {
57+ status : number ;
58+ } ;
59+
60+ type GatewaySample = {
61+ controlUi : ControlUiProbe ;
62+ readyz : ReadyProbe ;
63+ sessionsList : TimedProbe ;
64+ } ;
65+
5566type GatewayRpc = < T > ( method : string , params : unknown , timeoutMs ?: number ) => Promise < T > ;
5667
5768type BenchmarkRun = {
58- controlUi : TimedProbe [ ] ;
69+ controlUi : ControlUiProbe [ ] ;
5970 durationMs : number ;
6071 readyz : ReadyProbe [ ] ;
6172 sessionsList : TimedProbe [ ] ;
@@ -164,7 +175,7 @@ function printUsage(): void {
164175
165176Usage:
166177 pnpm test:gateway:concurrency -- [options]
167- node --import tsx scripts/bench-gateway-concurrency.ts [options]
178+ node scripts/bench-gateway-concurrency.ts [options]
168179
169180Options:
170181 --concurrency <n> Concurrent synthetic streaming turns (default: ${ DEFAULT_CONCURRENCY } )
@@ -264,6 +275,29 @@ function numberOrNull(value: unknown): number | null {
264275 return typeof value === "number" && Number . isFinite ( value ) ? value : null ;
265276}
266277
278+ function describeProbeError ( error : unknown ) : string {
279+ const message = error instanceof Error ? error . message : String ( error ) ;
280+ return message . slice ( 0 , 500 ) ;
281+ }
282+
283+ function formatProbeResult ( name : string , probe : TimedProbe & { status ?: number } ) : string {
284+ const status = probe . status === undefined ? ( probe . ok ? "ok" : "failed" ) : probe . status ;
285+ return `${ name } (ok=${ probe . ok } status=${ status } error=${ probe . error ? JSON . stringify ( probe . error ) : "none" } latencyMs=${ probe . latencyMs . toFixed ( 1 ) } )` ;
286+ }
287+
288+ function assertBaselineProbes ( baseline : GatewaySample ) : void {
289+ if ( baseline . readyz . ok && baseline . sessionsList . ok && baseline . controlUi . ok ) {
290+ return ;
291+ }
292+ throw new Error (
293+ `gateway probes did not pass before concurrent load: ${ [
294+ formatProbeResult ( "readyz" , baseline . readyz ) ,
295+ formatProbeResult ( "sessionsList" , baseline . sessionsList ) ,
296+ formatProbeResult ( "controlUi" , baseline . controlUi ) ,
297+ ] . join ( "; " ) } `,
298+ ) ;
299+ }
300+
267301function captureChildOutput ( child : ChildProcessWithoutNullStreams ) : ( ) => string {
268302 let output = "" ;
269303 const append = ( chunk : Buffer ) => {
@@ -425,7 +459,8 @@ async function sampleGateway(params: {
425459 port : number ;
426460 rpc : GatewayRpc ;
427461 runStartedAt : number ;
428- } ) : Promise < { controlUi : TimedProbe ; readyz : ReadyProbe ; sessionsList : TimedProbe } > {
462+ serial ?: boolean ;
463+ } ) : Promise < GatewaySample > {
429464 const atMs = performance . now ( ) - params . runStartedAt ;
430465 const safeHttpProbe = async ( pathValue : string , accept : string ) => {
431466 const startedAt = performance . now ( ) ;
@@ -437,30 +472,38 @@ async function sampleGateway(params: {
437472 path : pathValue ,
438473 port : params . port ,
439474 } ) ) ,
475+ error : null ,
440476 ok : true ,
441477 } ;
442- } catch {
478+ } catch ( error ) {
443479 return {
444480 body : "" ,
481+ error : describeProbeError ( error ) ,
445482 latencyMs : performance . now ( ) - startedAt ,
446483 ok : false ,
447484 status : 0 ,
448485 } ;
449486 }
450487 } ;
451- const [ readyz , controlUi , sessions ] = await Promise . all ( [
452- safeHttpProbe ( "/readyz" , "application/json" ) ,
453- safeHttpProbe ( "/" , "text/html" ) ,
454- ( async ( ) => {
455- const startedAt = performance . now ( ) ;
456- try {
457- const payload = await params . rpc ( "sessions.list" , { } , HTTP_TIMEOUT_MS ) ;
458- return { latencyMs : performance . now ( ) - startedAt , ok : true , payload } ;
459- } catch {
460- return { latencyMs : performance . now ( ) - startedAt , ok : false , payload : null } ;
461- }
462- } ) ( ) ,
463- ] ) ;
488+ const probeReadyz = ( ) => safeHttpProbe ( "/readyz" , "application/json" ) ;
489+ const probeControlUi = ( ) => safeHttpProbe ( "/" , "text/html" ) ;
490+ const probeSessions = async ( ) => {
491+ const startedAt = performance . now ( ) ;
492+ try {
493+ const payload = await params . rpc ( "sessions.list" , { } , HTTP_TIMEOUT_MS ) ;
494+ return { error : null , latencyMs : performance . now ( ) - startedAt , ok : true , payload } ;
495+ } catch ( error ) {
496+ return {
497+ error : describeProbeError ( error ) ,
498+ latencyMs : performance . now ( ) - startedAt ,
499+ ok : false ,
500+ payload : null ,
501+ } ;
502+ }
503+ } ;
504+ const [ readyz , controlUi , sessions ] = params . serial
505+ ? [ await probeReadyz ( ) , await probeControlUi ( ) , await probeSessions ( ) ]
506+ : await Promise . all ( [ probeReadyz ( ) , probeControlUi ( ) , probeSessions ( ) ] ) ;
464507 const readyBody = ( ( ) => {
465508 if ( readyz . status !== 200 ) {
466509 return { } ;
@@ -475,11 +518,18 @@ async function sampleGateway(params: {
475518 return {
476519 controlUi : {
477520 atMs,
521+ error :
522+ controlUi . error ??
523+ ( controlUi . status === 200 && ! controlUi . body . includes ( "<html" )
524+ ? "response body did not contain <html"
525+ : null ) ,
478526 latencyMs : controlUi . latencyMs ,
479527 ok : controlUi . ok && controlUi . status === 200 && controlUi . body . includes ( "<html" ) ,
528+ status : controlUi . status ,
480529 } ,
481530 readyz : {
482531 atMs,
532+ error : readyz . error ,
483533 latencyMs : readyz . latencyMs ,
484534 ok : readyz . ok && readyz . status === 200 ,
485535 status : readyz . status ,
@@ -489,7 +539,12 @@ async function sampleGateway(params: {
489539 utilization : numberOrNull ( eventLoop ?. utilization ) ,
490540 cpuCoreRatio : numberOrNull ( eventLoop ?. cpuCoreRatio ) ,
491541 } ,
492- sessionsList : { atMs, latencyMs : sessions . latencyMs , ok : sessions . ok } ,
542+ sessionsList : {
543+ atMs,
544+ error : sessions . error ,
545+ latencyMs : sessions . latencyMs ,
546+ ok : sessions . ok ,
547+ } ,
493548 } ;
494549}
495550
@@ -553,12 +608,11 @@ async function runGatewaySample(options: {
553608 port,
554609 rpc,
555610 runStartedAt,
611+ serial : true ,
556612 } ) ;
557- if ( ! baseline . readyz . ok || ! baseline . sessionsList . ok || ! baseline . controlUi . ok ) {
558- throw new Error ( "gateway probes did not pass before concurrent load" ) ;
559- }
613+ assertBaselineProbes ( baseline ) ;
560614
561- const controlUi : TimedProbe [ ] = [ ] ;
615+ const controlUi : ControlUiProbe [ ] = [ ] ;
562616 const readyz : ReadyProbe [ ] = [ ] ;
563617 const sessionsList : TimedProbe [ ] = [ ] ;
564618 let turnsDone = false ;
@@ -700,7 +754,9 @@ async function main(): Promise<void> {
700754
701755export const testing = {
702756 parseOptions,
757+ assertBaselineProbes,
703758 runTurn,
759+ sampleGateway,
704760 summarizeNumbers,
705761 summarizeRuns,
706762} ;
0 commit comments