/
benchmarks.ts
79 lines (61 loc) · 1.77 KB
/
benchmarks.ts
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
import childProcess from 'child_process'
import path from 'path'
import fs from 'fs'
// @ts-ignore
import autocannon from 'autocannon'
// @ts-ignore
import table from 'markdown-table'
// @ts-ignore
import size from 'filesize'
const basePath = path.resolve(__dirname, '../../benchmarks')
const sleep = (time: number) =>
new Promise(resolve => setTimeout(resolve, time));
const benchmarksMDPath = path.join(basePath, 'README.md')
const benchmarksMDStream = fs.createWriteStream(benchmarksMDPath)
fs.writeFileSync(benchmarksMDPath, '')
benchmarksMDStream.write('# Benchmarks\n')
const opt = {
connections: 100,
pipelining: 10,
duration: 10
}
const libWithoutRoutes = [
'daze-without-routes',
'express-without-routes',
'koa-without-routes',
];
const libWith1000Routes = [
'daze-with-1000-routes',
'express-with-1000-routes',
'koa-with-1000-routes'
]
async function run(lib: string, url: string) {
const _targetPath = path.join(basePath, lib)
const child = childProcess.spawn('node', [_targetPath], {
stdio: 'inherit',
});
await sleep(2000)
benchmarksMDStream.write('\n')
benchmarksMDStream.write(`### ${lib}\n`)
benchmarksMDStream.write('\n')
const { requests, throughput } = await autocannon({
...opt,
url,
})
child.kill()
benchmarksMDStream.write(table([
['Stat', 'Avg', 'Stdev', 'Min'],
['Req/Sec', requests.average, requests.stddev, requests.min],
['Bytes/Sec', size(throughput.average), size(requests.stddev), size(requests.min)]
]))
benchmarksMDStream.write('\n')
benchmarksMDStream.write('\n')
}
(async () => {
for (const lib of libWithoutRoutes) {
await run(lib, 'http://localhost:3000/hello')
}
for (const lib of libWith1000Routes) {
await run(lib, 'http://localhost:3000/uuid5/uuid5/uuid5')
}
})()