-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathk6-rest-test.js
More file actions
39 lines (33 loc) · 1.05 KB
/
k6-rest-test.js
File metadata and controls
39 lines (33 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import http from 'k6/http';
import { check, sleep } from 'k6';
import { Rate } from 'k6/metrics';
// Custom metrics
export const errorRate = new Rate('errors');
export const options = {
stages: [
{ duration: '20s', target: 5000 },
],
thresholds: {
http_req_duration: ['p(95)<1000'], // 95% of requests must complete within 1s
http_req_failed: ['rate<0.01'], // Error rate must be less than 1%
errors: ['rate<0.01'],
},
};
export default function () {
const response = http.get('http://localhost:3000/');
const success = check(response, {
'status is 200': (r) => r.status === 200,
'response body contains data': (r) => {
try {
const data = JSON.parse(r.body);
return Array.isArray(data) && data.length === 500;
} catch (e) {
return false;
}
},
'response time < 500ms': (r) => r.timings.duration < 500,
});
errorRate.add(!success);
// Small pause between requests
sleep(0.5);
}