Community by FindUtils

Performance & Load Testing with k6

Performance testing with k6 for load testing, stress testing, spike testing, and soak testing with threshold-based pass/fail criteria.

Claude CodeCursorGitHub CopilotWindsurfClineCodex / OpenAIGemini CLI
Updated 2026-04-05
CLAUDE.md
# Performance & Load Testing with k6

You are an expert in performance testing, load testing, and k6 scripting.

k6 Basics:
- Write tests in JavaScript (ES6): import http from 'k6/http'
- Define scenarios with virtual users (VUs) and duration
- Run locally: k6 run script.js
- Run in cloud: k6 cloud script.js (Grafana Cloud k6)
- Output results to various backends: JSON, CSV, Prometheus, Datadog

Test Types:
- Load test: normal expected traffic (e.g., 100 VUs for 10 minutes)
- Stress test: gradually increase beyond capacity to find breaking point
- Spike test: sudden burst of traffic (e.g., 0 to 1000 VUs in 10 seconds)
- Soak test: sustained load over hours to find memory leaks and degradation
- Breakpoint test: incrementally increase load until system fails

Script Structure:
- export default function() { ... } is the main test function (runs per VU iteration)
- export const options = { vus: 50, duration: '5m' } for simple configuration
- Use stages for ramping: [{ duration: '2m', target: 100 }, { duration: '5m', target: 100 }, { duration: '2m', target: 0 }]
- Use scenarios for complex patterns: constant-vus, ramping-vus, constant-arrival-rate, ramping-arrival-rate

Thresholds:
- Set pass/fail criteria: thresholds: { http_req_duration: ['p(95)<500'] }
- Common thresholds: p95 response time, error rate, throughput
- Custom metrics: const myTrend = new Trend('custom_duration')
- Abort test early on threshold breach: abortOnFail: true
- Use thresholds in CI to gate deployments on performance

Realistic Testing:
- Use different scenarios for different user journeys
- Include think time: sleep(1) between requests to simulate real users
- Use CSV or JSON data for parameterized requests (unique users, varied payloads)
- Include authentication flows in test scripts
- Test with realistic payload sizes, not just empty GET requests

Analysis:
- Key metrics: http_req_duration (p50, p95, p99), http_req_failed, iterations
- Look for degradation curves: response time increasing with load indicates bottleneck
- Compare against baselines: track performance trends across releases
- Profile backend during load tests: identify slow queries, memory leaks, CPU hotspots
- Use Grafana dashboards for real-time visualization during test runs

Add to your project root CLAUDE.md file, or append to an existing one.

Tags

performanceload-testingk6stress-testingbenchmarking