1
0
mirror of https://github.com/actix/examples synced 2024-11-30 17:14:35 +01:00
examples/server-sent-events/benchmark.js
2020-09-12 16:49:45 +01:00

61 lines
1.5 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const http = require('http')
const n = 100;
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'
}, 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();
http.get({
host: 'localhost',
port: 8080,
path: '/broadcast/' + message
}, response => {
response.on('data', _ => {})
})
}
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)