Реализовать lab3 в общем Helm-релизе с Traefik, TLS и HPA

This commit is contained in:
Vladislav Agapov
2026-10-16 11:51:20 +03:00
parent a57cbcf329
commit 9494547300
88 changed files with 1922 additions and 255 deletions
+18
View File
@@ -0,0 +1,18 @@
import http from 'k6/http';
import encoding from 'k6/encoding';
import { check, sleep } from 'k6';
export const options = { vus: 2, duration: '90s', thresholds: { checks: ['rate>0.95'] } };
const base = __ENV.BASE_URL || 'https://hl.agapov.space';
export function setup() {
const username = `hpa_${Date.now()}`;
const password = 'synthetic-autoscaling-password';
const response = http.post(`${base}/api/v1/users`, JSON.stringify({ username, display_name: 'HPA CPU test', password }), { headers: { 'Content-Type': 'application/json' } });
if (response.status !== 201) throw new Error(`registration: ${response.status}`);
return { authorization: `Basic ${encoding.b64encode(`${username}:${password}`)}` };
}
export default function (data) {
const response = http.post(`${base}/api/v1/auth/sessions`, null, { headers: { Authorization: data.authorization } });
check(response, { 'authenticated login': r => r.status === 201 });
// Keep one account below its 20 requests/s auth budget.
sleep(0.1);
}
+34
View File
@@ -0,0 +1,34 @@
import http from 'k6/http';
import encoding from 'k6/encoding';
import { check, sleep } from 'k6';
export const options = { vus: 8, iterations: 32, thresholds: { checks: ['rate==1'] } };
const base = __ENV.BASE_URL || 'https://hl.agapov.space';
export function setup() {
const username = `idem_${Date.now()}`;
const password = 'synthetic-idempotency-password';
const registered = http.post(`${base}/api/v1/users`, JSON.stringify({ username, display_name: 'Idempotency', password }), { headers: { 'Content-Type': 'application/json' } });
if (registered.status !== 201) throw new Error(`registration ${registered.status}`);
const session = http.post(`${base}/api/v1/auth/sessions`, null, { headers: { Authorization: `Basic ${encoding.b64encode(`${username}:${password}`)}` } });
if (session.status !== 201) throw new Error(`login ${session.status}`);
const headers = { Authorization: `Bearer ${session.json().token}`, 'Content-Type': 'application/json', 'Idempotency-Key': 'shared-key' };
const chat = http.post(`${base}/api/v1/chats`, JSON.stringify({ title: 'Retry race', member_ids: [registered.json().id] }), { headers });
if (chat.status !== 201) throw new Error(`chat ${chat.status}`);
return { headers, path: `${base}/api/v1/chats/${chat.json().id}/messages` };
}
export default function (data) {
// Eight writers share four user slots. Retry admission failures with the same key/body.
let response;
for (let attempt = 0; attempt < 20; attempt++) {
response = http.post(data.path, JSON.stringify({ text: 'one logical message' }), { headers: data.headers });
if (response.status !== 503 && response.status !== 429) break;
sleep(0.1);
}
check(response, { 'concurrent retry succeeds': r => r.status === 201 });
console.log(`instance=${response.headers['X-Instance-Id']}, status=${response.status}`);
}
export function teardown(data) {
const history = http.get(`${data.path}?limit=100`, { headers: data.headers });
check(history, { 'exactly one stored message': r => r.status === 200 && r.json().items.length === 1 });
const conflict = http.post(data.path, JSON.stringify({ text: 'different message' }), { headers: data.headers });
check(conflict, { 'different body conflicts': r => r.status === 409 });
}
+62
View File
@@ -0,0 +1,62 @@
import http from 'k6/http';
import encoding from 'k6/encoding';
import execution from 'k6/execution';
import { check } from 'k6';
import { Counter, Trend } from 'k6/metrics';
const base = __ENV.BASE_URL || 'https://hl.agapov.space';
const seconds = Number(__ENV.DURATION || 30);
const users = Number(__ENV.USERS || 16);
const writes = new Counter('confirmed_messages');
const attempts = new Counter('attempted_messages');
const failures = new Counter('application_errors');
const latency = new Trend('message_latency_ms', true);
export const options = {
scenarios: {
warmup: { executor: 'constant-vus', vus: 4, duration: '5s' },
measured: { executor: 'constant-vus', vus: Number(__ENV.VUS || 32), duration: `${seconds}s`, startTime: '5s' },
},
setupTimeout: '120s',
summaryTrendStats: ['avg', 'p(50)', 'p(95)', 'p(99)', 'max'],
};
export function setup() {
const actors = [];
const prefix = `bench_${Date.now()}`;
for (let i = 0; i < users; i++) {
const username = `${prefix}_${i}`;
const password = 'synthetic-benchmark-password';
let response = http.post(`${base}/api/v1/users`, JSON.stringify({ username, display_name: 'Synthetic', password }), { headers: { 'Content-Type': 'application/json' }, tags: { phase: 'setup' } });
if (response.status !== 201) throw new Error(`registration: ${response.status}`);
const user = response.json();
response = http.post(`${base}/api/v1/auth/sessions`, null, { headers: { Authorization: `Basic ${encoding.b64encode(`${username}:${password}`)}` }, tags: { phase: 'setup' } });
if (response.status !== 201) throw new Error(`login: ${response.status}`);
const token = response.json().token;
response = http.post(`${base}/api/v1/chats`, JSON.stringify({ title: 'Synthetic benchmark', member_ids: [user.id] }), { headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json' }, tags: { phase: 'setup' } });
if (response.status !== 201) throw new Error(`chat: ${response.status}`);
actors.push({ token, chat: response.json().id });
}
return actors;
}
export default function (actors) {
const measured = execution.scenario.name === 'measured';
const actor = actors[(__VU - 1) % actors.length];
const headers = { Authorization: `Bearer ${actor.token}`, 'Content-Type': 'application/json', 'Idempotency-Key': `${execution.scenario.name}:${__VU}:${__ITER}` };
const path = `${base}/api/v1/chats/${actor.chat}/messages`;
const payload = JSON.stringify({ text: `synthetic message ${__VU}:${__ITER}` });
const response = http.post(path, payload, { headers, tags: { phase: 'load' } });
if (measured) { latency.add(response.timings.duration); attempts.add(1); }
if (check(response, { 'message confirmed': r => r.status === 201 })) { if (measured) writes.add(1); }
else if (measured) failures.add(1, { status: String(response.status) });
if (response.status === 201 && __ITER % 10 === 0) {
const repeated = http.post(path, payload, { headers, tags: { phase: 'load' } });
if (!check(repeated, { 'retry returns original': r => r.status === 201 && r.json().id === response.json().id })) { if (measured) failures.add(1, { status: String(repeated.status) }); }
}
const read = http.get(`${path}?limit=10`, { headers, tags: { phase: 'load' } });
if (!check(read, { 'history readable': r => r.status === 200 })) { if (measured) failures.add(1, { status: String(read.status) }); }
}
export function handleSummary(data) {
return { stdout: JSON.stringify({ profile: { vus: Number(__ENV.VUS || 32), seconds, warmup_seconds: 5, users }, summary: data }) + '\n' };
}