1- import { describe , expect , it } from 'bun:test'
2- import { ohaArgs } from './drivers'
1+ import { describe , expect , it , spyOn } from 'bun:test'
2+ import { DRIVERS , ohaArgs } from './drivers'
33
44describe ( 'oha command' , ( ) => {
55 it ( 'applies one global fixed request rate with latency correction' , ( ) => {
@@ -18,3 +18,42 @@ describe('oha command', () => {
1818 ] )
1919 } )
2020} )
21+
22+ describe ( 'autocannon warmup' , ( ) => {
23+ it . each ( [ 0 , 3 ] ) ( 'discards %s seconds of warmup without changing workers' , async ( warmupSeconds ) => {
24+ const output = ( requests : number ) => ( {
25+ stdout : new Response ( JSON . stringify ( { requests : { total : requests , average : requests } } ) ) . body ,
26+ stderr : new Response ( '' ) . body ,
27+ exited : Promise . resolve ( 0 ) ,
28+ } ) as unknown as ReturnType < typeof Bun . spawn >
29+ const spawn = spyOn ( Bun , 'spawn' )
30+ if ( warmupSeconds > 0 ) spawn . mockReturnValueOnce ( output ( 999 ) )
31+ spawn . mockReturnValueOnce ( output ( 42 ) )
32+ try {
33+ const result = await DRIVERS . find ( driver => driver . name === 'autocannon' ) ! . run ( {
34+ url : 'http://127.0.0.1:39400/bench/echo' ,
35+ method : 'POST' ,
36+ body : '{"name":"bench","count":7}' ,
37+ headers : { 'content-type' : 'application/json' , 'x-bench' : 'preserved' } ,
38+ connections : 7 ,
39+ warmupSeconds,
40+ durationSeconds : 11 ,
41+ } )
42+ expect ( result . requests ) . toBe ( 42 )
43+ expect ( JSON . parse ( result . raw ) . requests . total ) . toBe ( 42 )
44+ const commands = spawn . mock . calls . map ( call => call [ 0 ] as string [ ] )
45+ expect ( commands . map ( args => args [ args . indexOf ( '-d' ) + 1 ] ) ) . toEqual ( warmupSeconds > 0 ? [ '3' , '11' ] : [ '11' ] )
46+ for ( const args of commands ) {
47+ expect ( args ) . not . toContain ( '-w' )
48+ expect ( args [ args . indexOf ( '-c' ) + 1 ] ) . toBe ( '7' )
49+ expect ( args [ args . indexOf ( '-m' ) + 1 ] ) . toBe ( 'POST' )
50+ expect ( args [ args . indexOf ( '-b' ) + 1 ] ) . toBe ( '{"name":"bench","count":7}' )
51+ expect ( args ) . toContain ( 'content-type: application/json' )
52+ expect ( args ) . toContain ( 'x-bench: preserved' )
53+ }
54+ }
55+ finally {
56+ spawn . mockRestore ( )
57+ }
58+ } )
59+ } )
0 commit comments