Skip to content

Commit 1fe4291

Browse files
mcollinacrysmags
authored andcommitted
Use correct http Agent for node-fetch, axios, got and request (nodejs#2629)
* Use correct http Agent for node-fetch, axios, got and request Signed-off-by: Matteo Collina <hello@matteocollina.com> * fixup Signed-off-by: Matteo Collina <hello@matteocollina.com> --------- Signed-off-by: Matteo Collina <hello@matteocollina.com>
1 parent 26b8ebb commit 1fe4291

File tree

2 files changed

+39
-19
lines changed

2 files changed

+39
-19
lines changed

README.md

+15-15
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,21 @@ npm i undici
1818
## Benchmarks
1919

2020
The benchmark is a simple `hello world` [example](benchmarks/benchmark.js) using a
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-
| undici - fetch | 30 | 2081.23 req/sec | ± 2.91 % | + 24.14 % |
27-
| http - no keepalive | 30 | 2477.88 req/sec | ± 2.73 % | + 47.80 % |
28-
| axios | 20 | 2673.40 req/sec | ± 2.54 % | + 59.46 % |
29-
| node-fetch | 10 | 2776.09 req/sec | ± 1.16 % | + 65.59 % |
30-
| request | 10 | 3458.80 req/sec | ± 0.92 % | + 106.31 % |
31-
| http - keepalive | 40 | 4415.13 req/sec | ± 3.00 % | + 163.35 % |
32-
| undici - pipeline | 10 | 5912.29 req/sec | ± 3.00 % | + 252.65 % |
33-
| undici - request | 80 | 7829.95 req/sec | ± 2.92 % | + 367.04 % |
34-
| undici - stream | 65 | 8221.08 req/sec | ± 2.86 % | + 390.37 % |
35-
| undici - dispatch | 60 | 9600.55 req/sec | ± 2.88 % | + 472.65 % |
21+
50 TCP connections with a pipelining depth of 10 running on Node 20.10.0.
22+
23+
Tests Samples Result Tolerance Difference with slowest
24+
|─────────────────────|─────────|─────────────────|───────────|─────────────────────────|
25+
got 45 │ 1661.71 req/sec ± 2.93 % -
26+
│ node-fetch │ 20 │ 2164.81 req/sec ± 2.63 % + 30.28 %
27+
│ undici - fetch │ 35 │ 2274.27 req/sec ± 2.70 % + 36.86 %
28+
│ http - no keepalive │ 15 │ 2376.04 req/sec ± 2.99 % + 42.99 %
29+
│ axios │ 25 │ 2612.93 req/sec ± 2.89 % + 57.24 %
30+
request 40 │ 2712.19 req/sec ± 2.92 % + 63.22 %
31+
http - keepalive 45 │ 4393.25 req/sec ± 2.86 % + 164.38 %
32+
undici - pipeline 45 │ 5484.69 req/sec ± 2.87 % + 230.06 %
33+
undici - request 55 │ 7773.98 req/sec ± 2.93 % + 367.83 %
34+
undici - stream 70 │ 8425.96 req/sec ± 2.91 % + 407.07 %
35+
undici - dispatch 50 │ 9488.99 req/sec ± 2.85 % + 471.04 %
3636

3737
## Quick Start
3838

benchmarks/benchmark.js

+24-4
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,26 @@ const httpKeepAliveOptions = {
6666
})
6767
}
6868

69+
const axiosAgent = new http.Agent({
70+
keepAlive: true,
71+
maxSockets: connections
72+
})
73+
74+
const fetchAgent = new http.Agent({
75+
keepAlive: true,
76+
maxSockets: connections
77+
})
78+
79+
const gotAgent = new http.Agent({
80+
keepAlive: true,
81+
maxSockets: connections
82+
})
83+
84+
const requestAgent = new http.Agent({
85+
keepAlive: true,
86+
maxSockets: connections
87+
})
88+
6989
const undiciOptions = {
7090
path: '/',
7191
method: 'GET',
@@ -280,7 +300,7 @@ if (process.env.PORT) {
280300

281301
experiments['node-fetch'] = () => {
282302
return makeParallelRequests(resolve => {
283-
nodeFetch(dest.url).then(res => {
303+
nodeFetch(dest.url, { agent: fetchAgent }).then(res => {
284304
res.body.pipe(new Writable({
285305
write (chunk, encoding, callback) {
286306
callback()
@@ -292,7 +312,7 @@ if (process.env.PORT) {
292312

293313
experiments.axios = () => {
294314
return makeParallelRequests(resolve => {
295-
axios.get(dest.url, { responseType: 'stream' }).then(res => {
315+
axios.get(dest.url, { responseType: 'stream', httpAgent: axiosAgent }).then(res => {
296316
res.data.pipe(new Writable({
297317
write (chunk, encoding, callback) {
298318
callback()
@@ -304,7 +324,7 @@ if (process.env.PORT) {
304324

305325
experiments.got = () => {
306326
return makeParallelRequests(resolve => {
307-
got.get(dest.url).then(res => {
327+
got.get(dest.url, null, { http: gotAgent }).then(res => {
308328
res.pipe(new Writable({
309329
write (chunk, encoding, callback) {
310330
callback()
@@ -316,7 +336,7 @@ if (process.env.PORT) {
316336

317337
experiments.request = () => {
318338
return makeParallelRequests(resolve => {
319-
request(dest.url).then(res => {
339+
request(dest.url, { agent: requestAgent }).then(res => {
320340
res.pipe(new Writable({
321341
write (chunk, encoding, callback) {
322342
callback()

0 commit comments

Comments
 (0)