feat(k6): add bespoke load test files
All checks were successful
Build and Push to ACR / Build and Push (push) Successful in 4m49s

This commit is contained in:
2026-05-11 13:00:39 +00:00
parent e870170da3
commit 197de4e6b9
3 changed files with 161 additions and 0 deletions

85
k6/load-test.js Normal file
View File

@@ -0,0 +1,85 @@
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-nest3.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 res1 = http.get(`${targetUrl}/api/items`);
check(res1, {
'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 createBody = { name: 'Test Item', description: 'A test description' };
const res2 = http.post(`${targetUrl}/api/items`, JSON.stringify(createBody), {
headers: { 'Content-Type': 'application/json' },
});
check(res2, {
'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 === createBody.name,
});
const itemId = JSON.parse(res2.body).id;
const res3 = http.get(`${targetUrl}/api/items/${itemId}`);
check(res3, {
'status is 200': (r) => r.status === 200,
'response time < 500ms': (r) => r.timings.duration < 500,
'response contains item': (r) => JSON.parse(r.body).id === itemId,
});
const updateBody = { name: 'Updated Item', description: 'Updated description' };
const res4 = http.put(`${targetUrl}/api/items/${itemId}`, JSON.stringify(updateBody), {
headers: { 'Content-Type': 'application/json' },
});
check(res4, {
'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 === updateBody.name,
});
const res5 = http.del(`${targetUrl}/api/items/${itemId}`);
check(res5, {
'status is 200': (r) => r.status === 200,
'response time < 500ms': (r) => r.timings.duration < 500,
'response confirms deletion': (r) => JSON.parse(r.body).deleted === itemId,
});
});
sleep(0.5);
}