-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.js
76 lines (65 loc) · 1.95 KB
/
benchmark.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
const http = require('http')
const n = 1000;
let connected = 0;
let messages = 0;
let start = Date.now();
let phase = 'connecting';
let connection_time;
let broadcast_time;
let message = process.argv[2] || 'msg';
let expected_data = "data: " + message;
for (let i = 0; i < n; i++) {
http.get({
host: 'localhost',
port: 8080,
path: '/events/channel1'
}, response => {
response.on('data', data => {
if (data.includes(expected_data)) {
messages += 1;
} else if (data.includes("data: connected\n")) {
connected += 1;
}
})
}).on('error', (_) => {});
}
setInterval(() => {
if (phase === 'connecting' && connected === n) {
// done connecting
phase = 'messaging';
connection_time = Date.now() - start;
}
if (phase === 'messaging') {
phase = 'waiting';
start = Date.now();
postData = JSON.stringify({
channel: "channel1",
message: message
})
options = {
host: 'localhost',
port: 8080,
path: '/broadcast/',
method: "POST",
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(postData),
},
};
var req = http.request(options, res => {
res.on('data', _ => {})
})
req.on("error", e => console.error(`problem with request ${e.msg}`));
req.write(postData);
req.end();
}
if (phase === 'waiting' && messages >= n) {
// all messages received
broadcast_time = Date.now() - start;
phase = 'paused';
messages = 0;
phase = 'messaging';
}
process.stdout.write("\r\x1b[K");
process.stdout.write(`Connected: ${connected}, connection time: ${connection_time} ms, total broadcast time: ${broadcast_time} ms`);
}, 20)