Welcome back! In our first episode, we learned how to install k6 and run a basic load test with a single virtual user. It was a great start, but real-world traffic is rarely that simple.
In this second episode, we’ll level up our load testing skills. We'll simulate a crowd of users, mimic traffic spikes using stages, and finally, set some rules (thresholds) to ensure our system doesn’t just survive the load, but performs well under it. Let's dive in!
Testing with one user is good for checking if your script works. To really load test an application, you need more virtual users (VUs) and a longer duration.
You can still use the exact same script from Episode 1. Instead of changing the code, we just change the command we run:
k6 run --vus 50 --duration 30s test.jsThis command tells k6 to simulate 50 concurrent users constantly hammering the API for 30 seconds. Watch the console output to see how your server handles the increased pressure.
Real traffic naturally ramps up and down. A sudden spike from 0 to 50 users isn't always realistic. k6 lets us configure 'stages' to slowly scale traffic over time.
Let's update our `test.js` script to include some options:
import http from 'k6/http';
import { check, sleep } from 'k6';
export const options = {
stages: [
{ duration: '30s', target: 20 }, // ramp up to 20 users
{ duration: '1m', target: 20 }, // stay at 20 users for 1 minute
{ duration: '10s', target: 0 }, // ramp down to 0 users
],
};
export default function () {
const res = http.get('https://test-api.k6.io/');
check(res, { 'status is 200': (r) => r.status === 200 });
sleep(1); // wait 1 second before the next iteration
}
Now you just run `k6 run test.js`. The script will gradually scale up, hold the load, and safely scale down, mimicking a real burst of traffic.
What good is a load test if we don't know what 'success' looks like? A server returning 200 OKs but taking 5 seconds to respond is still a failing system.
We use **Thresholds** in k6 to define our performance expectations.
export const options = {
thresholds: {
// 95% of requests must complete below 500ms
http_req_duration: ['p(95)<500'],
// Less than 1% of requests can fail
http_req_failed: ['rate<0.01'],
},
// ... (stages can go here too!)
};
If any of these thresholds are breached during the test, k6 will finish the test with a non-zero exit code. This is perfect for integrating into CI/CD pipelines to automatically fail a bad build.
You've now moved from a beginner script to realistic, production-ready load testing configurations. By combining VUs, stages, and thresholds, you have total control over how you test your application's limits.
In future episodes, we can explore authenticating users, sending POST requests, and organizing test data. Keep testing, and build stronger systems!
If you need professional help optimizing your infrastructure, we're always here.
See our expertise in performance engineering.Whether you’re starting fresh or refining your vision, we’re here to bring it to life. Let’s collaborate and build your perfect solution together!