Files
mtusi2026lab3/bench/k6/read.js
T
ricoromaandClaude Opus 5.5 af4bca08c9 feat: carry lab 2 over and add X-Instance-Id and idempotency keys
- carry the lab 2 solution over and plan lab 3 (PLAN.md, OpenSpec
  change instance-id-idempotency)
- rename the compose project to mtusi-lab3 and the image to :lab3
- add X-Instance-Id to every response and instance_id to every log
  record ([app] instance_id, hostname by default)
- require Idempotency-Key on POST messages: keys table in Postgres,
  key and message in one transaction, replay returns the same 201,
  another body gets 409, TTL from [idempotency] ttl_seconds
- send the key from student-tests, k6 scenarios and the skill driver;
  add idempotency, X-Instance-Id, key race and fault tests

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 19:42:36 +03:00

70 lines
2.1 KiB
JavaScript

// Сценарий read: страница истории читаемого чата с курсором, случайно
// выбранным из пула. У каждого VU свой генератор с зерном SEED + номер VU:
// последовательность одинакова в разных метках и различна у клиентов.
import exec from "k6/execution";
import http from "k6/http";
import { check } from "k6";
import { SharedArray } from "k6/data";
import {
API,
PAGE_LIMIT,
SUMMARY_THRESHOLDS,
SUMMARY_TREND_STATS,
WORK_DIR,
authHeaders,
mulberry32,
summaryFiles,
} 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 SEED = Number(__ENV.SEED || 0);
// отладка стенда: напечатать первые TRACE выборов каждого VU
const TRACE = Number(__ENV.TRACE || 0);
const URL = `${API}/chats/${fixture.readChatId}/messages`;
const PARAMS = {
headers: authHeaders(fixture.tokens[fixture.readerId]),
responseCallback: http.expectedStatuses(200),
};
export const options = {
scenarios: {
read: {
executor: "constant-vus",
vus: Number(__ENV.VUS),
duration: __ENV.DURATION,
gracefulStop: "5s",
},
},
summaryTrendStats: SUMMARY_TREND_STATS,
thresholds: SUMMARY_THRESHOLDS,
};
let random = null;
let picks = 0;
export default function () {
if (random === null) {
random = mulberry32(SEED + exec.vu.idInTest);
}
const index = Math.floor(random() * cursors.length);
if (picks < TRACE) {
console.log(`trace vu=${exec.vu.idInTest} pick=${picks} index=${index}`);
}
picks += 1;
const cursor = encodeURIComponent(cursors[index]);
const res = http.get(`${URL}?limit=${PAGE_LIMIT}&cursor=${cursor}`, PARAMS);
check(res, {
"status 200": (r) => r.status === 200,
"full page": (r) => r.status === 200 && r.json("items.#") === PAGE_LIMIT,
});
}
export function handleSummary(data) {
return summaryFiles(data);
}