feat(k6): add bespoke load test files
All checks were successful
Build and Push to ACR / Build and Push (push) Successful in 4m59s
All checks were successful
Build and Push to ACR / Build and Push (push) Successful in 4m59s
This commit is contained in:
87
k6/load-test.js
Normal file
87
k6/load-test.js
Normal file
@@ -0,0 +1,87 @@
|
||||
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-nest2.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 resCreate = http.post(
|
||||
`${targetUrl}/api/items`,
|
||||
JSON.stringify({ name: 'Test Item', description: 'A test description' }),
|
||||
{ headers: { 'Content-Type': 'application/json' } }
|
||||
);
|
||||
check(resCreate, {
|
||||
'status is 201': (r) => r.status === 201,
|
||||
'response time < 500ms': (r) => r.timings.duration < 500,
|
||||
'response contains id': (r) => JSON.parse(r.body).id !== undefined,
|
||||
});
|
||||
|
||||
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 resUpdate = http.put(
|
||||
`${targetUrl}/api/items/${itemId}`,
|
||||
JSON.stringify({ name: 'Updated Item', description: 'Updated description' }),
|
||||
{ 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 name': (r) => JSON.parse(r.body).name === 'Updated Item',
|
||||
});
|
||||
|
||||
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,
|
||||
'response confirms deletion': (r) => JSON.parse(r.body).deleted === itemId,
|
||||
});
|
||||
});
|
||||
|
||||
sleep(0.5);
|
||||
}
|
||||
Reference in New Issue
Block a user