Load Testing / Stress Testing
The @colyseus/loadtest tool is useful when you’d like to stress test your server and see how it is going to perform on a live environment.
Install the package
This package is installed by default on new projects created via npm create colyseus-app.
npm install --save-dev @colyseus/loadtestUsage
The colyseus-loadtest command requires a few arguments to work:
script: The custom script the tool is going to use--endpoint: Your server endpoint (by default usesws://localhost:2567)--room: Name of the room you’d like to connect to--numClients: Number of clients you’d like to connect into the room.
Example
This is an example scripting file. Based on the room lifecycle events for each connected client, you may implement a “bot” to interact with the room.
loadtest/example.ts
import { Client, Room } from "colyseus.js";
import { cli, Options } from "@colyseus/loadtest";
async function main(options: Options) {
const client = new Client(options.endpoint);
const room: Room = await client.joinOrCreate(options.roomName, {
// your join options here...
});
console.log("joined successfully!");
room.onMessage("*", (type, message) => {
console.log("onMessage:", type, message);
});
room.onStateChange((state) => {
console.log(room.sessionId, "new state:", state);
});
room.onError((err) => {
console.log(room.sessionId, "!! ERROR !!", err.message);
})
room.onLeave((code) => {
console.log(room.sessionId, "left.");
});
}
cli(main);Connecting 50 clients into a "battle" room
Terminal
npx tsx loadtest/example.ts --room battle --numClients 50 --endpoint ws://localhost:2567Last updated on