42 lines
1.8 KiB
JavaScript
42 lines
1.8 KiB
JavaScript
// Closed-loop load on GET /api/v1/users/me: every VU reuses one of TOKENS sessions, so VUS / TOKENS requests
|
|
// per session are in flight at any moment. The shell script empties Redis once a second during the measure phase.
|
|
import http from 'k6/http';
|
|
import { check } from 'k6';
|
|
import exec from 'k6/execution';
|
|
|
|
const BASE_URL = __ENV.BASE_URL || 'http://localhost:8080';
|
|
const VUS = Number(__ENV.VUS || 200);
|
|
const WARMUP_S = Number(__ENV.WARMUP_S || 5);
|
|
const DURATION_S = Number(__ENV.DURATION_S || 30);
|
|
const tokens = open(__ENV.TOKENS_FILE).split('\n').filter((line) => line.length > 0);
|
|
|
|
export const options = {
|
|
discardResponseBodies: true,
|
|
// One scenario for warm-up and measurement: the VUs keep their keep-alive connections, so the measure phase
|
|
// does not start with 200 fresh TCP connects through the Docker port forward.
|
|
scenarios: {
|
|
load: { executor: 'constant-vus', vus: VUS, duration: `${WARMUP_S + DURATION_S}s` },
|
|
},
|
|
summaryTrendStats: ['avg', 'med', 'p(95)', 'p(99)', 'max'],
|
|
thresholds: {
|
|
// Thresholds only make k6 report the measure-phase submetrics; they never fail the run.
|
|
'http_req_duration{phase:measure}': ['max>=0'],
|
|
'http_reqs{phase:measure}': ['count>=0'],
|
|
'http_req_failed{phase:measure}': ['rate>=0'],
|
|
},
|
|
};
|
|
|
|
export default function () {
|
|
const token = tokens[(__VU - 1) % tokens.length];
|
|
const phase = Date.now() - exec.scenario.startTime < WARMUP_S * 1000 ? 'warmup' : 'measure';
|
|
const response = http.get(`${BASE_URL}/api/v1/users/me`, {
|
|
headers: { Authorization: `Bearer ${token}` },
|
|
tags: { phase },
|
|
});
|
|
check(response, { 'status is 200': (r) => r.status === 200 });
|
|
}
|
|
|
|
export function handleSummary(data) {
|
|
return { [__ENV.SUMMARY_FILE]: JSON.stringify(data, null, 2) };
|
|
}
|