Files
mtusi2026lab6/bench/k6/mixed.js
T
ricoromaandClaude Opus 5.5 9cc6c0c7a6 chore: port lab 5 solution into the lab 6 skeleton
Source: mtusi2026lab5-safronov, solution/bvt2301-safronov, 046ec9e.
Contracts, public tests and evidence of lab 5 are not carried over.
The compose project, image and CI prefix are renamed to lab6; the first
broker start defect is recorded in KNOWN_ISSUES.md; PLAN.md outlines the
OpenSpec changes of lab 6.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 04:46:56 +03:00

218 lines
7.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Ступень серии трека 1 (решения 2–3 design change scaling-bench):
// открытая модель, три сценария ramping-arrival-rate с долями смеси —
// страница истории читаемого чата, отправка с новым Idempotency-Key в один
// из чатов записи, GET /users/me. Ступень — подъём от START до LEVEL за
// WARMUP_S (в замер не входит) и окно MEASURE_S с постоянной
// интенсивностью. Каждый запрос получает тег window (warm / hold) по
// времени отправки от начала сценария; итог содержит подметрики окна.
// Время setup (t0) — часы ВМ Docker: по нему run.py режет отсчёты
// сэмплера. Вход по Basic — в prepare.js, до ступеней.
import exec from "k6/execution";
import http from "k6/http";
import { check } from "k6";
import { SharedArray } from "k6/data";
import { Counter } from "k6/metrics";
import {
API,
PAGE_LIMIT,
SUMMARY_TREND_STATS,
WORK_DIR,
authHeaders,
messageHeaders,
mulberry32,
summaryFiles,
syntheticText,
} from "./lib.js";
const fixture = JSON.parse(open(`${WORK_DIR}/fixture.json`));
const cursors = new SharedArray(
"cursors",
() => JSON.parse(open(`${WORK_DIR}/cursors.json`)).cursors,
);
const LEVEL = Number(__ENV.LEVEL);
const START = Number(__ENV.START || 0);
// «read:0.7,write:0.2,me:0.1» — Profile.mix
const MIX = (__ENV.MIX || "read:0.7,write:0.2,me:0.1")
.split(",")
.map((item) => item.split(":"))
.map(([name, share]) => [name, Number(share)]);
const WARMUP_S = Number(__ENV.WARMUP_S);
const MEASURE_S = Number(__ENV.MEASURE_S);
const WARMUP_MS = WARMUP_S * 1000;
// больше timeout server LB (10 с): клиент видит ответ LB, а не свой таймаут
const TIMEOUT_S = Number(__ENV.TIMEOUT || 15);
const PRE_VUS_PER_RPS = Number(__ENV.PRE_VUS_PER_RPS || 0.05);
const MAX_VUS_PER_RPS = Number(__ENV.MAX_VUS_PER_RPS || 0.2);
const SEED = Number(__ENV.SEED || 0);
const STEP = Number(__ENV.STEP || 0);
// короткие ID реплик: подметрики распределения по X-Instance-Id
const INSTANCES = (__ENV.INSTANCES || "").split(",").filter((x) => x);
const ABORT_ERRORS = Number(__ENV.ABORT_ERRORS || 0.5);
const ABORT_P95_MS = Number(__ENV.ABORT_P95_MS || 2000);
// коды неуспешных ответов окна (bench/scaling.py, ERROR_STATUSES)
const ERROR_STATUSES = [0, 400, 401, 403, 404, 409, 422, 429, 500, 502, 503, 504];
const READ_URL = `${API}/chats/${fixture.readChatId}/messages`;
const READ_HEADERS = authHeaders(fixture.tokens[fixture.readerId]);
const USERS = [fixture.readerId, ...fixture.memberIds];
const WRITE_CHATS = fixture.writeChatIds || [fixture.writeChatId];
const shortPages = new Counter("short_pages");
const instanceOk = new Counter("instance_ok");
function scenario(name, share) {
const target = Math.round(LEVEL * share);
const start = Math.round(START * share);
return {
executor: "ramping-arrival-rate",
exec: name,
startRate: start,
timeUnit: "1s",
preAllocatedVUs: Math.ceil(target * PRE_VUS_PER_RPS) + 10,
maxVUs: Math.ceil(target * MAX_VUS_PER_RPS) + 20,
stages: [
{ target, duration: `${WARMUP_S}s` },
{ target, duration: `${MEASURE_S}s` },
],
// запрос, начатый в конце окна, успевает дождаться ответа или таймаута
gracefulStop: `${TIMEOUT_S + 1}s`,
};
}
const HOLD = "window:hold";
const thresholds = {
// досрочный выход по подметрикам окна (код k6 99): перегрузка не тянется
// до конца ступени
[`http_req_duration{${HOLD},expected_response:true}`]: [
"max>=0",
{
threshold: `p(95)<${ABORT_P95_MS}`,
abortOnFail: true,
delayAbortEval: `${WARMUP_S + 5}s`,
},
],
[`http_req_failed{${HOLD}}`]: [
"rate>=0",
{
threshold: `rate<=${ABORT_ERRORS}`,
abortOnFail: true,
delayAbortEval: `${WARMUP_S + 5}s`,
},
],
[`short_pages{${HOLD}}`]: ["count>=0"],
[`checks{${HOLD}}`]: ["rate>=0"],
};
// остальные пороги всегда выполняются: они нужны, чтобы подметрики попали
// в итог
for (const [name] of MIX) {
thresholds[`http_reqs{${HOLD},scenario:${name}}`] = ["count>=0"];
thresholds[`http_req_failed{${HOLD},scenario:${name}}`] = ["rate>=0"];
thresholds[
`http_req_duration{${HOLD},expected_response:true,scenario:${name}}`
] = ["max>=0"];
thresholds[`http_reqs{scenario:${name}}`] = ["count>=0"];
thresholds[`http_req_failed{scenario:${name}}`] = ["rate>=0"];
}
for (const id of [...INSTANCES, "-"]) {
thresholds[`instance_ok{${HOLD},instance:${id}}`] = ["count>=0"];
}
for (const status of ERROR_STATUSES) {
thresholds[`http_reqs{${HOLD},status:${status}}`] = ["count>=0"];
}
export const options = {
scenarios: Object.fromEntries(
MIX.map(([name, share]) => [name, scenario(name, share)]),
),
summaryTrendStats: SUMMARY_TREND_STATS,
thresholds,
};
export function setup() {
return { t0: Date.now() };
}
// тег окна по времени отправки от начала сценария
function windowTag() {
return Date.now() - exec.scenario.startTime < WARMUP_MS ? "warm" : "hold";
}
function counted(res, ok, window) {
if (ok) {
instanceOk.add(1, {
window,
instance: res.headers["X-Instance-Id"] || "-",
});
}
}
let random = null;
export function read() {
if (random === null) {
random = mulberry32(SEED + exec.vu.idInTest);
}
const window = windowTag();
const cursor = encodeURIComponent(
cursors[Math.floor(random() * cursors.length)],
);
const res = http.get(`${READ_URL}?limit=${PAGE_LIMIT}&cursor=${cursor}`, {
headers: READ_HEADERS,
timeout: `${TIMEOUT_S}s`,
tags: { window },
responseCallback: http.expectedStatuses(200),
});
const full = res.status === 200 && res.json("items.#") === PAGE_LIMIT;
if (res.status === 200 && !full) {
// неполная страница при 200: отдельный счётчик, без двойного учёта
shortPages.add(1, { window });
}
check(res, { "read 200 full page": () => full }, { window });
counted(res, full, window);
}
export function write() {
const n = exec.scenario.iterationInTest;
const sender = fixture.memberIds[n % fixture.memberIds.length];
const chat = WRITE_CHATS[
Math.floor(n / fixture.memberIds.length) % WRITE_CHATS.length
];
const window = windowTag();
const res = http.post(
`${API}/chats/${chat}/messages`,
JSON.stringify({ text: syntheticText(`s${STEP}`, n) }),
{
// ключ — новый на каждую отправку (lib.js); стенд не повторяет
headers: messageHeaders(fixture.tokens[sender]),
responseType: "none",
timeout: `${TIMEOUT_S}s`,
tags: { window },
responseCallback: http.expectedStatuses(201),
},
);
const ok = res.status === 201;
check(res, { "write 201": () => ok }, { window });
counted(res, ok, window);
}
export function me() {
const n = exec.scenario.iterationInTest;
const user = USERS[n % USERS.length];
const window = windowTag();
const res = http.get(`${API}/users/me`, {
headers: authHeaders(fixture.tokens[user]),
responseType: "none",
timeout: `${TIMEOUT_S}s`,
tags: { window },
responseCallback: http.expectedStatuses(200),
});
const ok = res.status === 200;
check(res, { "me 200": () => ok }, { window });
counted(res, ok, window);
}
export function handleSummary(data) {
return summaryFiles(data);
}