|
| 1 | +'use strict'; |
| 2 | +import {URL} from 'url'; |
| 3 | +import https = require('https'); |
| 4 | +import axios from 'axios'; |
| 5 | +import Benchmark = require('benchmark'); |
| 6 | +import fetch from 'node-fetch'; |
| 7 | +import request = require('request'); |
| 8 | +import got from '../source'; |
| 9 | +import PromisableRequest from '../source/as-promise/core'; |
| 10 | +import Request, {kIsNormalizedAlready} from '../source/core'; |
| 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 | + rejectUnauthorized: false, |
| 28 | + retry: 0 |
| 29 | +}; |
| 30 | + |
| 31 | +const normalizedGotOptions = normalizeArguments(url, gotOptions); |
| 32 | +normalizedGotOptions[kIsNormalizedAlready] = true; |
| 33 | + |
| 34 | +const requestOptions = { |
| 35 | + strictSSL: false, |
| 36 | + agent: httpsAgent |
| 37 | +}; |
| 38 | + |
| 39 | +const fetchOptions = { |
| 40 | + agent: httpsAgent |
| 41 | +}; |
| 42 | + |
| 43 | +const axiosOptions = { |
| 44 | + url: urlString, |
| 45 | + httpsAgent, |
| 46 | + rejectUnauthorized: false |
| 47 | +}; |
| 48 | + |
| 49 | +const axiosStreamOptions: typeof axiosOptions & {responseType: 'stream'} = { |
| 50 | + ...axiosOptions, |
| 51 | + responseType: 'stream' |
| 52 | +}; |
| 53 | + |
| 54 | +const httpsOptions = { |
| 55 | + rejectUnauthorized: false, |
| 56 | + agent: httpsAgent |
| 57 | +}; |
| 58 | + |
| 59 | +const suite = new Benchmark.Suite(); |
| 60 | + |
| 61 | +// Benchmarking |
| 62 | +suite.add('got - promise', { |
| 63 | + defer: true, |
| 64 | + fn: async (deferred: {resolve(): void}) => { |
| 65 | + await got(url, gotOptions); |
| 66 | + deferred.resolve(); |
| 67 | + } |
| 68 | +}).add('got - stream', { |
| 69 | + defer: true, |
| 70 | + fn: async (deferred: {resolve(): void}) => { |
| 71 | + got.stream(url, gotOptions).resume().once('end', () => { |
| 72 | + deferred.resolve(); |
| 73 | + }); |
| 74 | + } |
| 75 | +}).add('got - promise core', { |
| 76 | + defer: true, |
| 77 | + fn: async (deferred: {resolve(): void}) => { |
| 78 | + const stream = new PromisableRequest(url, gotOptions); |
| 79 | + stream.resume().once('end', () => { |
| 80 | + deferred.resolve(); |
| 81 | + }); |
| 82 | + } |
| 83 | +}).add('got - stream core', { |
| 84 | + defer: true, |
| 85 | + fn: async (deferred: {resolve(): void}) => { |
| 86 | + const stream = new Request(url, gotOptions); |
| 87 | + stream.resume().once('end', () => { |
| 88 | + deferred.resolve(); |
| 89 | + }); |
| 90 | + } |
| 91 | +}).add('got - stream core - normalized options', { |
| 92 | + defer: true, |
| 93 | + fn: async (deferred: {resolve(): void}) => { |
| 94 | + const stream = new Request(undefined as any, normalizedGotOptions); |
| 95 | + stream.resume().once('end', () => { |
| 96 | + deferred.resolve(); |
| 97 | + }); |
| 98 | + } |
| 99 | +}).add('request - callback', { |
| 100 | + defer: true, |
| 101 | + fn: (deferred: {resolve(): void}) => { |
| 102 | + request(urlString, requestOptions, (error: Error) => { |
| 103 | + if (error) { |
| 104 | + throw error; |
| 105 | + } |
| 106 | + |
| 107 | + deferred.resolve(); |
| 108 | + }); |
| 109 | + } |
| 110 | +}).add('request - stream', { |
| 111 | + defer: true, |
| 112 | + fn: (deferred: {resolve(): void}) => { |
| 113 | + const stream = request(urlString, requestOptions); |
| 114 | + stream.resume(); |
| 115 | + stream.once('end', () => { |
| 116 | + deferred.resolve(); |
| 117 | + }); |
| 118 | + } |
| 119 | +}).add('node-fetch - promise', { |
| 120 | + defer: true, |
| 121 | + fn: async (deferred: {resolve(): void}) => { |
| 122 | + const response = await fetch(url, fetchOptions); |
| 123 | + await response.text(); |
| 124 | + |
| 125 | + deferred.resolve(); |
| 126 | + } |
| 127 | +}).add('node-fetch - stream', { |
| 128 | + defer: true, |
| 129 | + fn: async (deferred: {resolve(): void}) => { |
| 130 | + const {body} = await fetch(url, fetchOptions); |
| 131 | + |
| 132 | + body.resume(); |
| 133 | + body.once('end', () => { |
| 134 | + deferred.resolve(); |
| 135 | + }); |
| 136 | + } |
| 137 | +}).add('axios - promise', { |
| 138 | + defer: true, |
| 139 | + fn: async (deferred: {resolve(): void}) => { |
| 140 | + await axios.request(axiosOptions); |
| 141 | + deferred.resolve(); |
| 142 | + } |
| 143 | +}).add('axios - stream', { |
| 144 | + defer: true, |
| 145 | + fn: async (deferred: {resolve(): void}) => { |
| 146 | + const {data} = await axios.request(axiosStreamOptions); |
| 147 | + data.resume(); |
| 148 | + data.once('end', () => { |
| 149 | + deferred.resolve(); |
| 150 | + }); |
| 151 | + } |
| 152 | +}).add('https - stream', { |
| 153 | + defer: true, |
| 154 | + fn: (deferred: {resolve(): void}) => { |
| 155 | + https.request(urlString, httpsOptions, response => { |
| 156 | + response.resume(); |
| 157 | + response.once('end', () => { |
| 158 | + deferred.resolve(); |
| 159 | + }); |
| 160 | + }).end(); |
| 161 | + } |
| 162 | +}).on('cycle', (event: Benchmark.Event) => { |
| 163 | + console.log(String(event.target)); |
| 164 | +}).on('complete', function (this: any) { |
| 165 | + console.log(`Fastest is ${this.filter('fastest').map('name') as string}`); |
| 166 | + |
| 167 | + internalBenchmark(); |
| 168 | +}).run(); |
| 169 | + |
| 170 | +const internalBenchmark = (): void => { |
| 171 | + console.log(); |
| 172 | + |
| 173 | + const internalSuite = new Benchmark.Suite(); |
| 174 | + internalSuite.add('got - normalize options', { |
| 175 | + fn: () => { |
| 176 | + normalizeArguments(url, gotOptions); |
| 177 | + } |
| 178 | + }).on('cycle', (event: Benchmark.Event) => { |
| 179 | + console.log(String(event.target)); |
| 180 | + }); |
| 181 | + |
| 182 | + internalSuite.run(); |
| 183 | +}; |
| 184 | + |
| 185 | +// Results (i7-7700k, CPU governor: performance): |
| 186 | +// got - promise x 3,092 ops/sec ±5.25% (73 runs sampled) |
| 187 | +// got - stream x 4,313 ops/sec ±5.61% (72 runs sampled) |
| 188 | +// got - promise core x 6,756 ops/sec ±5.32% (80 runs sampled) |
| 189 | +// got - stream core x 6,863 ops/sec ±4.68% (76 runs sampled) |
| 190 | +// got - stream core - normalized options x 7,960 ops/sec ±3.83% (81 runs sampled) |
| 191 | +// request - callback x 6,912 ops/sec ±6.50% (76 runs sampled) |
| 192 | +// request - stream x 7,821 ops/sec ±4.28% (80 runs sampled) |
| 193 | +// node-fetch - promise x 7,036 ops/sec ±8.17% (78 runs sampled) |
| 194 | +// node-fetch - stream x 7,877 ops/sec ±4.17% (80 runs sampled) |
| 195 | +// axios - promise x 6,613 ops/sec ±3.22% (76 runs sampled) |
| 196 | +// axios - stream x 8,642 ops/sec ±2.84% (79 runs sampled) |
| 197 | +// https - stream x 9,955 ops/sec ±6.36% (76 runs sampled) |
| 198 | +// Fastest is https - stream |
| 199 | + |
| 200 | +// got - normalize options x 166,389 ops/sec ±0.63% (91 runs sampled) |
0 commit comments