forked from mtusi2026/mtusi2026lab3
Lab3 - оценка 5
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
import http from 'k6/http';
|
||||
import encoding from 'k6/encoding';
|
||||
import { check } from 'k6';
|
||||
import { Counter } from 'k6/metrics';
|
||||
|
||||
const BASE = __ENV.BASE_URL || 'http://lb:8080';
|
||||
const USERS = parseInt(__ENV.USERS || '32');
|
||||
const RUN = __ENV.RUN_ID || String(Date.now());
|
||||
const non2xx = new Counter('non_2xx');
|
||||
|
||||
export const options = {
|
||||
scenarios: {
|
||||
main: {
|
||||
executor: 'constant-vus',
|
||||
vus: parseInt(__ENV.VUS || '64'),
|
||||
duration: __ENV.DURATION || '30s',
|
||||
},
|
||||
},
|
||||
setupTimeout: '120s',
|
||||
summaryTrendStats: ['avg', 'p(50)', 'p(95)', 'p(99)', 'max'],
|
||||
discardResponseBodies: false,
|
||||
};
|
||||
|
||||
export function setup() {
|
||||
const users = [];
|
||||
for (let i = 0; i < USERS; i++) {
|
||||
const username = `t1_${RUN}_${i}`.slice(0, 32);
|
||||
const password = `T3st-password-${RUN}-${i}`;
|
||||
let r = http.post(`${BASE}/api/v1/users`, JSON.stringify({ username, display_name: `Load ${i}`, password }),
|
||||
{ headers: { 'Content-Type': 'application/json' } });
|
||||
if (r.status !== 201) throw new Error(`create user: ${r.status}`);
|
||||
const id = r.json('id');
|
||||
r = http.post(`${BASE}/api/v1/auth/sessions`, null,
|
||||
{ headers: { Authorization: 'Basic ' + encoding.b64encode(`${username}:${password}`) } });
|
||||
if (r.status !== 201) throw new Error(`login: ${r.status}`);
|
||||
const token = r.json('token');
|
||||
r = http.post(`${BASE}/api/v1/chats`, JSON.stringify({ title: `load ${i}`, member_ids: [id] }),
|
||||
{ headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` } });
|
||||
if (r.status !== 201) throw new Error(`create chat: ${r.status}`);
|
||||
users.push({ token, chat: r.json('id') });
|
||||
}
|
||||
return { users };
|
||||
}
|
||||
|
||||
export default function (data) {
|
||||
const u = data.users[(__VU - 1) % data.users.length];
|
||||
const auth = { Authorization: `Bearer ${u.token}` };
|
||||
let r;
|
||||
if (Math.random() < 0.7) {
|
||||
r = http.post(`${BASE}/api/v1/chats/${u.chat}/messages`,
|
||||
JSON.stringify({ text: `k6 ${__VU}-${__ITER}` }),
|
||||
{ headers: Object.assign({ 'Content-Type': 'application/json',
|
||||
'Idempotency-Key': `${RUN}-${__VU}-${__ITER}` }, auth), tags: { op: 'post' } });
|
||||
check(r, { 'post 201': (x) => x.status === 201 });
|
||||
} else {
|
||||
r = http.get(`${BASE}/api/v1/chats/${u.chat}/messages?limit=20`, { headers: auth, tags: { op: 'get' } });
|
||||
check(r, { 'get 200': (x) => x.status === 200 });
|
||||
}
|
||||
if (r.status < 200 || r.status >= 300) non2xx.add(1);
|
||||
}
|
||||
Reference in New Issue
Block a user