68 lines
2.7 KiB
JavaScript
68 lines
2.7 KiB
JavaScript
// Open-model load through the balancer: RATE requests per second regardless of response time.
|
|
// 70 % GET /users/me (session lookup), 30 % POST message with a unique Idempotency-Key.
|
|
// Every response adds a `served_by` sample tagged with X-Instance-Id and status, so the summary can show
|
|
// which replica answered each second. Tokens live only in this process.
|
|
import http from 'k6/http';
|
|
import encoding from 'k6/encoding';
|
|
import { Counter } from 'k6/metrics';
|
|
|
|
const BASE = __ENV.BASE_URL || 'http://localhost:8080';
|
|
const RATE = Number(__ENV.RATE || 150);
|
|
const DURATION = __ENV.DURATION || '40s';
|
|
const USERS = 10;
|
|
const PASSWORD = 'load-test-password';
|
|
|
|
const servedBy = new Counter('served_by');
|
|
|
|
export const options = {
|
|
scenarios: {
|
|
load: {
|
|
executor: 'constant-arrival-rate',
|
|
rate: RATE,
|
|
timeUnit: '1s',
|
|
duration: DURATION,
|
|
preAllocatedVUs: 50,
|
|
maxVUs: 300,
|
|
},
|
|
},
|
|
summaryTrendStats: ['avg', 'med', 'p(95)', 'p(99)', 'max'],
|
|
};
|
|
|
|
export function setup() {
|
|
const json = { headers: { 'Content-Type': 'application/json' } };
|
|
const users = [];
|
|
for (let i = 0; i < USERS; i++) {
|
|
const username = `load_${Date.now().toString(36)}_${i}`;
|
|
const user = http.post(`${BASE}/api/v1/users`, JSON.stringify({ username, display_name: 'load', password: PASSWORD }), json).json();
|
|
const basic = encoding.b64encode(`${username}:${PASSWORD}`);
|
|
const session = http.post(`${BASE}/api/v1/auth/sessions`, null, { headers: { Authorization: `Basic ${basic}` } }).json();
|
|
users.push({ id: user.id, token: session.token });
|
|
}
|
|
const chat = http.post(`${BASE}/api/v1/chats`, JSON.stringify({ title: 'load', member_ids: users.map((u) => u.id) }), {
|
|
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${users[0].token}` },
|
|
}).json();
|
|
return { users, chatId: chat.id };
|
|
}
|
|
|
|
export default function (data) {
|
|
const user = data.users[Math.floor(Math.random() * data.users.length)];
|
|
const auth = { Authorization: `Bearer ${user.token}` };
|
|
let response;
|
|
|
|
if (Math.random() < 0.7) {
|
|
response = http.get(`${BASE}/api/v1/users/me`, { headers: auth, tags: { name: 'me' }, timeout: '5s' });
|
|
} else {
|
|
response = http.post(
|
|
`${BASE}/api/v1/chats/${data.chatId}/messages`,
|
|
JSON.stringify({ text: `load ${__VU}-${__ITER}` }),
|
|
{
|
|
headers: { ...auth, 'Content-Type': 'application/json', 'Idempotency-Key': `load-${__VU}-${__ITER}-${Date.now()}` },
|
|
tags: { name: 'send' },
|
|
timeout: '5s',
|
|
},
|
|
);
|
|
}
|
|
|
|
servedBy.add(1, { instance: response.headers['X-Instance-Id'] || 'none', code: String(response.status) });
|
|
}
|