Skip to content

Commit 0fe2a63

Browse files
authored
Add more libraries to benchmarks (#2614)
1 parent 1b1ae6b commit 0fe2a63

3 files changed

Lines changed: 92 additions & 33 deletions

File tree

README.md

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,34 +18,31 @@ npm i undici
1818
## Benchmarks
1919

2020
The benchmark is a simple `hello world` [example](benchmarks/benchmark.js) using a
21-
number of unix sockets (connections) with a pipelining depth of 10 running on Node 20.6.0.
22-
23-
### Connections 1
24-
25-
26-
| Tests | Samples | Result | Tolerance | Difference with slowest |
27-
|---------------------|---------|---------------|-----------|-------------------------|
28-
| http - no keepalive | 15 | 5.32 req/sec | ± 2.61 % | - |
29-
| http - keepalive | 10 | 5.35 req/sec | ± 2.47 % | + 0.44 % |
30-
| undici - fetch | 15 | 41.85 req/sec | ± 2.49 % | + 686.04 % |
31-
| undici - pipeline | 40 | 50.36 req/sec | ± 2.77 % | + 845.92 % |
32-
| undici - stream | 15 | 60.58 req/sec | ± 2.75 % | + 1037.72 % |
33-
| undici - request | 10 | 61.19 req/sec | ± 2.60 % | + 1049.24 % |
34-
| undici - dispatch | 20 | 64.84 req/sec | ± 2.81 % | + 1117.81 % |
35-
36-
37-
### Connections 50
38-
39-
| Tests | Samples | Result | Tolerance | Difference with slowest |
40-
|---------------------|---------|------------------|-----------|-------------------------|
41-
| undici - fetch | 30 | 2107.19 req/sec | ± 2.69 % | - |
42-
| http - no keepalive | 10 | 2698.90 req/sec | ± 2.68 % | + 28.08 % |
43-
| http - keepalive | 10 | 4639.49 req/sec | ± 2.55 % | + 120.17 % |
44-
| undici - pipeline | 40 | 6123.33 req/sec | ± 2.97 % | + 190.59 % |
45-
| undici - stream | 50 | 9426.51 req/sec | ± 2.92 % | + 347.35 % |
46-
| undici - request | 10 | 10162.88 req/sec | ± 2.13 % | + 382.29 % |
47-
| undici - dispatch | 50 | 11191.11 req/sec | ± 2.98 % | + 431.09 % |
48-
21+
50 TCP connections with a pipelining depth of 10 running on Node 20.6.0.
22+
23+
│ Tests │ Samples │ Result │ Tolerance │ Difference with slowest │
24+
|─────────────────────|─────────|─────────────────|───────────|─────────────────────────|
25+
│ got │ 40 │ 1676.51 req/sec │ ± 2.93 % │ - │
26+
|─────────────────────|─────────|─────────────────|───────────|─────────────────────────|
27+
│ undici - fetch │ 30 │ 2081.23 req/sec │ ± 2.91 % │ + 24.14 % │
28+
|─────────────────────|─────────|─────────────────|───────────|─────────────────────────|
29+
│ http - no keepalive │ 30 │ 2477.88 req/sec │ ± 2.73 % │ + 47.80 % │
30+
|─────────────────────|─────────|─────────────────|───────────|─────────────────────────|
31+
│ axios │ 20 │ 2673.40 req/sec │ ± 2.54 % │ + 59.46 % │
32+
|─────────────────────|─────────|─────────────────|───────────|─────────────────────────|
33+
│ node-fetch │ 10 │ 2776.09 req/sec │ ± 1.16 % │ + 65.59 % │
34+
|─────────────────────|─────────|─────────────────|───────────|─────────────────────────|
35+
│ request │ 10 │ 3458.80 req/sec │ ± 0.92 % │ + 106.31 % │
36+
|─────────────────────|─────────|─────────────────|───────────|─────────────────────────|
37+
│ http - keepalive │ 40 │ 4415.13 req/sec │ ± 3.00 % │ + 163.35 % │
38+
|─────────────────────|─────────|─────────────────|───────────|─────────────────────────|
39+
│ undici - pipeline │ 10 │ 5912.29 req/sec │ ± 3.00 % │ + 252.65 % │
40+
|─────────────────────|─────────|─────────────────|───────────|─────────────────────────|
41+
│ undici - request │ 80 │ 7829.95 req/sec │ ± 2.92 % │ + 367.04 % │
42+
|─────────────────────|─────────|─────────────────|───────────|─────────────────────────|
43+
│ undici - stream │ 65 │ 8221.08 req/sec │ ± 2.86 % │ + 390.37 % │
44+
|─────────────────────|─────────|─────────────────|───────────|─────────────────────────|
45+
│ undici - dispatch │ 60 │ 9600.55 req/sec │ ± 2.88 % │ + 472.65 % │
4946

5047
## Quick Start
5148

@@ -62,9 +59,7 @@ const {
6259
console.log('response received', statusCode)
6360
console.log('headers', headers)
6461

65-
for await (const data of body) {
66-
console.log('data', data)
67-
}
62+
for await (const data of body) { console.log('data', data) }
6863

6964
console.log('trailers', trailers)
7065
```

benchmarks/benchmark.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ const { isMainThread } = require('worker_threads')
99

1010
const { Pool, Client, fetch, Agent, setGlobalDispatcher } = require('..')
1111

12+
let nodeFetch
13+
const axios = require('axios')
14+
let got
15+
16+
const util = require('util')
17+
const _request = require('request')
18+
const request = util.promisify(_request)
19+
1220
const iterations = (parseInt(process.env.SAMPLES, 10) || 10) + 1
1321
const errorThreshold = parseInt(process.env.ERROR_THRESHOLD, 10) || 3
1422
const connections = parseInt(process.env.CONNECTIONS, 10) || 50
@@ -269,10 +277,62 @@ if (process.env.PORT) {
269277
}).catch(console.log)
270278
})
271279
}
280+
281+
experiments['node-fetch'] = () => {
282+
return makeParallelRequests(resolve => {
283+
nodeFetch(dest.url).then(res => {
284+
res.body.pipe(new Writable({
285+
write (chunk, encoding, callback) {
286+
callback()
287+
}
288+
})).on('finish', resolve)
289+
}).catch(console.log)
290+
})
291+
}
292+
293+
experiments.axios = () => {
294+
return makeParallelRequests(resolve => {
295+
axios.get(dest.url, { responseType: 'stream' }).then(res => {
296+
res.data.pipe(new Writable({
297+
write (chunk, encoding, callback) {
298+
callback()
299+
}
300+
})).on('finish', resolve)
301+
}).catch(console.log)
302+
})
303+
}
304+
305+
experiments.got = () => {
306+
return makeParallelRequests(resolve => {
307+
got.get(dest.url).then(res => {
308+
res.pipe(new Writable({
309+
write (chunk, encoding, callback) {
310+
callback()
311+
}
312+
})).on('finish', resolve)
313+
}).catch(console.log)
314+
})
315+
}
316+
317+
experiments.request = () => {
318+
return makeParallelRequests(resolve => {
319+
request(dest.url).then(res => {
320+
res.pipe(new Writable({
321+
write (chunk, encoding, callback) {
322+
callback()
323+
}
324+
})).on('finish', resolve)
325+
}).catch(console.log)
326+
})
327+
}
272328
}
273329

274330
async function main () {
275331
const { cronometro } = await import('cronometro')
332+
const _nodeFetch = await import('node-fetch')
333+
nodeFetch = _nodeFetch.default
334+
const _got = await import('got')
335+
got = _got.default
276336

277337
cronometro(
278338
experiments,

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
"bench": "PORT=3042 concurrently -k -s first npm:bench:server npm:bench:run",
9393
"bench:server": "node benchmarks/server.js",
9494
"prebench:run": "node benchmarks/wait.js",
95-
"bench:run": "CONNECTIONS=1 node benchmarks/benchmark.js; CONNECTIONS=50 node benchmarks/benchmark.js",
95+
"bench:run": "SAMPLES=100 CONNECTIONS=50 node benchmarks/benchmark.js",
9696
"serve:website": "docsify serve .",
9797
"prepare": "husky install",
9898
"fuzz": "jsfuzz test/fuzzing/fuzz.js corpus"
@@ -136,7 +136,11 @@
136136
"tsd": "^0.30.1",
137137
"typescript": "^5.0.2",
138138
"wait-on": "^7.0.1",
139-
"ws": "^8.11.0"
139+
"ws": "^8.11.0",
140+
"axios": "^1.6.5",
141+
"got": "^14.0.0",
142+
"node-fetch": "^3.3.2",
143+
"request": "^2.88.2"
140144
},
141145
"engines": {
142146
"node": ">=18.0"

0 commit comments

Comments
 (0)