# curl https://devtools.krishanchawla.com/raw/k6-load-testing-cheatsheet.txt K6 LOAD TESTING CHEATSHEET Enterprise-ready K6 performance testing cheatsheet with commands, scripts, thresholds, and CI usage Category: Testing · Type: cheatsheet Official docs: https://k6.io/ ──────────────────────────────────────────────────────────── ## 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 --- ## 🛠️ Interactive Command Builder Build a `k6 run` command without memorizing flag order — fill in the fields below and copy the result. ## 📥 K6 - Installation Install K6 on different operating systems. ### macOS ```bash brew install k6 ``` ### Linux (Debian / Ubuntu) ```bash sudo apt update sudo apt install k6 ``` ### Windows ```bash choco install k6 ``` Verify installation: ```bash k6 version ``` --- ## 🚀 K6 - Basic Test Script Minimal HTTP load test example. ```javascript import http from 'k6/http'; import { sleep } from 'k6'; export default function () { http.get('https://test.k6.io'); sleep(1); } ``` Run: ```bash k6 run script.js ``` --- ## 👥 K6 - Virtual Users & Duration Control load using VUs and duration. ```bash k6 run --vus 10 --duration 30s script.js ``` --- ## 📊 K6 - Stages (Ramp Up / Down) Simulate realistic traffic patterns. ```javascript 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. ```javascript export const options = { thresholds: { http_req_duration: ['p(95)<500'], http_req_failed: ['rate<0.01'], }, }; ``` --- ## ✅ K6 - Checks (Assertions) Validate responses during load. ```javascript import { check } from 'k6'; check(res, { 'status is 200': (r) => r.status === 200, 'response time < 500ms': (r) => r.timings.duration < 500, }); ``` --- ## 🌐 K6 - HTTP Methods ```javascript http.get(url); http.post(url, payload, params); http.put(url, payload); http.del(url); ``` --- ## 🔐 K6 - Headers & Authentication ```javascript const params = { headers: { 'Authorization': 'Bearer TOKEN', 'Content-Type': 'application/json', }, }; ``` --- ## 🔧 K6 - Environment Variables ```bash k6 run -e BASE_URL=https://api.test.com script.js ``` ```javascript const baseUrl = __ENV.BASE_URL; ``` --- ## 📝 K6 - Data Parameterization ```javascript import data from './users.json'; export default function () { const user = data[Math.floor(Math.random() * data.length)]; } ``` --- ## ⚙️ K6 - Executors (Advanced) ```javascript export const options = { scenarios: { constant_load: { executor: 'constant-vus', vus: 20, duration: '1m', }, }, }; ``` --- ## 💪 K6 - Stress Testing ```javascript stages: [ { duration: '1m', target: 50 }, { duration: '1m', target: 200 }, { duration: '30s', target: 0 }, ] ``` --- ## ⚡ K6 - Spike Testing ```javascript stages: [ { duration: '10s', target: 200 }, { duration: '1m', target: 0 }, ] ``` --- ## ⏱️ K6 - Soak Testing ```javascript { duration: '2h', target: 50 } ``` --- ## 📄 K6 - Output to JSON ```bash k6 run --out json=results.json script.js ``` --- ## 📊 K6 - InfluxDB Integration ```bash k6 run --out influxdb=http://localhost:8086/k6 script.js ``` --- ## 🔄 K6 - CI/CD Usage ```bash 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 ```bash 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 --- ──────────────────────────────────────────────────────────── Full page: https://devtools.krishanchawla.com/devtools/k6-load-testing-cheatsheet More tools: https://devtools.krishanchawla.com