🧪 Performance Testing with K6: A Complete Guide with Examples Link to heading
📌 Introduction Link to heading
When building modern web applications, ensuring they can handle a high load of users is essential. K6 is a developer-centric open-source load testing tool built for making performance testing simple, efficient, and fun.
K6 enables developers and QA engineers to write load tests using JavaScript, execute them locally or in the cloud, and gain insights into performance bottlenecks before they impact real users.
🚀 Why Use K6? Link to heading
- 📜 Written in JavaScript
- 🧪 Load, stress, and soak testing
- 🧰 CLI-based tool, easy to integrate in CI/CD pipelines
- 📊 Rich output for metrics
- ☁️ Cloud execution with k6 Cloud
🔍 How K6 Differs from Other Tools Link to heading
K6 offers a unique approach to performance testing compared to traditional tools like JMeter, Gatling, or Locust. Here’s a breakdown:
| Feature | K6 | JMeter | Gatling | Locust |
|---|---|---|---|---|
| Scripting Language | JavaScript | XML-based UI or Groovy | Scala | Python |
| Ease of Use | Simple JS scripts | GUI-based, can be complex | Requires Scala knowledge | Python is beginner-friendly |
| CLI Support | ✅ Strong | ✅ Moderate | ✅ Strong | ✅ Strong |
| CI/CD Integration | ✅ Native support | ⚠️ Possible with config | ✅ Supported | ✅ Supported |
| Performance | High, event-loop based | Lower due to Java threads | High (Scala, async) | Moderate (Python threads) |
| Visual Results | Basic CLI + k6 Cloud | GUI, Plugins | Web-based dashboard | Web UI |
| Distributed Tests | ✅ with k6 Cloud or OSS tools | ✅ via plugins | ✅ | ✅ |
Summary:
- K6 is ideal for developers who prefer coding tests directly.
- JMeter is better suited for testers who prefer a GUI.
- Gatling and Locust are also code-based but require Scala or Python, respectively.
K6 is especially strong for:
- Modern DevOps practices
- Script versioning in Git
- Integrations with Grafana for dashboards
🛠️ Installation Link to heading
Install using Homebrew (macOS):
brew install k6
Or using Chocolatey (Windows):
choco install k6
Or via Docker:
docker run -i loadimpact/k6 run - <script.js
📄 Basic Example Link to heading
Simple Load Test Script (script.js)
Link to heading
import http from 'k6/http';
import { sleep } from 'k6';
export const options = {
vus: 10, // virtual users
duration: '30s', // test duration
};
export default function () {
http.get('https://test.k6.io');
sleep(1);
}
Run the test Link to heading
k6 run script.js
📦 Project Structure Example Link to heading
A typical K6 test project might look like this:
k6-load-tests/
├── tests/
│ ├── homepage.test.js
│ └── login.test.js
├── data/
│ └── users.csv
├── utils/
│ └── auth.js
└── k6.config.js
👨💻 Sample Advanced Script Link to heading
Login Simulation with Data Link to heading
data/users.csv
username,password
testuser1,pass1
testuser2,pass2
tests/login.test.js
import http from 'k6/http';
import { check, sleep } from 'k6';
import { SharedArray } from 'k6/data';
const users = new SharedArray('users', function () {
return open('../data/users.csv').split('\n').slice(1).map(row => {
const [username, password] = row.split(',');
return { username, password };
});
});
export const options = {
vus: 5,
duration: '20s',
};
export default function () {
const user = users[Math.floor(Math.random() * users.length)];
const payload = JSON.stringify({
username: user.username,
password: user.password,
});
const headers = { 'Content-Type': 'application/json' };
const res = http.post('https://test-api.k6.io/auth/token/', payload, { headers });
check(res, {
'login success': (r) => r.status === 200,
});
sleep(1);
}
🧪 Test Types Link to heading
- Smoke Test: Run with 1–2 users to validate script
- Load Test: Simulate normal traffic
- Stress Test: Increase load until the system fails
- Spike Test: Sudden increase in users
- Soak Test: Long-duration test
🔗 Integration with CI/CD Link to heading
K6 can be easily integrated with tools like GitHub Actions:
jobs:
load-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install k6
run: sudo apt install k6 -y
- name: Run Load Test
run: k6 run tests/homepage.test.js
📈 Viewing Results Link to heading
- CLI metrics summary
- JSON or CSV output:
k6 run --out json=output.json script.js - Use k6 Cloud for dashboard
📚 Resources Link to heading
🎯 Conclusion Link to heading
K6 provides a fast, powerful, and developer-friendly way to ensure your system’s performance and resilience. With JavaScript scripting and seamless integrations, it becomes a must-have tool in your performance testing toolkit.
Happy Testing! 🚀