-
Notifications
You must be signed in to change notification settings - Fork 733
Expand file tree
/
Copy pathbenchmark.js
More file actions
246 lines (213 loc) · 8.68 KB
/
Copy pathbenchmark.js
File metadata and controls
246 lines (213 loc) · 8.68 KB
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
import { spawn } from "node:child_process";
import { readFileSync, writeFileSync } from "node:fs";
import { resolve, dirname } from "node:path";
import { fileURLToPath } from "node:url";
import autocannon from "autocannon";
const __dirname = dirname(fileURLToPath(import.meta.url));
const ROOT = resolve(__dirname, "..");
const API_DIR = resolve(ROOT, "apps/api");
const RESULTS_DIR = resolve(__dirname, "results");
const PORT = 4001;
const BASE = `http://localhost:${PORT}`;
const THRESHOLDS = JSON.parse(readFileSync(resolve(__dirname, "thresholds.json"), "utf-8"));
const TEST_TOKEN =
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c3JfYmVuY2htYXJrIiwicm9sZSI6ImFkbWluIiwiaWF0IjoxNzAwMDAwMDAwLCJleHAiOjk5OTk5OTk5OTl9.QLKHkIBjSBTqFFa5Dd-qzOsr9K8bWGH2nSC6tENvz74";
const ENDPOINTS = [
{ method: "GET", path: "/health" },
{ method: "POST", path: "/api/auth/register", body: { email: "test@benchmark.dev", password: "benchpass1" } },
{ method: "POST", path: "/api/auth/login", body: { email: "test@benchmark.dev", password: "benchpass1" } },
{ method: "POST", path: "/api/auth/refresh" },
{ method: "GET", path: "/api/auth/oauth/google/callback" },
{ method: "GET", path: "/api/users" },
{ method: "POST", path: "/api/users", body: { email: "new@benchmark.dev", name: "Bench User" } },
{ method: "GET", path: "/api/jobs" },
{ method: "POST", path: "/api/jobs", body: { title: "Benchmark Job Position", description: "This is a benchmark job description for load testing purposes.", budgetMin: 500, budgetMax: 5000, categoryId: "cat_web", skills: ["node", "react"] } },
{ method: "GET", path: "/api/proposals" },
{ method: "POST", path: "/api/proposals", body: { jobId: "job_1", coverLetter: "I am an experienced benchmark engineer and can deliver.", rate: 75 } },
{ method: "POST", path: "/api/payments", body: { amount: 100, currency: "USD", paymentMethodId: "pm_bench" } },
{ method: "GET", path: "/api/reviews" },
{ method: "POST", path: "/api/reviews", body: { userId: "usr_1", rating: 5, comment: "Excellent benchmark performance." } },
{ method: "GET", path: "/api/messages" },
{ method: "POST", path: "/api/messages", body: { recipientId: "usr_2", content: "Hello from benchmark suite." } },
{ method: "GET", path: "/api/notifications" },
{ method: "POST", path: "/api/notifications", body: { userId: "usr_1", type: "info", message: "Benchmark notification test." } },
{ method: "POST", path: "/api/uploads" },
{ method: "GET", path: "/api/search", query: { q: "benchmark" } },
{ method: "GET", path: "/api/admin/metrics", auth: true },
];
function fmtMs(ms) {
return `${ms.toFixed(2)} ms`;
}
function fmtNum(n) {
return n.toLocaleString();
}
async function waitForServer(url, retries = 30) {
for (let i = 0; i < retries; i++) {
try {
const res = await fetch(url);
if (res.ok) return;
} catch {}
await new Promise((r) => setTimeout(r, 500));
}
throw new Error(`Server did not start within ${retries * 500}ms`);
}
function runBenchmark(opts) {
return new Promise((resolve, reject) => {
const instance = autocannon(
{
url: BASE,
...opts,
connections: 10,
duration: 10,
pipelining: 1,
timeout: 30,
// Note: autocannon provides p50, p75, p99 by default.
// p95 is estimated as avg(p75, p99) in the output when p95 is 0.
headers: {
"Content-Type": "application/json",
...(opts.headers || {}),
},
},
(err, result) => {
if (err) return reject(err);
resolve(result);
},
);
});
}
async function main() {
console.log("\n[benchmark] Starting API server...\n");
const server = spawn("node", ["src/server.js"], {
cwd: API_DIR,
env: { ...process.env, BENCHMARK: "1", PORT: String(PORT) },
stdio: "pipe",
});
server.stderr.on("data", (d) => process.stderr.write(d));
await waitForServer(`${BASE}/health`);
console.log(`[benchmark] Server ready on ${BASE}\n`);
const results = [];
for (const ep of ENDPOINTS) {
const label = `${ep.method} ${ep.path}`;
process.stdout.write(` ${label} ... `);
const urlPath = ep.query
? `${ep.path}?${new URLSearchParams(ep.query)}`
: ep.path;
const opts = {
method: ep.method,
path: urlPath,
headers: {},
setupClient: null,
};
if (ep.auth) {
opts.headers["Authorization"] = `Bearer ${TEST_TOKEN}`;
}
if (ep.method === "POST") {
opts.headers["Content-Type"] = "application/json";
if (ep.body) {
opts.body = JSON.stringify(ep.body);
}
}
try {
const result = await runBenchmark(opts);
const codes = result.statusCodes || {};
const non2xxCt = Object.entries(codes)
.filter(([code]) => !code.startsWith("2"))
.reduce((sum, [, ct]) => sum + ct, 0);
const errors = (result.errors || 0) + (result.timeouts || 0);
const r = {
endpoint: label,
method: ep.method,
path: ep.path,
latency: {
p50: result.latency?.p50 ?? 0,
p75: result.latency?.p75 ?? 0,
p95: (result.latency?.p95 ?? 0) || Math.round(((result.latency?.p75 ?? 0) + (result.latency?.p99 ?? 0)) / 2),
p99: result.latency?.p99 ?? 0,
average: result.latency?.average ?? 0,
min: result.latency?.min ?? 0,
max: result.latency?.max ?? 0,
},
requests: {
total: result.requests?.total ?? 0,
average: Math.round(result.requests?.average ?? 0),
throughput: Math.round(result.throughput?.average ?? 0),
},
errors: {
total: errors,
non2xx: non2xxCt,
codes,
},
ttfb: result.latency?.first ? result.latency.first : "—",
};
results.push(r);
process.stdout.write(
`p50=${fmtMs(r.latency.p50)} p95=${fmtMs(r.latency.p95)} p99=${fmtMs(r.latency.p99)} rps=${fmtNum(r.requests.average)} err=${r.errors.total}\n`,
);
} catch (err) {
results.push({
endpoint: label,
method: ep.method,
path: ep.path,
error: err.message,
});
process.stdout.write(`FAILED: ${err.message}\n`);
}
}
const jsonPath = resolve(RESULTS_DIR, "benchmark-results.json");
writeFileSync(jsonPath, JSON.stringify({ timestamp: new Date().toISOString(), results }, null, 2));
console.log(`\n[benchmark] JSON report: ${jsonPath}`);
const mdPath = resolve(RESULTS_DIR, "benchmark-summary.md");
const mdLines = [];
mdLines.push("# Benchmark Summary");
mdLines.push("");
mdLines.push(`**Date:** ${new Date().toISOString()}`);
mdLines.push(`**Tool:** autocannon (10 connections, 10s duration)`);
mdLines.push(`**Target:** http://localhost:${PORT}`);
mdLines.push("");
mdLines.push("## Per-Endpoint Results");
mdLines.push("");
mdLines.push("| Endpoint | Method | p50 | p95 | p99 | Avg RPS | Errors |");
mdLines.push("|----------|--------|-----|-----|-----|---------|--------|");
let allPass = true;
for (const r of results) {
if (r.error) {
mdLines.push(`| ${r.endpoint} | ${r.method} | — | — | — | — | **${r.error}** |`);
allPass = false;
continue;
}
const errorRate = (r.errors.total || 0) / Math.max(r.requests.total, 1) * 100;
const pass =
r.latency.p99 <= THRESHOLDS.p99 &&
errorRate <= THRESHOLDS.errorRate &&
r.requests.average >= THRESHOLDS.minRps &&
r.errors.non2xx === 0;
if (!pass) allPass = false;
const status = pass ? "✅" : "❌";
mdLines.push(
`| ${r.endpoint} ${status} | ${r.method} | ${fmtMs(r.latency.p50)} | ${fmtMs(r.latency.p95)} | ${fmtMs(r.latency.p99)} | ${fmtNum(r.requests.average)} | ${r.errors.total} |`,
);
}
mdLines.push("");
mdLines.push("## Thresholds");
mdLines.push("");
mdLines.push(`| Metric | Threshold | Status |`);
mdLines.push(`|--------|-----------|--------|`);
mdLines.push(`| p99 latency | ≤ ${THRESHOLDS.p99} ms | ${allPass ? "✅ Pass" : "❌ Fail"} |`);
mdLines.push(`| Error rate | ≤ ${THRESHOLDS.errorRate}% | ${allPass ? "✅ Pass" : "❌ Fail"} |`);
mdLines.push(`| Min RPS | ≥ ${THRESHOLDS.minRps} | ${allPass ? "✅ Pass" : "❌ Fail"} |`);
mdLines.push("");
mdLines.push("## Environment");
mdLines.push("");
mdLines.push("- **Node.js:** " + process.version);
mdLines.push("- **Platform:** " + process.platform);
mdLines.push("- **Architecture:** " + process.arch);
writeFileSync(mdPath, mdLines.join("\n"));
console.log(`[benchmark] Markdown report: ${mdPath}`);
console.log(`\n[benchmark] Threshold check: ${allPass ? "✅ ALL PASS" : "❌ SOME FAILED"}\n`);
server.kill();
process.exit(allPass ? 0 : 1);
}
main().catch((err) => {
console.error("[benchmark] Fatal:", err);
process.exit(1);
});