-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
290 lines (261 loc) · 6.97 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
const textTable = require('text-table')
const path = require('path')
const fs = require('fs')
const Benchmark = require('benchmark')
const http = require('http')
const axios = require('axios')
const got = require('got')
const nodeFetch = require('node-fetch')
const isomorphicFetch = require('isomorphic-fetch')
const superagent = require('superagent')
const ky = require('ky-universal')
const request = require('request')
const { Agent } = require('http')
const badDataError = new Error('ERROR: incorrect data')
const fixtures = {
16: fs.readFileSync(path.join(__dirname, 'fixtures/16K.txt')).toString(),
32: fs.readFileSync(path.join(__dirname, 'fixtures/32K.txt')).toString(),
64: fs.readFileSync(path.join(__dirname, 'fixtures/64K.txt')).toString(),
256: fs.readFileSync(path.join(__dirname, 'fixtures/256K.txt')).toString(),
1024: fs.readFileSync(path.join(__dirname, 'fixtures/1024K.txt')).toString(),
2048: fs.readFileSync(path.join(__dirname, 'fixtures/2048K.txt')).toString()
}
const addTestCases = (suite, options) => {
suite.add('http.request with default agent', {
defer: true,
fn: defer =>
http.get(options.uri, res => {
let body = ''
res.on('data', data => {
body += data
})
res.on('end', () => {
if (body === fixtures[options.size]) {
return defer.resolve(body)
}
throw badDataError
})
})
})
suite.add('http.request with http 1.1', {
defer: true,
fn: defer =>
http.get(options.uri, { agent: new Agent({ keepalive: true }) }, res => {
let body = ''
res.on('data', data => {
body += data
})
res.on('end', () => {
if (body === fixtures[options.size]) {
return defer.resolve(body)
}
throw badDataError
})
})
})
suite.add('http.request with http 1.0', {
defer: true,
fn: defer =>
http.get(options.uri, { agent: false }, res => {
let body = ''
res.on('data', data => {
body += data
})
res.on('end', () => {
if (body === fixtures[options.size]) {
return defer.resolve(body)
}
throw badDataError
})
})
})
suite.add('http.request with http 1.0 and nodelay', {
defer: true,
fn: defer =>
http.get(options.uri, { agent: false }, res => {
let body = ''
res.on('data', data => {
body += data
})
res.on('end', () => {
if (body === fixtures[options.size]) {
return defer.resolve(body)
}
throw badDataError
})
}).setNoDelay(true)
})
suite.add('axios', {
defer: true,
fn: defer => {
return axios
.get(options.uri)
.then(response => {
if (response.data === fixtures[options.size]) {
return defer.resolve()
}
throw badDataError
})
.catch(err => {
throw err
})
}
})
suite.add('got', {
defer: true,
fn: defer => {
return got(options.uri)
.then(response => {
if (response.body === fixtures[options.size]) {
return defer.resolve()
}
throw badDataError
})
.catch(err => {
throw err
})
}
})
suite.add('superagent', {
defer: true,
fn: defer => {
return superagent
.get(options.uri)
.then(response => {
if (response.text === fixtures[options.size]) {
return defer.resolve()
}
throw badDataError
})
.catch(err => {
throw err
})
}
})
suite.add('isomorphicFetch', {
defer: true,
fn: defer => {
return isomorphicFetch(options.uri)
.then(response => response.text())
.then(text => {
if (text === fixtures[options.size]) {
return defer.resolve()
}
throw badDataError
})
.catch(err => {
throw err
})
}
})
suite.add('nodeFetch', {
defer: true,
fn: defer => {
return nodeFetch(options.uri)
.then(response => response.text())
.then(text => {
if (text === fixtures[options.size]) {
return defer.resolve()
}
throw badDataError
})
.catch(err => {
throw err
})
}
})
suite.add('ky-universal', {
defer: true,
fn: async defer => {
try {
const response = await ky(options.uri)
const text = await response.text()
if (text === fixtures[options.size]) {
return defer.resolve()
}
throw badDataError
} catch (err) {
throw err
}
}
})
suite.add('request', {
defer: true,
fn: defer => {
return request(options.uri,function(error, response, body) {
if (error) {
throw error
}
if (body === fixtures[options.size]) {
return defer.resolve()
}
throw badDataError
})
}
})
}
let benchmarkIndex = 0
let downloadSizeIndex = 0
const onCycle = (totalCases, options, results) => event => {
const t = event.currentTarget[benchmarkIndex]
const name = t.name
const opsPerSecond = parseFloat(t.hz).toFixed(0)
const rme = parseFloat(t.stats.rme).toFixed(2)
const runsSampled = t.count
const output = [name, opsPerSecond, `±${rme}%`, runsSampled]
console.log(
`${name} x ${opsPerSecond} ops/sec ±${rme}% (${runsSampled} runs sampled)`
)
const uri = options[downloadSizeIndex].uri
results[uri].push(output)
benchmarkIndex = benchmarkIndex + 1
if (benchmarkIndex % (totalCases / options.length) === 0) {
downloadSizeIndex += 1
}
}
const createFileSizeSection = (size, results) => {
return `
### GET ${size}
\`\`\`
${textTable(results, { align: ['l', 'r', 'r', 'r'] })}
\`\`\`
`
}
const onComplete = (options, results) => () => {
let template = fs
.readFileSync(path.join(__dirname, './templates/readme.md'))
.toString()
const resultsString = options
.map(s => createFileSizeSection(`${s.size}K.txt`, results[s.uri]))
.join('')
template = template.replace('${results}', resultsString)
fs.writeFileSync(path.join(__dirname, 'readme.md'), template)
}
const runBenchmarks = ({ downloadSizes }) => {
const tableHead = [
['Module', 'OPS', 'RME', 'Samples'],
['---------------', '----------', '----------', '----------']
]
const results = {}
downloadSizes.map(s => {
results[s.uri] = [...tableHead]
})
const suite = Benchmark.Suite()
downloadSizes.map(o => addTestCases(suite, o))
suite.on('cycle', onCycle(suite.length, downloadSizes, results))
suite.on('complete', onComplete(downloadSizes, results))
suite.run({
async: true
})
}
if (!module.parent) {
const options = {
downloadSizes: [16, 32, 64, 256, 1024].map(s => {
return {
uri: `http://${process.env.NGINX}/${s}K.txt`,
size: s
}
})
}
runBenchmarks(options)
}