-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark-runner.ts
190 lines (157 loc) · 4.77 KB
/
benchmark-runner.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
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
import { spawn, ChildProcess } from 'child_process';
import path from 'path';
interface ServerConfig {
name: string;
script: string;
port: number;
}
interface TestConfig {
name: string;
endpoint: string;
method: string;
connections: number;
pipelining: number;
duration: number;
body?: string;
headers?: { [key: string]: string };
}
interface BombardierResult {
rps: {
mean: number;
};
latency: {
min: number;
};
duration: number;
}
function startServer(serverConfig: ServerConfig): Promise<ChildProcess> {
return new Promise((resolve, reject) => {
const server = spawn('bun', ['run', 'index.ts'], {
env: { ...process.env, PORT: serverConfig.port.toString() },
stdio: ['ignore', 'pipe', 'pipe'],
cwd: "./servers/" + serverConfig.name
});
let started = false;
let output = '';
server.stdout.on('data', (data) => {
output += data.toString();
if (!started && (output.includes('listening') || output.includes('started') || output.includes('running'))) {
started = true;
resolve(server);
}
});
server.stderr.on('data', (data) => {
console.error(`[${serverConfig.name}]`, data.toString());
});
server.on('error', (err) => {
reject(err);
});
setTimeout(() => {
if (!started) {
resolve(server);
}
}, 5000);
});
}
function stopServer(server: ChildProcess): Promise<void> {
return new Promise((resolve) => {
server.on('close', () => {
resolve();
});
server.kill('SIGTERM');
setTimeout(() => {
if (!server.killed) {
server.kill('SIGKILL');
}
}, 3000);
});
}
function runBenchmark(url: string, options: TestConfig): Promise<BombardierResult> {
return new Promise((resolve, reject) => {
const args = [
'-m', options.method,
'-c', options.connections.toString(),
'-d', `${options.duration}s`,
'--print', 'r',
'--format', 'json'
];
if (options.body) {
args.push('-b', options.body);
}
if (options.headers) {
for (const [key, value] of Object.entries(options.headers)) {
args.push('-H', `${key}: ${value}`);
}
}
args.push(url);
const bombardier = spawn('bombardier', args);
let output = '';
bombardier.stdout.on('data', (data) => {
output += data.toString();
});
bombardier.stderr.on('data', (data) => {
console.error(`[bombardier]`, data.toString());
});
bombardier.on('close', (code) => {
if (code !== 0) {
reject(new Error(`bombardier exited with code ${code}`));
return;
}
try {
const result = JSON.parse(output);
const latencyInMs = result.result.latency.mean / 1000;
resolve({
rps: {
mean: result.result.rps.mean
},
latency: {
min: latencyInMs
},
duration: result.result.timeTakenSeconds
});
} catch (err) {
console.error('Raw output:', output);
reject(new Error(`Failed to parse bombardier output: ${err}`));
}
});
bombardier.on('error', (err) => {
reject(err);
});
});
}
export async function runBenchmarks(servers: ServerConfig[], tests: TestConfig[]): Promise<void> {
const serverProcesses: { [key: string]: ChildProcess } = {};
try {
for (const server of servers) {
serverProcesses[server.name] = await startServer(server);
}
await new Promise(resolve => setTimeout(resolve, 2000));
for (const test of tests) {
console.log(`\n=== TEST: ${test.name} ===`);
const testResults: any[] = [];
for (const server of servers) {
const url = `http://localhost:${server.port}${test.endpoint}`;
const results = await runBenchmark(url, test);
const requestsPerSecond = Math.floor(results.rps.mean);
const minLatency = results.latency.min.toFixed(2);
const totalTime = results.duration.toFixed(2);
testResults.push({
name: server.name,
requestsPerSecond,
minLatency,
totalTime
});
console.log(`${server.name}: ${requestsPerSecond} req/sec, ${minLatency} ms min latency, ${totalTime} s total time`);
}
testResults.sort((a, b) => b.requestsPerSecond - a.requestsPerSecond);
console.log('\nRésultats:');
testResults.forEach((result, index) => {
console.log(`#${index + 1} ${result.name}: ${result.requestsPerSecond} req/sec, ${result.minLatency} ms min latency, ${result.totalTime} s total time`);
});
}
} finally {
for (const server of Object.values(serverProcesses)) {
await stopServer(server);
}
}
}