-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathk6-spike-test-demo.js
More file actions
79 lines (65 loc) · 2.96 KB
/
k6-spike-test-demo.js
File metadata and controls
79 lines (65 loc) · 2.96 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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 (for our checks)
const failureRate = new Rate('check_failure_rate');
/*
Spike test example:
- Keep a small baseline of virtual users for a period
- Suddenly spike to a very large number of users for a short time
- Return quickly back to baseline and observe recovery
This demonstrates how the system behaves under a sudden surge of traffic.
*/
export const options = {
stages: [
// Baseline warm-up
{ duration: '10s', target: 10 }, // Ramp up to 10 users
{ duration: '30s', target: 10 }, // Steady baseline for 30 seconds
// Spike
{ duration: '5s', target: 500 }, // Rapid ramp up to 500 users (spike)
{ duration: '30s', target: 500 }, // Stay at spike load for 30 seconds
// Recovery back to baseline
{ duration: '20s', target: 10 }, // Rapid ramp down to baseline
{ duration: '30s', target: 10 }, // Let the system recover at baseline
// Cooldown to 0
{ duration: '30s', target: 0 }, // Ramp down to 0 users
],
// Thresholds tuned for spike (we allow some degradation during spike but keep limits reasonable)
thresholds: {
'http_req_duration': [
'p(95)<2000', // 95% of requests should be below 2s
'p(99)<5000', // 99% of requests should be below 5s during a big spike
],
'http_req_failed': ['rate<0.10'], // Allow up to 10% failure rate during the spike
'check_failure_rate': ['rate<0.10'], // Same threshold for our custom check
},
};
export default function () {
// Target URL - adjust if your API runs on a different host/port
const url = 'http://localhost:5127/mostborrowedbook';
// Add a tag so metrics in k6 can be filtered/grouped by endpoint
const response = http.get(url, { tags: { name: 'MostBorrowedBook' } });
// Basic checks to validate correctness and a soft latency guard
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 && r.body.length > 0,
});
// Record custom metric for later assertions
failureRate.add(!checks);
// Helpful debug log when things fail — keep minimal to avoid log spam during large spikes
if (!checks) {
console.log(`Check failed. status=${response.status} duration=${response.timings.duration}`);
}
// Short randomized sleep to avoid perfectly synchronized requests
sleep(Math.random() * 1 + 0.5); // 0.5 - 1.5s
}
// Produce an HTML report plus a console summary when the test finishes
export function handleSummary(data) {
return {
"spike-test-results.html": htmlReport(data),
stdout: textSummary(data, { indent: ' ', enableColors: true }),
};
}