sharded-gotify/benchmark/k6/websocket-simple.js

75 lines
1.9 KiB
JavaScript

import ws from 'k6/ws';
import { check, sleep } from 'k6';
import http from 'k6/http';
import encoding from 'k6/encoding';
// Simple test for quick validation
export const options = {
vus: 100, // 100 virtual users
duration: '2m', // Run for 2 minutes
thresholds: {
'ws_connecting': ['p(95)<1000'],
'ws_session_duration': ['p(95)<5000'],
},
};
const BASE_URL = __ENV.BASE_URL || 'http://gotify-256:80';
const USERNAME = __ENV.USERNAME || 'admin';
const PASSWORD = __ENV.PASSWORD || 'admin';
let authToken = null;
let clientToken = null;
export function setup() {
const baseUrl = BASE_URL.replace(/^http/, 'http');
const credentials = encoding.b64encode(`${USERNAME}:${PASSWORD}`);
const clientRes = http.post(`${baseUrl}/client`, JSON.stringify({
name: `k6-simple-${__VU}-${Date.now()}`,
}), {
headers: {
'Content-Type': 'application/json',
'Authorization': `Basic ${credentials}`,
},
});
if (clientRes.status !== 200) {
console.error('Failed to create client:', clientRes.status, clientRes.body);
return {};
}
clientToken = JSON.parse(clientRes.body).token;
return { clientToken, baseUrl };
}
export default function (data) {
if (!data.clientToken) return;
const wsUrl = data.baseUrl.replace(/^http/, 'ws') + '/stream?token=' + data.clientToken;
const response = ws.connect(wsUrl, {}, function (socket) {
socket.on('open', () => console.log(`VU ${__VU}: Connected`));
socket.on('message', (data) => {
const msg = JSON.parse(data);
check(msg, { 'received message': (m) => m.id !== undefined });
});
socket.on('close', () => console.log(`VU ${__VU}: Disconnected`));
socket.on('error', (e) => {
if (e.error() !== 'websocket: close sent') {
console.error(`VU ${__VU}: Error:`, e.error());
}
});
sleep(30);
});
check(response, {
'connected successfully': (r) => r && r.status === 101,
});
sleep(1);
}