snapshot of https://github.com/centrifugal/centrifugo.git (master, 2026-09-16; blobs > 131072B omitted)
This commit is contained in:
78
misc/benchmarking/k6/benchmark.js
Normal file
78
misc/benchmarking/k6/benchmark.js
Normal file
@@ -0,0 +1,78 @@
|
||||
import ws from 'k6/ws';
|
||||
import { check } from 'k6';
|
||||
import crypto from "k6/crypto";
|
||||
import encoding from "k6/encoding";
|
||||
|
||||
const hmacSecret = "secret";
|
||||
|
||||
const algToHash = {
|
||||
HS256: "sha256",
|
||||
HS384: "sha384",
|
||||
HS512: "sha512"
|
||||
};
|
||||
|
||||
function sign(data, hashAlg, secret) {
|
||||
let hasher = crypto.createHMAC(hashAlg, secret);
|
||||
hasher.update(data);
|
||||
// Some manual base64 encoding as `Hasher.digest(encodingType)` doesn't support that encoding type yet.
|
||||
return hasher.digest("base64").replace(/\//g, "_").replace(/\+/g, "-").replace(/=/g, "");
|
||||
}
|
||||
|
||||
function encode(payload, secret, algorithm) {
|
||||
algorithm = algorithm || "HS256";
|
||||
let header = encoding.b64encode(JSON.stringify({ typ: "JWT", alg: algorithm }), "rawurl");
|
||||
payload = encoding.b64encode(JSON.stringify(payload), "rawurl");
|
||||
let sig = sign(header + "." + payload, algToHash[algorithm], secret);
|
||||
return [header, payload, sig].join(".");
|
||||
}
|
||||
|
||||
// Generate a token for each user separately using HMAC JWT
|
||||
function generateToken(userId) {
|
||||
const payload = {
|
||||
sub: userId,
|
||||
exp: Math.floor(Date.now() / 1000) + 60, // Expires in 60 seconds
|
||||
};
|
||||
return encode(payload, hmacSecret, "HS256")
|
||||
}
|
||||
|
||||
export let options = {
|
||||
stages: [
|
||||
{ duration: "10s", target: 1000 },
|
||||
{ duration: "50s", target: 1000 },
|
||||
]
|
||||
};
|
||||
|
||||
export default function () {
|
||||
const url = 'ws://localhost:8000/connection/websocket';
|
||||
|
||||
const user = `user_${__VU}_${__ITER}`;
|
||||
const token = generateToken(user);
|
||||
|
||||
const response = ws.connect(url, {}, function (socket) {
|
||||
socket.on('open', () => {
|
||||
const connectCommand = JSON.stringify({ id: 1, connect: { token: token } });
|
||||
const subscribeCommand = JSON.stringify({ id: 2, subscribe: { channel: 'test' } });
|
||||
socket.send(connectCommand + "\n" + subscribeCommand);
|
||||
});
|
||||
|
||||
socket.on('message', (message) => {
|
||||
// Respond to server pings.
|
||||
const substrings = message.split("\n");
|
||||
if (substrings.includes("{}")) {
|
||||
socket.send('{}');
|
||||
}
|
||||
});
|
||||
|
||||
socket.on('close', () => {});
|
||||
|
||||
socket.on('error', (error) => {
|
||||
console.log('WebSocket error: ', error);
|
||||
});
|
||||
|
||||
socket.setTimeout(() => {
|
||||
socket.close();
|
||||
}, 60000);
|
||||
});
|
||||
|
||||
check(response, { "status is 101": (r) => r && r.status === 101 });
|
||||
}
|
||||
27
misc/benchmarking/k6/readme.md
Normal file
27
misc/benchmarking/k6/readme.md
Normal file
@@ -0,0 +1,27 @@
|
||||
A simple script which demonstrates benchmarking Centrifugo with [k6](https://k6.io/). You can use it as a starting point.
|
||||
|
||||
It connects to Centrifugo with JWT per user, subscribes to the `test` channel, waits some time and exits.
|
||||
|
||||
## Running test
|
||||
|
||||
Run Centrifugo with config like this:
|
||||
|
||||
```json
|
||||
{
|
||||
"token_hmac_secret_key": "secret",
|
||||
"admin": true,
|
||||
"admin_password": "password",
|
||||
"admin_secret": "0453661f-8b95-4f75-ab49-cfb2e3b2024f",
|
||||
"api_key": "7d8cee29-f5c0-419e-ae70-760767b21f8e",
|
||||
"allow_subscribe_for_client": true,
|
||||
"allowed_origins": []
|
||||
}
|
||||
```
|
||||
|
||||
Then:
|
||||
|
||||
```
|
||||
k6 run benchmark.js
|
||||
```
|
||||
|
||||
Make sure [your system is tuned](https://centrifugal.dev/docs/server/infra_tuning) for dealing with many connections.
|
||||
48
misc/benchmarking/k6/uni_ws/benchmark.js
Normal file
48
misc/benchmarking/k6/uni_ws/benchmark.js
Normal file
@@ -0,0 +1,48 @@
|
||||
import ws from 'k6/ws';
|
||||
import { check } from 'k6';
|
||||
|
||||
export let options = {
|
||||
stages: [
|
||||
// Ramp up to 5000 connections over 60 seconds
|
||||
{ duration: '60s', target: 5000 },
|
||||
// Hold 5000 connections for 4 minutes
|
||||
{ duration: '4m', target: 5000 },
|
||||
// Ramp down to 0 connections
|
||||
{ duration: '10s', target: 0 },
|
||||
],
|
||||
};
|
||||
|
||||
export default function () {
|
||||
const url = 'ws://localhost:8000/connection/uni_websocket?cf_connect={}';
|
||||
|
||||
const response = ws.connect(url, {}, function (socket) {
|
||||
socket.on('open', function open() {
|
||||
console.log('WebSocket connection established');
|
||||
});
|
||||
|
||||
socket.on('message', function message(data) {
|
||||
console.log('Received message:', data);
|
||||
});
|
||||
|
||||
socket.on('close', function close() {
|
||||
console.log('WebSocket connection closed');
|
||||
});
|
||||
|
||||
socket.on('error', function error(e) {
|
||||
console.log('WebSocket error:', e.error());
|
||||
});
|
||||
|
||||
// Keep the connection alive for the duration of the test
|
||||
// The connection will be closed when the VU iteration ends
|
||||
socket.setTimeout(function () {
|
||||
socket.close();
|
||||
}, 300000); // 5 minutes timeout as safety measure
|
||||
|
||||
// Send a ping every 30 seconds to keep connection active
|
||||
socket.setInterval(function () {
|
||||
socket.ping();
|
||||
}, 30000);
|
||||
});
|
||||
|
||||
check(response, { 'status is 101': (r) => r && r.status === 101 });
|
||||
}
|
||||
5
misc/benchmarking/k6/uni_ws/readme.md
Normal file
5
misc/benchmarking/k6/uni_ws/readme.md
Normal file
@@ -0,0 +1,5 @@
|
||||
Makes 5k conns to unidirectional websocket endpoint during 1 min, waits 4 mins, stops.
|
||||
|
||||
```
|
||||
k6 run benchmark.js
|
||||
```
|
||||
Reference in New Issue
Block a user