-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathk6-stress-test-demo.js
More file actions
62 lines (53 loc) · 2.11 KB
/
k6-stress-test-demo.js
File metadata and controls
62 lines (53 loc) · 2.11 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import http from 'k6/http';
import { check, sleep } from 'k6';
import { Rate } from 'k6/metrics';
import { htmlReport } from "https://raw.githubusercontent.com/benc-uk/k6-reporter/main/dist/bundle.js";
import { textSummary } from "https://jslib.k6.io/k6-summary/0.0.1/index.js";
// Custom metric to track failure rate
const failureRate = new Rate('check_failure_rate');
export const options = {
// Stress test configuration with stages
stages: [
{ duration: '30s', target: 25 }, // Ramp up to 25 users
{ duration: '30s', target: 50 }, // Ramp up to 50 users
{ duration: '30s', target: 100 }, // Ramp up to 100 users
{ duration: '30s', target: 200 }, // Peak at 200 users
{ duration: '30s', target: 0 }, // Ramp down to 0 users (recovery)
],
thresholds: {
// We set higher thresholds for stress test as we expect degradation
'http_req_duration': [
'p(95)<2000', // 95% of requests should be below 2s
'p(99)<3000', // 99% of requests should be below 3s
],
'http_req_failed': ['rate<0.15'], // Allow up to 15% failure rate during stress
'check_failure_rate': ['rate<0.15'], // Same threshold for our custom check
},
};
export default function () {
const url = 'http://localhost:5127/mostborrowedbook';
// Send GET request with custom tags for better metrics
const response = http.get(url, {
tags: { name: 'MostBorrowedBook' },
});
// Define checks
const checks = check(response, {
'status is 200': (r) => r.status === 200,
'response time < 2000ms': (r) => r.timings.duration < 2000,
'valid response body': (r) => r.body.length > 0,
});
// Update custom metric
failureRate.add(!checks);
// Log if we see a non-200 response
if (response.status !== 200) {
console.log(`Got non-200 response: ${response.status}, body: ${response.body}`);
}
// Random sleep between 1s and 3s to add variability
sleep(Math.random() * 2 + 1); // Random sleep between 1-3s
}
export function handleSummary(data) {
return {
"stress-test-results.html": htmlReport(data),
stdout: textSummary(data, { indent: " ", enableColors: true }),
};
}