93 lines
4.4 KiB
JavaScript
93 lines
4.4 KiB
JavaScript
const { createClient } = require(process.argv[2]);
|
|
const { addMovement } = require("./client-movement.cjs");
|
|
const [port, version, playerCounts, seconds, interval, supplyBooks, richBooks, smtpFlood, healthy] = process.argv.slice(3);
|
|
const targets = playerCounts.split(",").map(Number);
|
|
const clients = [];
|
|
let observedDeliveries = 0;
|
|
|
|
const wait = milliseconds => new Promise(resolve => setTimeout(resolve, milliseconds));
|
|
function join(index) {
|
|
return new Promise(resolve => {
|
|
const username = "ICGLoad" + String(index).padStart(3, "0");
|
|
const client = createClient({ host: "127.0.0.1", port: Number(port), version, username, auth: "offline", hideErrors: true });
|
|
addMovement(client, version);
|
|
const entry = { client, online: false, username, reason: null };
|
|
clients.push(entry);
|
|
const timeout = setTimeout(() => { entry.reason = "login timeout"; client.end("login timeout"); resolve(); }, 25_000);
|
|
function settled() { clearTimeout(timeout); resolve(); }
|
|
client.once("login", () => { entry.online = true; settled(); });
|
|
client.once("error", error => { entry.reason = error.message; entry.online = false; settled(); });
|
|
client.on("end", reason => { entry.reason = reason; entry.online = false; settled(); });
|
|
for (const event of ["systemChat", "playerChat"]) {
|
|
client.on(event, data => {
|
|
if (index === 0 && JSON.stringify(data).includes("ICG-load")) observedDeliveries++;
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
async function main() {
|
|
let previous = 0;
|
|
for (const target of targets) {
|
|
for (let start = previous; start < target; start += 8) {
|
|
await Promise.all(Array.from({ length: Math.min(8, target - start) }, (_, i) => join(start + i)));
|
|
await wait(100);
|
|
}
|
|
previous = target;
|
|
await wait(4200);
|
|
if (supplyBooks === "true") {
|
|
for (const entry of clients.filter(entry => entry.online)) {
|
|
entry.client.chat("/icgprobe refill");
|
|
}
|
|
await wait(1300);
|
|
}
|
|
let messagesSent = 0;
|
|
let booksSent = 0;
|
|
const online = clients.filter(entry => entry.online).length;
|
|
const before = observedDeliveries;
|
|
const started = Date.now();
|
|
const stop = started + Number(seconds) * 1000;
|
|
let cycle = 0;
|
|
while (Date.now() < stop) {
|
|
const active = clients.filter(entry => entry.online);
|
|
const cycleStarted = Date.now();
|
|
for (let i = 0; i < active.length; i++) {
|
|
const text = "ICG-load " + target + " " + cycle + " " + i + " "
|
|
+ (smtpFlood === "true" ? "死全家" : healthy === "true" || cycle % 3 === 0 ? "违 规-内容 English test" :
|
|
cycle % 3 === 1 ? "ICG-timeout 违违规内容" : "违规内容 ".repeat(20) + "x".repeat(100));
|
|
active[i].client.chat(text);
|
|
messagesSent++;
|
|
if (cycle === 0) {
|
|
const pages = richBooks === "true"
|
|
? Array.from({ length: 50 }, (_, page) => "ICG-load " + i + "/" + page + " 违规内容 " + "text ".repeat(90))
|
|
: ["ICG-load 违 规内容", "Book stress test"];
|
|
active[i].client.write("edit_book", {
|
|
hand: 0, pages, ...(target >= 50 ? { title: "ICG stress" } : {}),
|
|
});
|
|
booksSent++;
|
|
}
|
|
if (healthy === "true") {
|
|
const next = cycleStarted + (i + 1) * Number(interval) / active.length;
|
|
await wait(Math.max(0, next - Date.now()));
|
|
}
|
|
}
|
|
cycle++;
|
|
await wait(healthy === "true" && active.length ? 0 : Number(interval));
|
|
}
|
|
await wait(2500);
|
|
console.log(JSON.stringify({
|
|
target, online, onlineAfter: clients.filter(entry => entry.online).length,
|
|
durationMs: Date.now() - started, messagesSent, booksSent,
|
|
firstClientObservedDeliveries: observedDeliveries - before,
|
|
disconnects: clients.filter(entry => !entry.online).map(entry => ({ player: entry.username, reason: entry.reason })),
|
|
}));
|
|
}
|
|
}
|
|
|
|
main().catch(error => {
|
|
console.error(error.stack);
|
|
process.exitCode = 1;
|
|
}).finally(() => {
|
|
for (const entry of clients) entry.client.end("Stress run complete");
|
|
});
|