forked from mtusi2026/mtusi2026lab5
71 lines
2.2 KiB
JavaScript
71 lines
2.2 KiB
JavaScript
import http from 'k6/http';
|
|
import encoding from 'k6/encoding';
|
|
import { check } from 'k6';
|
|
|
|
const baseURL = __ENV.BASE_URL || 'http://localhost:8080';
|
|
const vus = Number(__ENV.VUS || 10);
|
|
const duration = __ENV.DURATION || '60s';
|
|
|
|
export const options = {
|
|
scenarios: {
|
|
writes: {
|
|
executor: 'constant-vus',
|
|
vus,
|
|
duration,
|
|
},
|
|
},
|
|
summaryTrendStats: ['avg', 'min', 'med', 'p(50)', 'p(95)', 'p(99)', 'max'],
|
|
};
|
|
|
|
function headers(token) {
|
|
return {
|
|
Authorization: `Bearer ${token}`,
|
|
'Content-Type': 'application/json',
|
|
Accept: 'application/json',
|
|
};
|
|
}
|
|
|
|
export function setup() {
|
|
const suffix = `${Date.now()}_${Math.floor(Math.random() * 1e6)}`;
|
|
const username = `u_load_${suffix}`.slice(0, 32);
|
|
const password = `Load-${suffix}-pass`;
|
|
|
|
const registration = http.post(
|
|
`${baseURL}/api/v1/users`,
|
|
JSON.stringify({ username, display_name: 'Load test', password }),
|
|
{ headers: { 'Content-Type': 'application/json', Accept: 'application/json' } },
|
|
);
|
|
check(registration, { 'register 201': (r) => r.status === 201 });
|
|
const user = registration.json();
|
|
|
|
const basic = encoding.b64encode(`${username}:${password}`);
|
|
const login = http.post(`${baseURL}/api/v1/auth/sessions`, null, {
|
|
headers: { Authorization: `Basic ${basic}`, Accept: 'application/json' },
|
|
});
|
|
check(login, { 'login 201': (r) => r.status === 201 });
|
|
const token = login.json('token');
|
|
|
|
const chat = http.post(
|
|
`${baseURL}/api/v1/chats`,
|
|
JSON.stringify({ title: 'Load profile', member_ids: [user.id] }),
|
|
{ headers: headers(token) },
|
|
);
|
|
check(chat, { 'chat 201': (r) => r.status === 201 });
|
|
|
|
return { token, chatID: chat.json('id') };
|
|
}
|
|
|
|
export default function (data) {
|
|
const key = `${__VU}-${__ITER}-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
|
|
const response = http.post(
|
|
`${baseURL}/api/v1/chats/${data.chatID}/messages`,
|
|
JSON.stringify({ text: `load message ${key}` }),
|
|
{ headers: { ...headers(data.token), 'Idempotency-Key': key } },
|
|
);
|
|
|
|
check(response, {
|
|
'message 201': (r) => r.status === 201,
|
|
'instance header': (r) => Boolean(r.headers['X-Instance-Id'] || r.headers['x-instance-id']),
|
|
});
|
|
}
|