1
0
mirror of https://github.com/actix/examples synced 2024-12-18 00:13:57 +01:00
examples/server-sent-events/benchmark.js

74 lines
1.5 KiB
JavaScript
Raw Normal View History

2024-08-07 03:04:57 +02:00
const http = require("http")
2022-08-07 02:24:40 +02:00
const n = 120
let connected = 0
let messages = 0
let start = Date.now()
2024-08-07 03:04:57 +02:00
let phase = "connecting"
2022-08-07 02:24:40 +02:00
let connection_time
let broadcast_time
2024-08-07 03:04:57 +02:00
let message = process.argv[2] || "msg"
let expected_data = "data: " + message
for (let i = 0; i < n; i++) {
2022-08-07 02:24:40 +02:00
http
.get(
{
2024-08-07 03:04:57 +02:00
host: "127.0.0.1",
port: 8080,
2024-08-07 03:04:57 +02:00
path: "/events",
2022-08-07 02:24:40 +02:00
},
2024-08-07 03:04:57 +02:00
response => {
response.on("data", data => {
2022-08-07 02:24:40 +02:00
if (data.includes(expected_data)) {
messages += 1
2024-08-07 03:04:57 +02:00
} else if (data.includes("data: connected\n")) {
2022-08-07 02:24:40 +02:00
connected += 1
}
})
2024-08-07 03:04:57 +02:00
},
2022-08-07 02:24:40 +02:00
)
2024-08-07 03:04:57 +02:00
.on("error", _ => {})
}
setInterval(() => {
2024-08-07 03:04:57 +02:00
if (phase === "connecting" && connected === n) {
2022-08-07 02:24:40 +02:00
// done connecting
2024-08-07 03:04:57 +02:00
phase = "messaging"
2022-08-07 02:24:40 +02:00
connection_time = Date.now() - start
}
2024-08-07 03:04:57 +02:00
if (phase === "messaging") {
phase = "waiting"
2022-08-07 02:24:40 +02:00
start = Date.now()
http
.request(
{
2024-08-07 03:04:57 +02:00
method: "POST",
host: "127.0.0.1",
2022-08-07 02:24:40 +02:00
port: 8080,
2024-08-07 03:04:57 +02:00
path: "/broadcast/" + message,
},
response => {
response.on("data", _ => {})
2022-08-07 02:24:40 +02:00
},
)
.end()
}
2024-08-07 03:04:57 +02:00
if (phase === "waiting" && messages >= n) {
2022-08-07 02:24:40 +02:00
// all messages received
broadcast_time = Date.now() - start
2024-08-07 03:04:57 +02:00
phase = "paused"
2022-08-07 02:24:40 +02:00
messages = 0
2024-08-07 03:04:57 +02:00
phase = "messaging"
2022-08-07 02:24:40 +02:00
}
2024-08-07 03:04:57 +02:00
process.stdout.write("\r\x1b[K")
2022-08-07 02:24:40 +02:00
process.stdout.write(
2024-08-07 03:04:57 +02:00
`Connected: ${connected}, connection time: ${connection_time} ms, total broadcast time: ${broadcast_time} ms`,
2022-08-07 02:24:40 +02:00
)
}, 20)