November 13, 2026 · Ihor Stefaniv · 2 min read
Load testing with k6: your first scenario in 30 minutes
Install k6, write a realistic scenario, set thresholds on p95 and error rate, and read the results without fooling yourself.
Why k6
k6 is a free, open-source load testing tool from Grafana Labs. Scenarios are plain JavaScript, it runs from the command line and in CI, and a single machine can simulate thousands of virtual users. For HTTP APIs it's the fastest way from zero to a meaningful load test.
Install
brew install k6 # macOS
choco install k6 # Windows
docker run --rm -i grafana/k6 run - < script.js
A realistic scenario
Don't hammer one endpoint — model what users do:
import http from "k6/http";
import { check, sleep, group } from "k6";
export const options = {
stages: [
{ duration: "1m", target: 20 }, // ramp-up
{ duration: "3m", target: 20 }, // hold
{ duration: "1m", target: 0 }, // ramp-down
],
thresholds: {
http_req_duration: ["p(95)<800"],
http_req_failed: ["rate<0.01"],
},
};
const BASE = __ENV.BASE_URL || "https://staging.shop.example";
export default function () {
group("browse", () => {
const r = http.get(BASE + "/api/products?q=shirt");
check(r, { "search 200": (x) => x.status === 200 });
});
sleep(Math.random() * 3 + 1); // think time
group("cart", () => {
const r = http.post(BASE + "/api/cart", JSON.stringify({ sku: "TSHIRT-M", qty: 1 }), { headers: { "Content-Type": "application/json" } });
check(r, { "add to cart 201": (x) => x.status === 201 });
});
sleep(2);
}
Run it
k6 run -e BASE_URL=https://staging.shop.example script.js
If a threshold fails, k6 exits with a non-zero code — perfect for CI.
Reading the results
- http_req_duration p(95) — what an unlucky user experiences. Ignore the average; see why the average lies.
- http_req_failed — the share of failed requests. Even 1% at peak is a lot of angry users.
- iterations and vus — did you actually reach the target load?
- Look at the server side. CPU, memory, database connections and queue lengths during the test tell you why the numbers look the way they do.
Common mistakes
- Testing against an empty database — everything is fast.
- No think time — 20 virtual users behave like 200 real ones.
- Running the load generator on an underpowered laptop — you measure your laptop.
- Testing production without telling anyone.
Next steps
Add a stress profile (keep increasing until it breaks), a spike profile and a soak test of a few hours to catch memory leaks. Run a short version in CI before every release.