Skip to content

Commit

Permalink
Use correct http Agent for node-fetch, axios, got and request (#2629)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
mcollina committed Jan 19, 2024
1 parent 12d39f7 commit 14757d5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 19 deletions.
30 changes: 15 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@ npm i undici
## Benchmarks

The benchmark is a simple `hello world` [example](benchmarks/benchmark.js) using a
50 TCP connections with a pipelining depth of 10 running on Node 20.6.0.

| Tests | Samples | Result | Tolerance | Difference with slowest |
|---------------------|---------|-----------------|-----------|-------------------------|
| got | 40 | 1676.51 req/sec | ± 2.93 % | - |
| undici - fetch | 30 | 2081.23 req/sec | ± 2.91 % | + 24.14 % |
| http - no keepalive | 30 | 2477.88 req/sec | ± 2.73 % | + 47.80 % |
| axios | 20 | 2673.40 req/sec | ± 2.54 % | + 59.46 % |
| node-fetch | 10 | 2776.09 req/sec | ± 1.16 % | + 65.59 % |
| request | 10 | 3458.80 req/sec | ± 0.92 % | + 106.31 % |
| http - keepalive | 40 | 4415.13 req/sec | ± 3.00 % | + 163.35 % |
| undici - pipeline | 10 | 5912.29 req/sec | ± 3.00 % | + 252.65 % |
| undici - request | 80 | 7829.95 req/sec | ± 2.92 % | + 367.04 % |
| undici - stream | 65 | 8221.08 req/sec | ± 2.86 % | + 390.37 % |
| undici - dispatch | 60 | 9600.55 req/sec | ± 2.88 % | + 472.65 % |
50 TCP connections with a pipelining depth of 10 running on Node 20.10.0.

Tests Samples Result Tolerance Difference with slowest
|─────────────────────|─────────|─────────────────|───────────|─────────────────────────|
got 45 │ 1661.71 req/sec ± 2.93 % -
│ node-fetch │ 20 │ 2164.81 req/sec ± 2.63 % + 30.28 %
│ undici - fetch │ 35 │ 2274.27 req/sec ± 2.70 % + 36.86 %
│ http - no keepalive │ 15 │ 2376.04 req/sec ± 2.99 % + 42.99 %
│ axios │ 25 │ 2612.93 req/sec ± 2.89 % + 57.24 %
request 40 │ 2712.19 req/sec ± 2.92 % + 63.22 %
http - keepalive 45 │ 4393.25 req/sec ± 2.86 % + 164.38 %
undici - pipeline 45 │ 5484.69 req/sec ± 2.87 % + 230.06 %
undici - request 55 │ 7773.98 req/sec ± 2.93 % + 367.83 %
undici - stream 70 │ 8425.96 req/sec ± 2.91 % + 407.07 %
undici - dispatch 50 │ 9488.99 req/sec ± 2.85 % + 471.04 %

## Quick Start

Expand Down
28 changes: 24 additions & 4 deletions benchmarks/benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,26 @@ const httpKeepAliveOptions = {
})
}

const axiosAgent = new http.Agent({
keepAlive: true,
maxSockets: connections
})

const fetchAgent = new http.Agent({
keepAlive: true,
maxSockets: connections
})

const gotAgent = new http.Agent({
keepAlive: true,
maxSockets: connections
})

const requestAgent = new http.Agent({
keepAlive: true,
maxSockets: connections
})

const undiciOptions = {
path: '/',
method: 'GET',
Expand Down Expand Up @@ -280,7 +300,7 @@ if (process.env.PORT) {

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

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

experiments.got = () => {
return makeParallelRequests(resolve => {
got.get(dest.url).then(res => {
got.get(dest.url, null, { http: gotAgent }).then(res => {
res.pipe(new Writable({
write (chunk, encoding, callback) {
callback()
Expand All @@ -316,7 +336,7 @@ if (process.env.PORT) {

experiments.request = () => {
return makeParallelRequests(resolve => {
request(dest.url).then(res => {
request(dest.url, { agent: requestAgent }).then(res => {
res.pipe(new Writable({
write (chunk, encoding, callback) {
callback()
Expand Down

0 comments on commit 14757d5

Please sign in to comment.