Introduction
Load testing verifies how an application behaves under heavy load. In 2026, these tests are essential for ensuring the reliability of APIs and websites during traffic spikes. k6, a modern open-source tool, makes these tests accessible thanks to its familiar JavaScript language. This tutorial teaches you how to install k6, write complete scripts, and interpret key metrics like response time and error rate. You will quickly obtain actionable results to improve your performance.
Prerequisites
- Node.js 18 or higher installed
- Basic knowledge of JavaScript
- A terminal (bash or PowerShell)
- A web application accessible via HTTP (e.g., local REST API)
Installing k6
# Linux / macOS
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys C5AD17C747E3415A
sudo sh -c 'echo "deb https://dl.k6.io/deb stable main" | tee /etc/apt/sources.list.d/k6.list'
sudo apt-get update
sudo apt-get install k6
# Vérification
k6 versionThis command installs k6 from the official repository. It adds the GPG key and repository to get the latest stable version. Verify the installation with k6 version before continuing.
Basic Test Script
import http from 'k6/http';
import { sleep } from 'k6';
export default function () {
const response = http.get('https://jsonplaceholder.typicode.com/posts/1');
console.log(`Status: ${response.status}`);
sleep(1);
}This script performs a simple GET request to a public API. The sleep function simulates a real user. Run it with k6 run load-test.js to test basic behavior.
Running the Test
k6 run load-test.jsRun the test script. k6 displays real-time metrics: requests per second, average response time, and errors. Press Ctrl+C to stop.
Adding Load Scenarios
import http from 'k6/http';
import { sleep } from 'k6';
export const options = {
stages: [
{ duration: '30s', target: 10 },
{ duration: '1m', target: 50 },
{ duration: '30s', target: 0 },
],
};
export default function () {
http.get('https://jsonplaceholder.typicode.com/posts/1');
sleep(1);
}The stages options define a gradual ramp-up to 50 virtual users. This simulates a realistic traffic spike and lets you observe application stability.
Adding Checks and Thresholds
import http from 'k6/http';
import { check, sleep } from 'k6';
export const options = {
thresholds: {
http_req_duration: ['p(95)<500'],
http_req_failed: ['rate<0.01'],
},
};
export default function () {
const res = http.get('https://jsonplaceholder.typicode.com/posts/1');
check(res, {
'status is 200': (r) => r.status === 200,
'response time OK': (r) => r.timings.duration < 500,
});
sleep(1);
}Thresholds stop the test if conditions are not met. Checks validate status and response time. This is essential for automating performance validation.
Best Practices
- Always define strict thresholds before running tests
- Use dynamic data to avoid caching
- Start with low loads and increase gradually
- Run tests from multiple geographic regions
- Integrate tests into your CI/CD pipeline
Common Mistakes to Avoid
- Forgetting to validate responses with checks
- Hardcoding URLs without environment variables
- Not simulating realistic pauses between requests
- Ignoring 4xx/5xx errors in reports
Going Further
Deepen your skills with our comprehensive performance testing courses. Discover the detailed program at https://learni-group.com/formations.