|
| 1 | +'use strict'; |
| 2 | +import { URL } from 'url'; |
| 3 | +import axios from 'axios'; |
| 4 | +import fetch from 'node-fetch'; |
| 5 | +import got from '../source'; |
| 6 | +import PromisableRequest from '../source/as-promise/core'; |
| 7 | +import Request, { kIsNormalizedAlready } from '../source/core'; |
| 8 | +import https = require('https'); |
| 9 | +import Benchmark = require('benchmark'); |
| 10 | +import request = require('request'); |
| 11 | + |
| 12 | +const { normalizeArguments } = PromisableRequest; |
| 13 | + |
| 14 | +// Configuration |
| 15 | +const httpsAgent = new https.Agent({ |
| 16 | + keepAlive: true, |
| 17 | + rejectUnauthorized: false, |
| 18 | +}); |
| 19 | + |
| 20 | +const url = new URL('https://127.0.0.1:8080'); |
| 21 | +const urlString = url.toString(); |
| 22 | + |
| 23 | +const gotOptions = { |
| 24 | + agent: { |
| 25 | + https: httpsAgent, |
| 26 | + }, |
| 27 | + https: { |
| 28 | + rejectUnauthorized: false, |
| 29 | + }, |
| 30 | + retry: 0, |
| 31 | +}; |
| 32 | + |
| 33 | +const normalizedGotOptions = normalizeArguments(url, gotOptions); |
| 34 | +normalizedGotOptions[kIsNormalizedAlready] = true; |
| 35 | + |
| 36 | +const requestOptions = { |
| 37 | + strictSSL: false, |
| 38 | + agent: httpsAgent, |
| 39 | +}; |
| 40 | + |
| 41 | +const fetchOptions = { |
| 42 | + agent: httpsAgent, |
| 43 | +}; |
| 44 | + |
| 45 | +const axiosOptions = { |
| 46 | + url: urlString, |
| 47 | + httpsAgent, |
| 48 | + https: { |
| 49 | + rejectUnauthorized: false, |
| 50 | + }, |
| 51 | +}; |
| 52 | + |
| 53 | +const axiosStreamOptions: typeof axiosOptions & { responseType: 'stream' } = { |
| 54 | + ...axiosOptions, |
| 55 | + responseType: 'stream', |
| 56 | +}; |
| 57 | + |
| 58 | +const httpsOptions = { |
| 59 | + https: { |
| 60 | + rejectUnauthorized: false, |
| 61 | + }, |
| 62 | + agent: httpsAgent, |
| 63 | +}; |
| 64 | + |
| 65 | +const suite = new Benchmark.Suite(); |
| 66 | + |
| 67 | +// Benchmarking |
| 68 | +suite |
| 69 | + .add('got - promise', { |
| 70 | + defer: true, |
| 71 | + fn: async (deferred: { resolve: () => void }) => { |
| 72 | + await got(url, gotOptions); |
| 73 | + deferred.resolve(); |
| 74 | + }, |
| 75 | + }) |
| 76 | + .add('got - stream', { |
| 77 | + defer: true, |
| 78 | + fn: (deferred: { resolve: () => void }) => { |
| 79 | + got |
| 80 | + .stream(url, gotOptions) |
| 81 | + .resume() |
| 82 | + .once('end', () => { |
| 83 | + deferred.resolve(); |
| 84 | + }); |
| 85 | + }, |
| 86 | + }) |
| 87 | + .add('got - promise core', { |
| 88 | + defer: true, |
| 89 | + fn: (deferred: { resolve: () => void }) => { |
| 90 | + const stream = new PromisableRequest(url, gotOptions); |
| 91 | + stream.resume().once('end', () => { |
| 92 | + deferred.resolve(); |
| 93 | + }); |
| 94 | + }, |
| 95 | + }) |
| 96 | + .add('got - stream core', { |
| 97 | + defer: true, |
| 98 | + fn: (deferred: { resolve: () => void }) => { |
| 99 | + const stream = new Request(url, gotOptions); |
| 100 | + stream.resume().once('end', () => { |
| 101 | + deferred.resolve(); |
| 102 | + }); |
| 103 | + }, |
| 104 | + }) |
| 105 | + .add('got - stream core - normalized options', { |
| 106 | + defer: true, |
| 107 | + fn: (deferred: { resolve: () => void }) => { |
| 108 | + const stream = new Request(undefined as any, normalizedGotOptions); |
| 109 | + stream.resume().once('end', () => { |
| 110 | + deferred.resolve(); |
| 111 | + }); |
| 112 | + }, |
| 113 | + }) |
| 114 | + .add('request - callback', { |
| 115 | + defer: true, |
| 116 | + fn: (deferred: { resolve: () => void }) => { |
| 117 | + request(urlString, requestOptions, (error: Error) => { |
| 118 | + if (error) { |
| 119 | + throw error; |
| 120 | + } |
| 121 | + |
| 122 | + deferred.resolve(); |
| 123 | + }); |
| 124 | + }, |
| 125 | + }) |
| 126 | + .add('request - stream', { |
| 127 | + defer: true, |
| 128 | + fn: (deferred: { resolve: () => void }) => { |
| 129 | + const stream = request(urlString, requestOptions); |
| 130 | + stream.resume(); |
| 131 | + stream.once('end', () => { |
| 132 | + deferred.resolve(); |
| 133 | + }); |
| 134 | + }, |
| 135 | + }) |
| 136 | + .add('node-fetch - promise', { |
| 137 | + defer: true, |
| 138 | + fn: async (deferred: { resolve: () => void }) => { |
| 139 | + const response = await fetch(url, fetchOptions); |
| 140 | + await response.text(); |
| 141 | + |
| 142 | + deferred.resolve(); |
| 143 | + }, |
| 144 | + }) |
| 145 | + .add('node-fetch - stream', { |
| 146 | + defer: true, |
| 147 | + fn: async (deferred: { resolve: () => void }) => { |
| 148 | + const { body } = await fetch(url, fetchOptions); |
| 149 | + |
| 150 | + body.resume(); |
| 151 | + body.once('end', () => { |
| 152 | + deferred.resolve(); |
| 153 | + }); |
| 154 | + }, |
| 155 | + }) |
| 156 | + .add('axios - promise', { |
| 157 | + defer: true, |
| 158 | + fn: async (deferred: { resolve: () => void }) => { |
| 159 | + await axios.request(axiosOptions); |
| 160 | + deferred.resolve(); |
| 161 | + }, |
| 162 | + }) |
| 163 | + .add('axios - stream', { |
| 164 | + defer: true, |
| 165 | + fn: async (deferred: { resolve: () => void }) => { |
| 166 | + const { data } = await axios.request(axiosStreamOptions); |
| 167 | + data.resume(); |
| 168 | + data.once('end', () => { |
| 169 | + deferred.resolve(); |
| 170 | + }); |
| 171 | + }, |
| 172 | + }) |
| 173 | + .add('https - stream', { |
| 174 | + defer: true, |
| 175 | + fn: (deferred: { resolve: () => void }) => { |
| 176 | + https |
| 177 | + .request(urlString, httpsOptions, (response) => { |
| 178 | + response.resume(); |
| 179 | + response.once('end', () => { |
| 180 | + deferred.resolve(); |
| 181 | + }); |
| 182 | + }) |
| 183 | + .end(); |
| 184 | + }, |
| 185 | + }) |
| 186 | + .on('cycle', (event: Benchmark.Event) => { |
| 187 | + console.log(String(event.target)); |
| 188 | + }) |
| 189 | + .on('complete', function (this: any) { |
| 190 | + console.log(`Fastest is ${this.filter('fastest').map('name') as string}`); |
| 191 | + |
| 192 | + internalBenchmark(); |
| 193 | + }) |
| 194 | + .run(); |
| 195 | + |
| 196 | +const internalBenchmark = (): void => { |
| 197 | + console.log(); |
| 198 | + |
| 199 | + const internalSuite = new Benchmark.Suite(); |
| 200 | + internalSuite |
| 201 | + .add('got - normalize options', { |
| 202 | + fn: () => { |
| 203 | + normalizeArguments(url, gotOptions); |
| 204 | + }, |
| 205 | + }) |
| 206 | + .on('cycle', (event: Benchmark.Event) => { |
| 207 | + console.log(String(event.target)); |
| 208 | + }); |
| 209 | + |
| 210 | + internalSuite.run(); |
| 211 | +}; |
| 212 | + |
| 213 | +// Results (i7-7700k, CPU governor: performance): |
| 214 | +// got - promise x 3,204 ops/sec ±5.27% (73 runs sampled) |
| 215 | +// got - stream x 5,045 ops/sec ±3.85% (77 runs sampled) |
| 216 | +// got - promise core x 6,499 ops/sec ±3.67% (77 runs sampled) |
| 217 | +// got - stream core x 7,047 ops/sec ±2.32% (83 runs sampled) |
| 218 | +// got - stream core - normalized options x 7,313 ops/sec ±2.79% (85 runs sampled) |
| 219 | +// request - callback x 6,918 ops/sec ±5.76% (73 runs sampled) |
| 220 | +// request - stream x 7,746 ops/sec ±3.20% (82 runs sampled) |
| 221 | +// node-fetch - promise x 7,011 ops/sec ±7.54% (75 runs sampled) |
| 222 | +// node-fetch - stream x 7,941 ops/sec ±4.52% (82 runs sampled) |
| 223 | +// axios - promise x 6,788 ops/sec ±3.32% (80 runs sampled) |
| 224 | +// axios - stream x 8,584 ops/sec ±2.23% (81 runs sampled) |
| 225 | +// https - stream x 10,465 ops/sec ±2.89% (73 runs sampled) |
| 226 | +// Fastest is https - stream |
| 227 | + |
| 228 | +// got - normalize options x 166,389 ops/sec ±0.63% (91 runs sampled) |
0 commit comments