Krishan Chawla

Back

K6 Performance Testing for APIs – Quick Start GuideBlur image

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#

TermMeaning
Virtual User (VU)Simulated user executing your test
IterationOne full execution of the test function
Load TestExpected production traffic
Stress TestGradually increasing load to find limits
Spike TestSudden traffic increase
ThresholdSLA or pass/fail condition

Step 1: Install k6#

macOS#

brew install k6
bash

Windows#

choco install k6
bash

Linux (Debian / Ubuntu)#

sudo apt update
sudo apt install k6
bash

Docker (CI/CD Friendly)#

docker pull grafana/k6
bash

Verify installation:

k6 version
bash

Step 3: Your First K6 API Test#

Create api-test.js:

What 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.js
bash

K6 will immediately start printing live metrics to the terminal.

Step 5: Understanding K6 Output#

Key Metrics to Watch

MetricWhat It Tells You
http_req_durationEnd-to-end request latency
http_req_failedPercentage of failed requests
vusNumber of active virtual users
iterationsTotal test executions
checksAssertion 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',
}
javascript

Use 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 },
  ],
}
javascript

Use 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)
javascript

Step 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'],
  },
}
javascript

Perfect for CI/CD pipelines.

CI/CD Usage (Quick Example)#

k6 run tests/api-test.js --summary-export=summary.json
bash

Pipeline 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