lab3: итог

This commit is contained in:
2026-10-16 16:18:15 +03:00
parent 6cced49486
commit e42f02c016
141 changed files with 3180 additions and 4753 deletions
+76 -35
View File
@@ -17,6 +17,7 @@ export const BASE_URL = (__ENV.BASE_URL || 'http://haproxy:8080').replace(
);
const CHAT_COUNT = Number(__ENV.CHATS || 24);
const LOAD_USERS = 3;
const SEED_MESSAGES_PER_CHAT = __ENV.MODE === 'read' ? 50 : 0;
// Бюджет VU: на 1100 req/s при p95 около 25 ms достаточно около 30 активных VU;
// 60 предвыделенных и 200 максимальных оставляют запас, чтобы узкое место
// оказалось на сервере, а не в генераторе.
@@ -137,28 +138,67 @@ export function setup() {
});
}
const chats = [];
for (let index = 0; index < CHAT_COUNT; index += 1) {
const created = http.post(
`${BASE_URL}/api/v1/chats`,
JSON.stringify({
title: `k6 ${runId} chat ${index}`,
member_ids: users.map((user) => user.id),
}),
{
headers: {
'content-type': 'application/json',
authorization: `Bearer ${users[0].token}`,
},
},
);
if (created.status !== 201)
throw new Error(
`cannot create chat ${index}: ${created.status} ${created.body}`,
);
chats.push(JSON.parse(created.body).id);
}
return { runId, users, chats, profile: __ENV.PROFILE || 'spread' };
const chats = [];
for (let index = 0; index < CHAT_COUNT; index += 1) {
const created = http.post(
`${BASE_URL}/api/v1/chats`,
JSON.stringify({
title: `k6 ${runId} chat ${index}`,
member_ids: users.map((user) => user.id),
}),
{
headers: {
'content-type': 'application/json',
authorization: `Bearer ${users[0].token}`,
},
},
);
if (created.status !== 201)
throw new Error(
`cannot create chat ${index}: ${created.status} ${created.body}`,
);
const chat = JSON.parse(created.body);
chats.push(chat.id);
if (SEED_MESSAGES_PER_CHAT === 0) continue;
for (let messageIndex = 0; messageIndex < SEED_MESSAGES_PER_CHAT; messageIndex += 1) {
const key = `seed-${runId}-${index}-${messageIndex}`;
const seeded = http.post(
`${BASE_URL}/api/v1/chats/${chat.id}/messages`,
JSON.stringify({ text: `seed ${runId} ${index} ${messageIndex}` }),
{
headers: {
'content-type': 'application/json',
authorization: `Bearer ${users[0].token}`,
'idempotency-key': key,
},
},
);
if (seeded.status !== 201)
throw new Error(
`cannot seed chat ${index} message ${messageIndex}: ${seeded.status} ${seeded.body}`,
);
}
const seededPage = http.get(
`${BASE_URL}/api/v1/chats/${chat.id}/messages?limit=50`,
{ headers: { authorization: `Bearer ${users[0].token}` } },
);
if (seededPage.status !== 200)
throw new Error(
`cannot verify chat ${index} seed: ${seededPage.status} ${seededPage.body}`,
);
const seededItems = JSON.parse(seededPage.body).items;
if (!Array.isArray(seededItems) || seededItems.length !== SEED_MESSAGES_PER_CHAT)
throw new Error(
`cannot verify chat ${index} seed count: expected ${SEED_MESSAGES_PER_CHAT}, got ${seededItems?.length ?? 'invalid'}`,
);
}
return {
runId,
users,
chats,
profile: __ENV.PROFILE || 'spread',
seed_messages_per_chat: SEED_MESSAGES_PER_CHAT,
};
}
function chatFor(data) {
@@ -171,18 +211,18 @@ function actorFor(data) {
}
function recordResponse(step, response, expected) {
step.requests.add(1);
step.duration.add(response.timings.duration);
if (response.status === expected) {
step.status2xx.add(1);
return;
}
step.failed.add(1);
if (response.status >= 200 && response.status < 300) step.status2xx.add(1);
else if (response.status >= 400 && response.status < 500) step.status4xx.add(1);
else if (response.status >= 500 && response.status < 600) step.status5xx.add(1);
else step.statusOther.add(1);
step.vus.add(exec.instance.vusActive);
step.requests.add(1);
step.duration.add(response.timings.duration);
step.vus.add(exec.instance.vusActive);
if (response.status === expected) {
step.status2xx.add(1);
return;
}
step.failed.add(1);
if (response.status >= 200 && response.status < 300) step.status2xx.add(1);
else if (response.status >= 400 && response.status < 500) step.status4xx.add(1);
else if (response.status >= 500 && response.status < 600) step.status5xx.add(1);
else step.statusOther.add(1);
}
export function writeIteration(data, metrics) {
@@ -244,7 +284,7 @@ export function summaryJson(data, metrics, rates, kind, stepSeconds) {
p99: round(values['p(99)'] ?? 0, 2),
avg: round(values.avg ?? 0, 2),
max: round(values.max ?? 0, 2),
vus_active: data.metrics[`${name}_vus`]?.values?.value ?? 0,
vus_active: data.metrics[`${name}_vus`]?.values?.max ?? 0,
};
});
const global = data.metrics.http_req_duration?.values ?? {};
@@ -252,6 +292,7 @@ export function summaryJson(data, metrics, rates, kind, stepSeconds) {
label: __ENV.LABEL || 'unlabelled',
profile: __ENV.PROFILE || 'spread',
mode: __ENV.MODE || 'write',
seed_messages_per_chat: SEED_MESSAGES_PER_CHAT,
kind,
base_url: BASE_URL,
steps,