-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbench.js
71 lines (54 loc) · 1.4 KB
/
bench.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
'use strict'
const net = require('net')
const { promisify } = require('util')
const { spawn } = require('child_process')
const autocannon = require('autocannon')
const args = process.argv.slice(2)
process.env.NODE_ENV = 'production'
const up = promisify(function up (port, cb) {
const socket = net.connect(port)
.on('error', () => setTimeout(up, 300, port, cb))
.on('connect', () => {
socket.end()
cb()
})
})
const when = () => {
var _cb = null
const fn = () => _cb()
fn.done = promisify((cb) => _cb = cb)
return fn
}
run(args) // <-- main
async function run (args) {
if (args.length === 0) args = ['unoptimized', 'optimized']
for (const cmd of args) {
await bench(cmd)
}
}
async function bench (cmd) {
process.stdout.write(`
======================= Benchmarking ${cmd} server =============================
`)
const sp = spawn('npm', ['run', cmd], {
stdio: ['ignore', 'ignore', 'inherit']
})
await up(3000)
const benching = when()
const instance = autocannon({
url: 'http://localhost:3000',
duration: 5
}, benching)
// this is used to kill the instance on CTRL-C
process.once('SIGINT', () => {
instance.stop()
})
// just render results
autocannon.track(instance, {renderProgressBar: true})
await benching.done()
sp.kill()
const closing = when()
sp.once('close', closing)
await closing.done()
console.log('')
}