Introduction
Load testing is essential to ensure your applications stay performant under pressure. In 2026, modern tools like k6 let you write maintainable scripts and obtain precise metrics quickly. This intermediate tutorial teaches you how to create realistic scenarios, define strict thresholds, and integrate tests into your CI pipeline. You will discover how to simulate virtual users, measure response times, and detect bottlenecks before they impact real users.
Prerequisites
- Node.js 20+ and npm
- Basic knowledge of JavaScript/TypeScript
- A REST API to test (e.g. localhost:3000)
- k6 installed locally
Installing k6
curl -L https://github.com/grafana/k6/releases/download/v0.57.0/k6-v0.57.0-linux-amd64.tar.gz | tar xvz
sudo mv k6 /usr/local/bin/
k6 versionThis command downloads and installs the latest stable version of k6. Always verify the version to benefit from the latest optimizations and security fixes.
Basic Test Script
import http from 'k6/http';
import { sleep, check } from 'k6';
export default function () {
const res = http.get('http://localhost:3000/api/users');
check(res, {
'status is 200': (r) => r.status === 200,
'response time < 200ms': (r) => r.timings.duration < 200,
});
sleep(1);
}This script sends a GET request and checks the status and response time. Checks allow the test to fail if criteria are not met.
Configuring Options
import http from 'k6/http';
import { check, sleep } from 'k6';
export const options = {
stages: [
{ duration: '30s', target: 20 },
{ duration: '1m', target: 50 },
{ duration: '30s', target: 0 },
],
thresholds: {
http_req_duration: ['p(95)<250'],
http_req_failed: ['rate<0.01'],
},
};
export default function () {
const res = http.get('http://localhost:3000/api/users');
check(res, { 'status 200': (r) => r.status === 200 });
sleep(1);
}Stages define progressive load. Thresholds automatically fail the test if performance targets are not met.
Running the Test
k6 run --out json=results.json load-test.jsThis command runs the test and exports results in JSON format for later analysis or dashboard integration.
Multi-Endpoint Scenario
import http from 'k6/http';
import { sleep } from 'k6';
export default function () {
http.get('http://localhost:3000/api/users');
sleep(0.5);
http.post('http://localhost:3000/api/users', JSON.stringify({ name: 'Test' }), {
headers: { 'Content-Type': 'application/json' },
});
sleep(1);
}This script simulates a realistic user journey by chaining multiple requests. Adapt the endpoints to match your real application.
Best Practices
- Always define strict thresholds on p(95) and error rate
- Use dynamic data to avoid caching
- Run tests in an environment close to production
- Version test scripts like application code
- Analyze results with tools like Grafana
Common Mistakes to Avoid
- Forgetting to configure thresholds, which makes tests useless
- Using fixed IPs that trigger rate limiting
- Ignoring network errors in checks
- Not cleaning up test data after execution
Going Further
Integrate k6 into GitHub Actions and explore extensions for distributed testing. Check out our training programs to deepen your performance testing skills.