forked from nestjs/nest
/
get-benchmarks.ts
77 lines (65 loc) 路 1.72 KB
/
get-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
import wrkPkg = require('wrk');
import { spawn } from 'child_process';
import { join } from 'path';
export interface Benchmarks {
[lib: string]: WrkResults;
}
const wrk = (options: any) =>
new Promise<WrkResults>((resolve, reject) =>
wrkPkg(options, (err: any, result: any) =>
err ? reject(err) : resolve(result),
),
);
const sleep = (time: number) =>
new Promise(resolve => setTimeout(resolve, time));
const BENCHMARK_PATH = join(__dirname, '../../benchmarks');
export const LIBS = ['express', 'fastify', 'nest', 'nest-fastify'];
async function runBenchmarkOfLib(lib: string): Promise<WrkResults> {
const libPath = join(BENCHMARK_PATH, `${lib}.js`);
const process = spawn('node', [libPath], {
detached: true,
});
process.stdout!.on('data', data => {
console.log(`stdout: ${data}`);
});
process.stderr!.on('data', data => {
console.log(`stderr: ${data}`);
});
process.unref();
await sleep(2000);
const result = await wrk({
threads: 8,
duration: '10s',
connections: 1024,
url: 'http://localhost:3000',
});
process.kill();
return result;
}
export async function getBenchmarks() {
const results: Benchmarks = {};
for await (const lib of LIBS) {
const result = await runBenchmarkOfLib(lib);
results[lib] = result;
}
return results;
}
interface WrkResults {
transferPerSec: string;
requestsPerSec: number;
connectErrors: string;
readErrors: string;
writeErrors: string;
timeoutErrors: string;
requestsTotal: number;
durationActual: string;
transferTotal: string;
latencyAvg: string;
latencyStdev: string;
latencyMax: string;
latencyStdevPerc: number;
rpsAvg: string;
rpsStdev: string;
rpsMax: string;
rpsStdevPerc: number;
}