What is K6?
K6 is a modern, developer-friendly load testing tool built for testing the performance of APIs, microservices, and websites. Write tests in JavaScript and run them from the CLI or CI/CD pipeline.
⚡ Key Features
- JavaScript-based test scripts
- Virtual users and load stages
- Performance thresholds (SLOs)
- Metrics and assertions
- Cloud and local execution
🎯 Use Cases
- API performance testing
- Load and stress testing
- Spike and soak testing
- CI/CD integration
- Performance regression detection
📥 K6 - Installation
Install K6 on different operating systems.
macOS
brew install k6
Linux (Debian / Ubuntu)
sudo apt update
sudo apt install k6
Windows
choco install k6
Verify installation:
k6 version
🚀 K6 - Basic Test Script
Minimal HTTP load test example.
import http from 'k6/http';
import { sleep } from 'k6';
export default function () {
http.get('https://test.k6.io');
sleep(1);
}
Run:
k6 run script.js
👥 K6 - Virtual Users & Duration
Control load using VUs and duration.
k6 run --vus 10 --duration 30s script.js
📊 K6 - Stages (Ramp Up / Down)
Simulate realistic traffic patterns.
export const options = {
stages: [
{ duration: '30s', target: 10 },
{ duration: '1m', target: 50 },
{ duration: '30s', target: 0 }
],
};
🎯 K6 - Thresholds (SLAs / SLOs)
Fail tests if performance criteria are breached.
export const options = {
thresholds: {
http_req_duration: ['p(95)<500'],
http_req_failed: ['rate<0.01'],
},
};
✅ K6 - Checks (Assertions)
Validate responses during load.
import { check } from 'k6';
check(res, {
'status is 200': (r) => r.status === 200,
'response time < 500ms': (r) => r.timings.duration < 500,
});
🌐 K6 - HTTP Methods
http.get(url);
http.post(url, payload, params);
http.put(url, payload);
http.del(url);
🔐 K6 - Headers & Authentication
const params = {
headers: {
'Authorization': 'Bearer TOKEN',
'Content-Type': 'application/json',
},
};
🔧 K6 - Environment Variables
k6 run -e BASE_URL=https://api.test.com script.js
const baseUrl = __ENV.BASE_URL;
📝 K6 - Data Parameterization
import data from './users.json';
export default function () {
const user = data[Math.floor(Math.random() * data.length)];
}
⚙️ K6 - Executors (Advanced)
export const options = {
scenarios: {
constant_load: {
executor: 'constant-vus',
vus: 20,
duration: '1m',
},
},
};
💪 K6 - Stress Testing
stages: [
{ duration: '1m', target: 50 },
{ duration: '1m', target: 200 },
{ duration: '30s', target: 0 },
]
⚡ K6 - Spike Testing
stages: [
{ duration: '10s', target: 200 },
{ duration: '1m', target: 0 },
]
⏱️ K6 - Soak Testing
{ duration: '2h', target: 50 }
📄 K6 - Output to JSON
k6 run --out json=results.json script.js
📊 K6 - InfluxDB Integration
k6 run --out influxdb=http://localhost:8086/k6 script.js
🔄 K6 - CI/CD Usage
k6 run script.js || exit 1
📈 K6 - Common Metrics
| Metric | Description |
|---|---|
| http_req_duration | Total request time |
| http_req_failed | Failed request rate |
| vus | Active virtual users |
| checks | Assertion results |
🐛 K6 - Debugging
k6 run --http-debug="full" script.js
✅ K6 - Best Practices Checklist
- Define clear SLAs
- Add thresholds
- Use realistic data
- Monitor backend metrics
- Fail CI on performance breach