

K6 Performance Testing for APIs – Quick Start Guide
Learn how to install, configure, and run K6 performance tests on REST APIs
Introduction#
Performance testing is no longer optional for modern APIs. Even a perfectly functional API can fail under load if performance is not validated early.
K6 is a modern, open-source performance testing tool designed for developers, QA engineers, and DevOps teams. It allows you to write readable performance tests using JavaScript and run them locally or in CI/CD pipelines.
What is K6 and When Should You Use It#
K6 is best suited for:
- REST and GraphQL API performance testing
- Load testing microservices
- Stress and spike testing
- CI/CD performance gates
- Pre-production validation
Core Performance Testing Concepts#
| Term | Meaning |
|---|---|
| Virtual User (VU) | Simulated user executing your test |
| Iteration | One full execution of the test function |
| Load Test | Expected production traffic |
| Stress Test | Gradually increasing load to find limits |
| Spike Test | Sudden traffic increase |
| Threshold | SLA or pass/fail condition |
Step 1: Install k6#
macOS#
brew install k6bashWindows#
choco install k6bashLinux (Debian / Ubuntu)#
sudo apt update
sudo apt install k6bashDocker (CI/CD Friendly)#
docker pull grafana/k6bashVerify installation:
k6 versionbashStep 3: Your First K6 API Test#
Create api-test.js:
import http from 'k6/http'
import { check, sleep } from 'k6'
export const options = {
vus: 10,
duration: '30s',
}
export default function () {
const response = http.get('https://jsonplaceholder.typicode.com/posts')
check(response, {
'status is 200': (r) => r.status === 200,
'response time < 500ms': (r) => r.timings.duration < 500,
})
sleep(1)
}javascriptWhat this test does#
- Runs 10 virtual users
- Executes for 30 seconds
- Sends HTTP GET requests
- Validates response status and performance
Step 4: Running the Test#
k6 run tests/api-test.jsbashK6 will immediately start printing live metrics to the terminal.
Step 5: Understanding K6 Output#
Key Metrics to Watch
| Metric | What It Tells You |
|---|---|
| http_req_duration | End-to-end request latency |
| http_req_failed | Percentage of failed requests |
| vus | Number of active virtual users |
| iterations | Total test executions |
| checks | Assertion success rate |
Focus First On
- ✅ Error rate (must be near zero)
- ✅ P95 / P99 latency
- ✅ Consistency under sustained load
Load Testing Example#
export const options = {
vus: 50,
duration: '2m',
}javascriptUse this to simulate normal expected traffic.
Stress Testing Example#
export const options = {
stages: [
{ duration: '1m', target: 20 },
{ duration: '1m', target: 50 },
{ duration: '1m', target: 100 },
{ duration: '1m', target: 0 },
],
}javascriptUse this to identify:
- Performance degradation
- Breaking points
- Resource bottlenecks
API Testing with Headers and Auth#
const params = {
headers: {
Authorization: 'Bearer YOUR_TOKEN',
'Content-Type': 'application/json',
},
}
http.get('https://api.example.com/users', params)javascriptStep 6: Adding Thresholds (SLAs)#
Thresholds fail the test when SLAs are violated.
export const options = {
thresholds: {
http_req_duration: ['p(95)<500'],
http_req_failed: ['rate<0.01'],
},
}javascriptPerfect for CI/CD pipelines.
CI/CD Usage (Quick Example)#
k6 run tests/api-test.js --summary-export=summary.jsonbashPipeline can fail if thresholds are breached.
Best Practices#
- Start small, scale gradually
- Keep tests deterministic
- Version control test scripts
- Avoid testing from unstable networks
- Always define thresholds
When NOT to Use K6#
- UI/browser testing
- Visual performance testing
- End-to-end user journey validation
Conclusion#
K6 enables teams to shift performance testing left and catch issues early.
With minimal setup and readable scripts, it becomes a powerful part of your DevTools ecosystem.
Key Takeaways#
- K6 is lightweight and developer-friendly
- Ideal for API and microservice testing
- Easy CI/CD integration
- Clear metrics and strong automation support