$npx -y skills add girijashankarj/cursor-handbook --skill load-test-generatorGenerate load test scripts using k6, Artillery, or Locust from API endpoints or OpenAPI specs. Use when the user asks to create load tests, stress tests, or performance benchmarks.
| 1 | # Skill: Load Test Generator |
| 2 | |
| 3 | Generate load test scripts for API endpoints with configurable scenarios, thresholds, and reporting. |
| 4 | |
| 5 | ## Trigger |
| 6 | When the user asks to create load tests, stress tests, performance benchmarks, or capacity tests. |
| 7 | |
| 8 | ## Prerequisites |
| 9 | - [ ] Target API endpoints identified |
| 10 | - [ ] Expected load profile known (requests/sec, concurrent users) |
| 11 | - [ ] Performance thresholds defined (latency, error rate) |
| 12 | |
| 13 | ## Steps |
| 14 | |
| 15 | ### Step 1: Choose Tool |
| 16 | |
| 17 | | Tool | Language | Best For | |
| 18 | |------|----------|----------| |
| 19 | | **k6** | JavaScript | Developer-friendly, CI integration, cloud scaling | |
| 20 | | **Artillery** | YAML + JS | Quick setup, YAML-based scenarios | |
| 21 | | **Locust** | Python | Python teams, distributed testing | |
| 22 | |
| 23 | Default to **k6** unless user specifies otherwise. |
| 24 | |
| 25 | ### Step 2: Identify Scenarios |
| 26 | |
| 27 | | Scenario Type | Purpose | Pattern | |
| 28 | |--------------|---------|---------| |
| 29 | | **Smoke** | Verify works under minimal load | 1–5 VUs, 1 min | |
| 30 | | **Load** | Normal expected traffic | Target RPS, 5–10 min | |
| 31 | | **Stress** | Find breaking point | Ramp to 2–5x normal, 10 min | |
| 32 | | **Spike** | Sudden traffic burst | 0 → max → 0 in seconds | |
| 33 | | **Soak** | Memory leaks, degradation | Normal load, 1–4 hours | |
| 34 | |
| 35 | ### Step 3: Define Test Configuration |
| 36 | |
| 37 | ```javascript |
| 38 | // k6 load test |
| 39 | export const options = { |
| 40 | scenarios: { |
| 41 | load_test: { |
| 42 | executor: 'ramping-vus', |
| 43 | startVUs: 0, |
| 44 | stages: [ |
| 45 | { duration: '2m', target: 50 }, // Ramp up |
| 46 | { duration: '5m', target: 50 }, // Steady state |
| 47 | { duration: '2m', target: 100 }, // Peak |
| 48 | { duration: '1m', target: 0 }, // Ramp down |
| 49 | ], |
| 50 | }, |
| 51 | }, |
| 52 | thresholds: { |
| 53 | http_req_duration: ['p(95)<500', 'p(99)<1000'], |
| 54 | http_req_failed: ['rate<0.01'], |
| 55 | http_reqs: ['rate>100'], |
| 56 | }, |
| 57 | }; |
| 58 | ``` |
| 59 | |
| 60 | ### Step 4: Generate Test Script (k6) |
| 61 | |
| 62 | ```javascript |
| 63 | import http from 'k6/http'; |
| 64 | import { check, sleep } from 'k6'; |
| 65 | import { Rate, Trend } from 'k6/metrics'; |
| 66 | |
| 67 | const BASE_URL = __ENV.BASE_URL || '[API_BASE_URL]'; |
| 68 | const AUTH_TOKEN = __ENV.AUTH_TOKEN || '[TEST_TOKEN]'; |
| 69 | |
| 70 | const errorRate = new Rate('errors'); |
| 71 | const latency = new Trend('request_latency'); |
| 72 | |
| 73 | const headers = { |
| 74 | 'Content-Type': 'application/json', |
| 75 | 'Authorization': `Bearer ${AUTH_TOKEN}`, |
| 76 | }; |
| 77 | |
| 78 | export default function () { |
| 79 | // Scenario: List resources |
| 80 | const listRes = http.get(`${BASE_URL}/api/v1/resources`, { headers }); |
| 81 | check(listRes, { |
| 82 | 'list returns 200': (r) => r.status === 200, |
| 83 | 'list latency < 500ms': (r) => r.timings.duration < 500, |
| 84 | 'list returns array': (r) => JSON.parse(r.body).data.length >= 0, |
| 85 | }); |
| 86 | errorRate.add(listRes.status !== 200); |
| 87 | latency.add(listRes.timings.duration); |
| 88 | |
| 89 | sleep(1); |
| 90 | |
| 91 | // Scenario: Create resource |
| 92 | const payload = JSON.stringify({ |
| 93 | name: `load-test-${Date.now()}`, |
| 94 | type: 'test', |
| 95 | }); |
| 96 | const createRes = http.post(`${BASE_URL}/api/v1/resources`, payload, { headers }); |
| 97 | check(createRes, { |
| 98 | 'create returns 201': (r) => r.status === 201, |
| 99 | 'create latency < 1000ms': (r) => r.timings.duration < 1000, |
| 100 | }); |
| 101 | errorRate.add(createRes.status !== 201); |
| 102 | |
| 103 | sleep(1); |
| 104 | } |
| 105 | ``` |
| 106 | |
| 107 | ### Step 5: Add Data-Driven Testing |
| 108 | |
| 109 | ```javascript |
| 110 | import { SharedArray } from 'k6/data'; |
| 111 | import papaparse from 'https://jslib.k6.io/papaparse/5.1.1/index.js'; |
| 112 | |
| 113 | const testData = new SharedArray('users', function () { |
| 114 | return JSON.parse(open('./test-data.json')); |
| 115 | }); |
| 116 | |
| 117 | export default function () { |
| 118 | const user = testData[__VU % testData.length]; |
| 119 | // Use user.id, user.email, etc. in requests |
| 120 | } |
| 121 | ``` |
| 122 | |
| 123 | ### Step 6: Add Cleanup (Optional) |
| 124 | ```javascript |
| 125 | export function teardown(data) { |
| 126 | // Clean up test data created during the load test |
| 127 | http.del(`${BASE_URL}/api/v1/test-data/cleanup`, null, { headers }); |
| 128 | } |
| 129 | ``` |
| 130 | |
| 131 | ### Step 7: Create Run Scripts |
| 132 | ```bash |
| 133 | # Smoke test |
| 134 | k6 run --vus 5 --duration 1m load-test.js |
| 135 | |
| 136 | # Load test |
| 137 | k6 run load-test.js |
| 138 | |
| 139 | # Stress test (override VUs) |
| 140 | k6 run --stage 2m:200,5m:200,2m:0 load-test.js |
| 141 | |
| 142 | # With environment variables |
| 143 | k6 run -e BASE_URL=https://staging.example.com -e AUTH_TOKEN=$TOKEN load-test.js |
| 144 | ``` |
| 145 | |
| 146 | ### Step 8: Document Thresholds |
| 147 | - [ ] p95 latency target: `<500ms` |
| 148 | - [ ] p99 latency target: `<1000ms` |
| 149 | - [ ] Error rate target: `<1%` |
| 150 | - [ ] Throughput target: `>100 RPS` |
| 151 | - [ ] Document what "pass" and "fail" mean for each threshold |
| 152 | |
| 153 | ## Rules |
| 154 | - **NEVER** run load tests against production without explicit approval |
| 155 | - **NEVER** hardcode credentials — use environment variables |
| 156 | - **ALWAYS** include think time (`sleep`) between requests to simulate real users |
| 157 | - **ALWAYS** include `check` assertions for response validation |
| 158 | - **ALWAYS** define thresholds (the test should pass/fail based on performance) |
| 159 | - Use test-specific API tokens, not production credentials |
| 160 | - Clean up test data after runs |
| 161 | - Start with smoke tests before running full load tests |