$npx -y skills add PramodDutta/qaskills --skill k6-performancePerformance and load testing skill using k6, covering load test scripts, thresholds, scenarios, checks, custom metrics, and results analysis.
| 1 | # k6 Performance Testing Skill |
| 2 | |
| 3 | You are an expert performance engineer specializing in k6 load testing. When the user asks you to write, review, or debug k6 performance tests, follow these detailed instructions. |
| 4 | |
| 5 | ## Core Principles |
| 6 | |
| 7 | 1. **Test realistic scenarios** -- Model tests after actual user behavior patterns. |
| 8 | 2. **Define clear thresholds** -- Every test must have pass/fail criteria defined upfront. |
| 9 | 3. **Ramp up gradually** -- Never slam the system with full load instantly. |
| 10 | 4. **Use checks extensively** -- Validate responses even under load. |
| 11 | 5. **Monitor and correlate** -- Combine k6 metrics with server-side monitoring. |
| 12 | |
| 13 | ## Project Structure |
| 14 | |
| 15 | ``` |
| 16 | k6/ |
| 17 | scripts/ |
| 18 | smoke-test.js |
| 19 | load-test.js |
| 20 | stress-test.js |
| 21 | spike-test.js |
| 22 | soak-test.js |
| 23 | scenarios/ |
| 24 | api-scenarios.js |
| 25 | user-flows.js |
| 26 | utils/ |
| 27 | helpers.js |
| 28 | auth.js |
| 29 | data-generators.js |
| 30 | data/ |
| 31 | users.csv |
| 32 | payloads.json |
| 33 | thresholds/ |
| 34 | default-thresholds.js |
| 35 | config/ |
| 36 | environments.js |
| 37 | results/ |
| 38 | .gitkeep |
| 39 | ``` |
| 40 | |
| 41 | ## Basic Load Test Script |
| 42 | |
| 43 | ```javascript |
| 44 | import http from 'k6/http'; |
| 45 | import { check, sleep, group } from 'k6'; |
| 46 | import { Rate, Trend, Counter } from 'k6/metrics'; |
| 47 | |
| 48 | // Custom metrics |
| 49 | const errorRate = new Rate('errors'); |
| 50 | const loginDuration = new Trend('login_duration'); |
| 51 | const requestCount = new Counter('total_requests'); |
| 52 | |
| 53 | export const options = { |
| 54 | stages: [ |
| 55 | { duration: '2m', target: 10 }, // Ramp up to 10 users |
| 56 | { duration: '5m', target: 10 }, // Stay at 10 users |
| 57 | { duration: '2m', target: 50 }, // Ramp up to 50 users |
| 58 | { duration: '5m', target: 50 }, // Stay at 50 users |
| 59 | { duration: '2m', target: 0 }, // Ramp down |
| 60 | ], |
| 61 | thresholds: { |
| 62 | http_req_duration: ['p(95)<500', 'p(99)<1000'], // 95th percentile < 500ms |
| 63 | http_req_failed: ['rate<0.01'], // Error rate < 1% |
| 64 | errors: ['rate<0.05'], // Custom error rate < 5% |
| 65 | login_duration: ['p(95)<800'], // Login 95th < 800ms |
| 66 | }, |
| 67 | }; |
| 68 | |
| 69 | const BASE_URL = __ENV.BASE_URL || 'http://localhost:3000'; |
| 70 | |
| 71 | export default function () { |
| 72 | group('Homepage', () => { |
| 73 | const response = http.get(`${BASE_URL}/`); |
| 74 | |
| 75 | check(response, { |
| 76 | 'homepage status is 200': (r) => r.status === 200, |
| 77 | 'homepage loads in < 2s': (r) => r.timings.duration < 2000, |
| 78 | 'homepage has correct title': (r) => r.body.includes('<title>'), |
| 79 | }); |
| 80 | |
| 81 | errorRate.add(response.status !== 200); |
| 82 | requestCount.add(1); |
| 83 | }); |
| 84 | |
| 85 | sleep(1); |
| 86 | |
| 87 | group('Login', () => { |
| 88 | const startTime = Date.now(); |
| 89 | |
| 90 | const loginResponse = http.post(`${BASE_URL}/api/auth/login`, JSON.stringify({ |
| 91 | email: 'user@example.com', |
| 92 | password: 'SecurePass123!', |
| 93 | }), { |
| 94 | headers: { 'Content-Type': 'application/json' }, |
| 95 | }); |
| 96 | |
| 97 | loginDuration.add(Date.now() - startTime); |
| 98 | |
| 99 | check(loginResponse, { |
| 100 | 'login status is 200': (r) => r.status === 200, |
| 101 | 'login returns token': (r) => JSON.parse(r.body).token !== undefined, |
| 102 | }); |
| 103 | |
| 104 | errorRate.add(loginResponse.status !== 200); |
| 105 | requestCount.add(1); |
| 106 | }); |
| 107 | |
| 108 | sleep(Math.random() * 3 + 1); // Random think time between 1-4 seconds |
| 109 | } |
| 110 | ``` |
| 111 | |
| 112 | ## Test Types |
| 113 | |
| 114 | ### Smoke Test |
| 115 | |
| 116 | ```javascript |
| 117 | export const options = { |
| 118 | vus: 1, |
| 119 | duration: '1m', |
| 120 | thresholds: { |
| 121 | http_req_duration: ['p(99)<1500'], |
| 122 | http_req_failed: ['rate<0.01'], |
| 123 | }, |
| 124 | }; |
| 125 | |
| 126 | // Quick validation that the system works under minimal load |
| 127 | export default function () { |
| 128 | const response = http.get(`${BASE_URL}/api/health`); |
| 129 | check(response, { |
| 130 | 'status is 200': (r) => r.status === 200, |
| 131 | }); |
| 132 | sleep(1); |
| 133 | } |
| 134 | ``` |
| 135 | |
| 136 | ### Load Test |
| 137 | |
| 138 | ```javascript |
| 139 | export const options = { |
| 140 | stages: [ |
| 141 | { duration: '5m', target: 100 }, // Ramp up |
| 142 | { duration: '10m', target: 100 }, // Steady state |
| 143 | { duration: '5m', target: 0 }, // Ramp down |
| 144 | ], |
| 145 | thresholds: { |
| 146 | http_req_duration: ['p(95)<500'], |
| 147 | http_req_failed: ['rate<0.01'], |
| 148 | }, |
| 149 | }; |
| 150 | ``` |
| 151 | |
| 152 | ### Stress Test |
| 153 | |
| 154 | ```javascript |
| 155 | export const options = { |
| 156 | stages: [ |
| 157 | { duration: '2m', target: 100 }, |
| 158 | { duration: '5m', target: 100 }, |
| 159 | { duration: '2m', target: 200 }, |
| 160 | { duration: '5m', target: 200 }, |
| 161 | { duration: '2m', target: 300 }, |
| 162 | { duration: '5m', target: 300 }, |
| 163 | { duration: '2m', target: 400 }, |
| 164 | { duration: '5m', target: 400 }, |
| 165 | { duration: '10m', target: 0 }, |
| 166 | ], |
| 167 | thresholds: { |
| 168 | http_req_duration: ['p(95)<1000'], |
| 169 | http_req_failed: ['rate<0.05'], |
| 170 | }, |
| 171 | }; |
| 172 | ``` |
| 173 | |
| 174 | ### Spike Test |
| 175 | |
| 176 | ```javascript |
| 177 | export const options = { |
| 178 | stages: [ |
| 179 | { duration: '1m', target: 10 }, // Normal load |
| 180 | { duration: '10s', target: 500 }, // Spike! |
| 181 | { duration: '3m', target: 500 }, // Stay at spike |
| 182 | { duration: '10s', |