forked from mtusi2026/mtusi2026lab3
perf(bench): add scaling experiments and saturation diagnostics
Compare API and PostgreSQL CPU budgets and connection pool limits. Collect repeatable measurements and generate report aggregates and graphs.
This commit is contained in:
@@ -0,0 +1,166 @@
|
||||
import http from 'k6/http';
|
||||
import encoding from 'k6/encoding';
|
||||
import execution from 'k6/execution';
|
||||
import { check, fail, sleep } from 'k6';
|
||||
import { Counter, Trend } from 'k6/metrics';
|
||||
|
||||
const base = __ENV.BASE_URL || 'http://nginx';
|
||||
const rate = Number(__ENV.RATE || 100);
|
||||
const seconds = Number(__ENV.SECONDS || 30);
|
||||
const warmup = Number(__ENV.WARMUP || 10);
|
||||
const expected = Number(__ENV.EXPECTED || 1);
|
||||
const calibration = __ENV.CALIBRATION === '1';
|
||||
const attempts = new Counter('bench_attempts');
|
||||
const successes = new Counter('bench_successes');
|
||||
const failures = new Counter('bench_failures');
|
||||
const latency = new Trend('bench_latency', true);
|
||||
const successLatency = new Trend('bench_success_latency', true);
|
||||
const epoch = new Trend('bench_start_epoch');
|
||||
const operations = Object.fromEntries(['read', 'write'].map((op) => [op, {
|
||||
attempts: new Counter(`bench_${op}_attempts`),
|
||||
successes: new Counter(`bench_${op}_successes`),
|
||||
failures: new Counter(`bench_${op}_failures`),
|
||||
latency: new Trend(`bench_${op}_latency`, true),
|
||||
}]));
|
||||
const statuses = Object.fromEntries(['200', '201', 'other', 'network'].map((key) => [key, new Counter(`bench_status_${key}`)]));
|
||||
|
||||
const arrival = { executor: 'constant-arrival-rate', rate, timeUnit: '1s',
|
||||
preAllocatedVUs: 128, maxVUs: 128 };
|
||||
export const options = {
|
||||
scenarios: {
|
||||
warmup: { ...arrival, exec: 'warmupLoad', duration: `${warmup}s`, gracefulStop: '5s' },
|
||||
measurement: { ...arrival, exec: 'default', startTime: `${warmup}s`,
|
||||
duration: `${seconds}s`, gracefulStop: '10s' },
|
||||
},
|
||||
summaryTrendStats: ['min', 'med', 'avg', 'max', 'p(95)', 'p(99)'],
|
||||
setupTimeout: '120s',
|
||||
};
|
||||
|
||||
function request(method, path, token, body, key) {
|
||||
const headers = { Accept: 'application/json' };
|
||||
if (token) headers.Authorization = token;
|
||||
if (key) headers['Idempotency-Key'] = key;
|
||||
if (body !== undefined) headers['Content-Type'] = 'application/json';
|
||||
return http.request(method, base + path, body === undefined ? null : JSON.stringify(body), {
|
||||
headers, timeout: '5s', tags: { name: path.includes('/messages') ? 'messages' : 'setup' },
|
||||
});
|
||||
}
|
||||
|
||||
function required(response, status, field) {
|
||||
let data;
|
||||
try { data = response.json(); } catch { fail(`setup: invalid JSON, status ${response.status}`); }
|
||||
if (response.status !== status || !data?.[field]) fail(`setup: expected ${status}/${field}, got ${response.status}`);
|
||||
return data;
|
||||
}
|
||||
|
||||
export function setup() {
|
||||
const salt = `${Date.now().toString(36)}-${Math.floor(Math.random() * 1e9)}`;
|
||||
let token = 'Bearer calibration';
|
||||
let path = '/messages';
|
||||
if (!calibration) {
|
||||
const users = ['alice', 'bob'].map((label) => {
|
||||
const username = `bench_${label}_${salt.replaceAll('-', '')}`;
|
||||
const password = `T3st-${salt}-Aa1!`;
|
||||
const user = required(request('POST', '/api/v1/users', null,
|
||||
{ username, display_name: `Benchmark ${label}`, password }), 201, 'id');
|
||||
const auth = `Basic ${encoding.b64encode(`${username}:${password}`)}`;
|
||||
const session = required(request('POST', '/api/v1/auth/sessions', auth), 201, 'token');
|
||||
return { id: user.id, token: session.token };
|
||||
});
|
||||
token = `Bearer ${users[0].token}`;
|
||||
const chat = required(request('POST', '/api/v1/chats', token,
|
||||
{ title: 'Saturation benchmark', member_ids: users.map((u) => u.id) }), 201, 'id');
|
||||
path = `/api/v1/chats/${chat.id}/messages`;
|
||||
for (let i = 0; i < 30; i++) required(request('POST', path, token,
|
||||
{ text: `seed-${salt}-${i}` }, `seed-${salt}-${i}`), 201, 'id');
|
||||
}
|
||||
const seen = new Set();
|
||||
const until = Date.now() + 20000;
|
||||
do {
|
||||
const response = request('GET', `${path}?limit=50`, token);
|
||||
if (response.status !== 200) fail(`warmup status ${response.status}`);
|
||||
const id = response.headers['X-Instance-Id'];
|
||||
if (id) seen.add(id);
|
||||
if (seen.size === expected) break;
|
||||
sleep(0.02);
|
||||
} while (Date.now() < until);
|
||||
if (seen.size !== expected) fail(`warmup observed ${seen.size} instances, expected ${expected}`);
|
||||
return { token, path, salt };
|
||||
}
|
||||
|
||||
export function warmupLoad(data) { iteration(data, false); }
|
||||
export default function (data) { iteration(data, true); }
|
||||
|
||||
function iteration(data, measuring) {
|
||||
if (measuring && execution.scenario.iterationInTest === 0) epoch.add(Date.now());
|
||||
const sequence = execution.scenario.iterationInTest;
|
||||
const identity = `${execution.scenario.name}-${sequence}`;
|
||||
const op = ((sequence * 37 + 17) % 100) < 80 ? 'read' : 'write';
|
||||
if (measuring) { attempts.add(1); operations[op].attempts.add(1); }
|
||||
const response = op === 'read'
|
||||
? request('GET', `${data.path}?limit=50`, data.token)
|
||||
: request('POST', data.path, data.token, { text: `bench-${identity}-${data.salt}` }, `${data.salt}-${identity}`);
|
||||
if (!measuring) return;
|
||||
let payload;
|
||||
try { payload = response.json(); } catch { payload = null; }
|
||||
const instance = response.headers['X-Instance-Id'];
|
||||
const ok = response.status === (op === 'read' ? 200 : 201) && Boolean(instance)
|
||||
&& (op === 'read' ? Array.isArray(payload?.items) : Boolean(payload?.id));
|
||||
const elapsed = response.timings.duration + response.timings.blocked;
|
||||
latency.add(elapsed);
|
||||
successes.add(ok ? 1 : 0);
|
||||
failures.add(ok ? 0 : 1);
|
||||
operations[op].successes.add(ok ? 1 : 0);
|
||||
operations[op].failures.add(ok ? 0 : 1);
|
||||
if (ok) { successLatency.add(elapsed); operations[op].latency.add(elapsed); }
|
||||
statuses[String(response.status)]?.add(1);
|
||||
if (![200, 201].includes(response.status)) statuses[response.status === 0 ? 'network' : 'other'].add(1);
|
||||
if (instance) check(response, { [`instance:${instance}`]: () => true });
|
||||
}
|
||||
|
||||
export function handleSummary(data) {
|
||||
const count = (name) => data.metrics[name]?.values.count || 0;
|
||||
const times = (name) => ({ p50: data.metrics[name]?.values.med ?? null,
|
||||
p95: data.metrics[name]?.values['p(95)'] ?? null,
|
||||
p99: data.metrics[name]?.values['p(99)'] ?? null });
|
||||
const start = data.metrics.bench_start_epoch?.values.min;
|
||||
const totalDropped = count('dropped_iterations');
|
||||
const dropped = Math.max(0, rate * seconds - count('bench_attempts'));
|
||||
const instances = Object.fromEntries((data.root_group.checks || []).filter((c) => c.name.startsWith('instance:'))
|
||||
.map((c) => [c.name.slice(9), c.passes]));
|
||||
const group = (op) => {
|
||||
const prefix = op === 'all' ? 'bench' : `bench_${op}`;
|
||||
return { attempted: count(`${prefix}_attempts`), successful: count(`${prefix}_successes`),
|
||||
errors: count(`${prefix}_failures`), successful_rps: count(`${prefix}_successes`) / seconds,
|
||||
successful_latency_ms: times(op === 'all' ? 'bench_success_latency' : `${prefix}_latency`),
|
||||
status_counts: {}, error_counts: {}, instance_counts: op === 'all' ? instances : {} };
|
||||
};
|
||||
const result = {
|
||||
label: __ENV.LABEL, profile: { arrival_model: 'k6 constant-arrival-rate; one HTTP request per iteration',
|
||||
offered_rps: rate, duration_seconds: seconds, warmup_seconds: warmup, warmup_profile: 'same 80/20 workload at target rate',
|
||||
read_percent: 80, write_percent: 20, initial_messages: calibration ? 0 : 30, history_limit: 50,
|
||||
max_in_flight: 128, timeout_ms: 5000, automatic_retries: false, expected_instances: expected,
|
||||
phase: __ENV.PHASE || 'probe' },
|
||||
measurement: { scheduled_slots: rate * seconds, client_dropped_slots: dropped,
|
||||
scheduler_late_slots: 0, read: group('read'), write: group('write'), all: group('all') },
|
||||
timing: { measurement_started_epoch_ms: start,
|
||||
measurement_scheduled_end_epoch_ms: start + seconds * 1000,
|
||||
measurement_completed_epoch_ms: Date.now() },
|
||||
client: { engine: 'k6', scheduler_lateness_measured: false,
|
||||
raw_dropped_iterations_including_warmup: totalDropped,
|
||||
peak_vus: data.metrics.vus?.values.max, max_vus: 128 },
|
||||
all_latency_ms: times('bench_latency'), raw_metrics: data.metrics,
|
||||
notes: ['Setup and in-scenario mixed warmup excluded from custom bench metrics.',
|
||||
'Native warmup and measurement scenarios; no Date.now gating of measured requests.',
|
||||
'Latency uses native HTTP duration plus blocked time, not subtraction of wall-clock timestamps.',
|
||||
'Measurement missing slots are planned minus actual starts; raw k6 dropped iterations also include warmup.',
|
||||
'scheduler_late_slots is a compatibility placeholder, not a k6 measurement.',
|
||||
'Dropped iterations and actual delivered fraction must be checked.',
|
||||
'No credentials or message bodies are exported.'],
|
||||
};
|
||||
result.measurement.all.status_counts = Object.fromEntries(['200', '201', 'other', 'network']
|
||||
.map((status) => [status, count(`bench_status_${status}`)]));
|
||||
return { [__ENV.OUTPUT]: JSON.stringify(result, null, 2),
|
||||
stdout: JSON.stringify({ label: result.label, attempted: result.measurement.all.attempted,
|
||||
errors: result.measurement.all.errors, p95: result.all_latency_ms.p95, dropped }) + '\n' };
|
||||
}
|
||||
Reference in New Issue
Block a user