-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.ts
More file actions
55 lines (45 loc) · 1.55 KB
/
test.ts
File metadata and controls
55 lines (45 loc) · 1.55 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
const TEST_COUNT = 10;
const TEST_PAYLOAD = {
events: Array.from({ length: 10 }, (_, i) => ({ hello: `world${i + 1}` }))
};
async function measureRequest(): Promise<number> {
const startTime = performance.now();
const response = await fetch("http://localhost:8000", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(TEST_PAYLOAD),
});
await response.json(); // Wait for body to be read
const endTime = performance.now();
return endTime - startTime;
}
async function runTests() {
console.log("Starting performance tests...\n");
const times: number[] = [];
for (let i = 0; i < TEST_COUNT; i++) {
try {
const time = await measureRequest();
times.push(time);
console.log(`Request ${i + 1}: ${time.toFixed(2)}ms`);
} catch (error) {
console.error(`Error in request ${i + 1}:`, error);
}
// Small delay between requests to avoid overwhelming the server
await new Promise(resolve => setTimeout(resolve, 100));
}
// Calculate statistics
const average = times.reduce((a, b) => a + b, 0) / times.length;
const min = Math.min(...times);
const max = Math.max(...times);
const sorted = [...times].sort((a, b) => a - b);
const median = sorted[Math.floor(sorted.length / 2)];
console.log("\nResults:");
console.log(`Average time: ${average.toFixed(2)}ms`);
console.log(`Median time: ${median.toFixed(2)}ms`);
console.log(`Min time: ${min.toFixed(2)}ms`);
console.log(`Max time: ${max.toFixed(2)}ms`);
}
// Run the tests
runTests();