22const { Writable } = require ( 'stream' )
33const http = require ( 'http' )
44const Benchmark = require ( 'benchmark' )
5- const { Client } = require ( '..' )
5+ const { Client, Pool } = require ( '..' )
66
77// # Start the Node.js server
88// node benchmarks/server.js
99//
1010// # Start the benchmarks
1111// node benchmarks/index.js
1212
13- const httpOptions = {
13+ const connections = parseInt ( process . env . CONNECTIONS , 10 ) || 50
14+ const parallelRequests = parseInt ( process . env . PARALLEL , 10 ) || 10
15+ const pipelining = parseInt ( process . env . PIPELINING , 10 ) || 10
16+ const dest = { }
17+
18+ if ( process . env . PORT ) {
19+ dest . port = process . env . PORT
20+ dest . url = `http://localhost:${ process . env . PORT } `
21+ } else {
22+ dest . url = 'http://localhost'
23+ dest . socketPath = '/var/tmp/undici.sock'
24+ }
25+
26+ const httpNoAgent = {
1427 protocol : 'http:' ,
1528 hostname : 'localhost' ,
16- socketPath : '/var/tmp/undici.sock' ,
1729 method : 'GET' ,
1830 path : '/' ,
31+ ...dest
32+ }
33+
34+ const httpOptions = {
35+ ...httpNoAgent ,
1936 agent : new http . Agent ( {
2037 keepAlive : true ,
2138 maxSockets : 1
2239 } )
2340}
2441
42+ const httpOptionsMultiSocket = {
43+ ...httpNoAgent ,
44+ agent : new http . Agent ( {
45+ keepAlive : true ,
46+ maxSockets : connections
47+ } )
48+ }
49+
2550const undiciOptions = {
2651 path : '/' ,
2752 method : 'GET' ,
2853 headersTimeout : 0 ,
2954 bodyTimeout : 0
3055}
3156
32- const client = new Client ( `http:// ${ httpOptions . hostname } ` , {
33- pipelining : 10 ,
34- socketPath : '/var/tmp/undici.sock'
57+ const client = new Client ( httpOptions . url , {
58+ pipelining,
59+ ... dest
3560} )
3661
37- client . on ( 'disconnect' , ( err ) => {
38- throw err
62+ const pool = new Pool ( httpOptions . url , {
63+ pipelining,
64+ connections,
65+ ...dest
3966} )
4067
4168const suite = new Benchmark . Suite ( )
4269
43- Benchmark . options . minSamples = 200
70+ // Benchmark.options.minSamples = 200
4471
4572suite
73+ . add ( 'http - no agent ' , {
74+ defer : true ,
75+ fn : deferred => {
76+ Promise . all ( Array . from ( Array ( parallelRequests ) ) . map ( ( ) => new Promise ( ( resolve ) => {
77+ http . get ( httpOptions , ( res ) => {
78+ res
79+ . pipe ( new Writable ( {
80+ write ( chunk , encoding , callback ) {
81+ callback ( )
82+ }
83+ } ) )
84+ . on ( 'finish' , resolve )
85+ } )
86+ } ) ) ) . then ( ( ) => deferred . resolve ( ) )
87+ }
88+ } )
4689 . add ( 'http - keepalive' , {
4790 defer : true ,
4891 fn : deferred => {
49- Promise . all ( Array . from ( Array ( 10 ) ) . map ( ( ) => new Promise ( ( resolve ) => {
92+ Promise . all ( Array . from ( Array ( parallelRequests ) ) . map ( ( ) => new Promise ( ( resolve ) => {
5093 http . get ( httpOptions , ( res ) => {
5194 res
5295 . pipe ( new Writable ( {
@@ -59,10 +102,26 @@ suite
59102 } ) ) ) . then ( ( ) => deferred . resolve ( ) )
60103 }
61104 } )
105+ . add ( 'http - keepalive - multiple sockets' , {
106+ defer : true ,
107+ fn : deferred => {
108+ Promise . all ( Array . from ( Array ( parallelRequests ) ) . map ( ( ) => new Promise ( ( resolve ) => {
109+ http . get ( httpOptionsMultiSocket , ( res ) => {
110+ res
111+ . pipe ( new Writable ( {
112+ write ( chunk , encoding , callback ) {
113+ callback ( )
114+ }
115+ } ) )
116+ . on ( 'finish' , resolve )
117+ } )
118+ } ) ) ) . then ( ( ) => deferred . resolve ( ) )
119+ }
120+ } )
62121 . add ( 'undici - pipeline' , {
63122 defer : true ,
64123 fn : deferred => {
65- Promise . all ( Array . from ( Array ( 10 ) ) . map ( ( ) => new Promise ( ( resolve ) => {
124+ Promise . all ( Array . from ( Array ( parallelRequests ) ) . map ( ( ) => new Promise ( ( resolve ) => {
66125 client
67126 . pipeline ( undiciOptions , data => {
68127 return data . body
80139 . add ( 'undici - request' , {
81140 defer : true ,
82141 fn : deferred => {
83- Promise . all ( Array . from ( Array ( 10 ) ) . map ( ( ) => new Promise ( ( resolve ) => {
142+ Promise . all ( Array . from ( Array ( parallelRequests ) ) . map ( ( ) => new Promise ( ( resolve ) => {
84143 client
85144 . request ( undiciOptions )
86145 . then ( ( { body } ) => {
@@ -95,10 +154,28 @@ suite
95154 } ) ) ) . then ( ( ) => deferred . resolve ( ) )
96155 }
97156 } )
157+ . add ( 'undici - pool - request - multiple sockets' , {
158+ defer : true ,
159+ fn : deferred => {
160+ Promise . all ( Array . from ( Array ( parallelRequests ) ) . map ( ( ) => new Promise ( ( resolve ) => {
161+ pool
162+ . request ( undiciOptions )
163+ . then ( ( { body } ) => {
164+ body
165+ . pipe ( new Writable ( {
166+ write ( chunk , encoding , callback ) {
167+ callback ( )
168+ }
169+ } ) )
170+ . on ( 'finish' , resolve )
171+ } )
172+ } ) ) ) . then ( ( ) => deferred . resolve ( ) )
173+ }
174+ } )
98175 . add ( 'undici - stream' , {
99176 defer : true ,
100177 fn : deferred => {
101- Promise . all ( Array . from ( Array ( 10 ) ) . map ( ( ) => {
178+ Promise . all ( Array . from ( Array ( parallelRequests ) ) . map ( ( ) => {
102179 return client . stream ( undiciOptions , ( ) => {
103180 return new Writable ( {
104181 write ( chunk , encoding , callback ) {
@@ -112,23 +189,23 @@ suite
112189 . add ( 'undici - dispatch' , {
113190 defer : true ,
114191 fn : deferred => {
115- Promise . all ( Array . from ( Array ( 10 ) ) . map ( ( ) => new Promise ( ( resolve ) => {
192+ Promise . all ( Array . from ( Array ( parallelRequests ) ) . map ( ( ) => new Promise ( ( resolve ) => {
116193 client . dispatch ( undiciOptions , new SimpleRequest ( resolve ) )
117194 } ) ) ) . then ( ( ) => deferred . resolve ( ) )
118195 }
119196 } )
120197 . add ( 'undici - noop' , {
121198 defer : true ,
122199 fn : deferred => {
123- Promise . all ( Array . from ( Array ( 10 ) ) . map ( ( ) => new Promise ( ( resolve ) => {
200+ Promise . all ( Array . from ( Array ( parallelRequests ) ) . map ( ( ) => new Promise ( ( resolve ) => {
124201 client . dispatch ( undiciOptions , new NoopRequest ( resolve ) )
125202 } ) ) ) . then ( ( ) => deferred . resolve ( ) )
126203 }
127204 } )
128205 . on ( 'cycle' , ( { target } ) => {
129- // Multiply results by 10x to get opts/sec since we do 10 requests
206+ // Multiply results by parallelRequests to get opts/sec since we do mutiple requests
130207 // per run.
131- target . hz *= 10
208+ target . hz *= parallelRequests
132209 console . log ( String ( target ) )
133210 } )
134211 . on ( 'complete' , ( ) => {
0 commit comments