forked from ClusterWS/cWS
-
Notifications
You must be signed in to change notification settings - Fork 2
/
echoBenchmark.js
77 lines (67 loc) · 2.49 KB
/
echoBenchmark.js
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
const { WebSocket: { Server } } = require('../dist');
const WebSocket = require('ws');
const numClients = 1000; // Number of concurrent clients
const messageSize = 10240; // Size of the message (in bytes)
const numMessagesPerClient = 10; // Number of messages each client will send
const numRuns = 10; // Total number of benchmark runs
let currentRun = 0;
const runTimes = []; // Store total times for each run
function runBenchmark() {
let disconnectedClients = 0; // Counter for disconnected clients
let start; // Start time of the run
const wss = new Server({ port: 3000 }, () => {
console.log(`Server is running on port 3000, Run: ${currentRun + 1}`);
start = process.hrtime.bigint(); // Start the timer
for (let i = 0; i < numClients; i++) {
const ws = new WebSocket('ws://localhost:3000');
let messagesSent = 0;
ws.on('open', function open() {
for (let j = 0; j < numMessagesPerClient; j++) {
ws.send('x'.repeat(messageSize));
}
});
ws.on('message', function incoming(data) {
messagesSent++;
if (messagesSent === numMessagesPerClient) {
ws.close();
}
});
ws.on('close', () => {
disconnectedClients++;
if (disconnectedClients === numClients) {
const end = process.hrtime.bigint();
const totalTime = Number(end - start) / 1e6; // Convert BigInt to Number
console.log(`Total time for run ${currentRun + 1}: ${totalTime.toFixed(2)} ms`);
runTimes.push(totalTime);
wss.close(() => {
currentRun++;
if (currentRun < numRuns) {
runBenchmark();
} else {
printStatistics();
}
});
}
});
}
});
wss.on('connection', (ws, req) => {
ws.on('message', (msg) => {
ws.send(msg); // Echo the message
});
});
}
function printStatistics() {
const total = runTimes.reduce((a, b) => a + b, 0);
const average = total / runTimes.length;
const min = Math.min(...runTimes);
const max = Math.max(...runTimes);
const stdDev = Math.sqrt(runTimes.map(x => Math.pow(x - average, 2)).reduce((a, b) => a + b) / runTimes.length);
console.log('\n--- Benchmark Statistics ---');
console.log(`Runs: ${numRuns}`);
console.log(`Average Time: ${average.toFixed(2)} ms`);
console.log(`Min Time: ${min.toFixed(2)} ms`);
console.log(`Max Time: ${max.toFixed(2)} ms`);
console.log(`Standard Deviation: ${stdDev.toFixed(2)}`);
}
runBenchmark();