Testingadvanced
Generate load testing scripts (k6, Artillery, or JMeter)
Load Test Script
Generate load testing scripts (k6, Artillery, or JMeter)
Generate load testing scripts for performance testing.
Instructions
- Using k6 (recommended):
import http from 'k6/http';
import { check, sleep } from 'k6';
export const options = {
stages: [
{ duration: '30s', target: 20 }, // ramp up to 20 users
{ duration: '1m', target: 20 }, // hold at 20 users
{ duration: '30s', target: 100 }, // spike to 100
{ duration: '1m', target: 100 }, // hold at 100
{ duration: '30s', target: 0 }, // ramp down
],
thresholds: {
http_req_duration: ['p(95)<500'], // 95% of requests < 500ms
http_req_failed: ['rate<0.01'], // < 1% failure rate
},
};
export default function () {
const res = http.get('https://api.example.com/users');
check(res, {
'status is 200': (r) => r.status === 200,
'response time < 500ms': (r) => r.timings.duration < 500,
});
sleep(1); // think time between requests
}
- Run:
k6 run load-test.js
- Key metrics to watch:
- p95 response time: should be < your SLA
- Error rate: should be < 1%
- Throughput (req/s): matches expected peak traffic
Rules
- Test against a staging environment, never production
- Include think time (sleep) to simulate real users
- Test with realistic payloads, not just GETs