Files
sonar-test-nest/k6/load-test.js
demo-bot e034b1be06
All checks were successful
Build and Push to ACR / Build and Push (push) Successful in 4m54s
feat(k6): add bespoke load test files
2026-05-11 10:43:02 +00:00

84 lines
2.7 KiB
JavaScript

import http from 'k6/http';
import { check, sleep, group } from 'k6';
const vus = parseInt(__ENV.TEST_VUS || '10');
const duration = __ENV.TEST_DURATION || '30s';
const targetUrl = __ENV.TARGET_URL || 'http://sonar-test-nest.dev.svc.cluster.local:3000';
export const options = {
scenarios: {
load_test: {
executor: 'ramping-vus',
startVUs: 0,
stages: [
{ duration: '10s', target: vus },
{ duration: duration, target: vus },
{ duration: '5s', target: 0 },
],
},
},
thresholds: {
http_req_duration: ['p(95)<500'],
http_req_failed: ['rate<0.01'],
},
};
http.setResponseCallback(http.expectedStatuses({ min: 200, max: 399 }));
export default function () {
group('Health API', () => {
const res = http.get(`${targetUrl}/health`);
check(res, {
'status is 200': (r) => r.status === 200,
'response time < 500ms': (r) => r.timings.duration < 500,
});
});
sleep(0.5);
group('Items API', () => {
const resGetAll = http.get(`${targetUrl}/api/items`);
check(resGetAll, {
'status is 200': (r) => r.status === 200,
'response time < 500ms': (r) => r.timings.duration < 500,
'response contains items': (r) => Array.isArray(JSON.parse(r.body)),
});
const createPayload = { name: 'Test Item', description: 'A test item' };
const resCreate = http.post(`${targetUrl}/api/items`, JSON.stringify(createPayload), {
headers: { 'Content-Type': 'application/json' },
});
check(resCreate, {
'status is 201': (r) => r.status === 201,
'response time < 500ms': (r) => r.timings.duration < 500,
'response contains created item': (r) => JSON.parse(r.body).name === createPayload.name,
});
const itemId = JSON.parse(resCreate.body).id;
const resGetOne = http.get(`${targetUrl}/api/items/${itemId}`);
check(resGetOne, {
'status is 200': (r) => r.status === 200,
'response time < 500ms': (r) => r.timings.duration < 500,
'response contains correct item': (r) => JSON.parse(r.body).id === itemId,
});
const updatePayload = { name: 'Updated Item', description: 'Updated description' };
const resUpdate = http.put(`${targetUrl}/api/items/${itemId}`, JSON.stringify(updatePayload), {
headers: { 'Content-Type': 'application/json' },
});
check(resUpdate, {
'status is 200': (r) => r.status === 200,
'response time < 500ms': (r) => r.timings.duration < 500,
'response contains updated item': (r) => JSON.parse(r.body).name === updatePayload.name,
});
const resDelete = http.del(`${targetUrl}/api/items/${itemId}`);
check(resDelete, {
'status is 200': (r) => r.status === 200,
'response time < 500ms': (r) => r.timings.duration < 500,
});
});
sleep(0.5);
}