Performance issues in production are expensive. k6 makes it easy to find them before they impact users.
Why k6?
k6 stands out for several reasons:
- Write tests in JavaScript
- Low resource footprint
- Excellent metrics and reporting
- Easy CI/CD integration
Your First Load Test
import http from 'k6/http';
import { check, sleep } from 'k6';
export const options = {
stages: [
{ duration: '1m', target: 50 },
{ duration: '3m', target: 50 },
{ duration: '1m', target: 0 },
],
};
export default function() {
const res = http.get('https://api.example.com/products');
check(res, { 'status is 200': (r) => r.status === 200 });
sleep(1);
}
Understanding Metrics
Key metrics to watch:
- http_req_duration: Response time
- http_req_failed: Error rate
- vus: Virtual users
- iterations: Completed test cycles
Setting Thresholds
Fail tests when performance degrades:
export const options = {
thresholds: {
'http_req_duration': ['p95<500'],
'http_req_failed': ['rate<0.01'],
},
};
The best time to find performance issues is in testing, not from angry customer tickets.