Stress + spike tests
Push to breaking point; sudden traffic surges.
Smoke and average load ask whether the system works. Stress and spike ask what it does when it stops working, which is a different question and usually a more useful one, because every system stops working eventually and only some of them do it gracefully.
Stress: four times normal, held
export const options = {stages: [{ duration: '2m', target: 100 }, // normal{ duration: '5m', target: 400 }, // 4x normal, the stress{ duration: '2m', target: 0 }, // ramp down],thresholds: aggressiveThresholds,}
Four times is a convention, not a law. Pick the multiple that represents your worst realistic day: the Monday after a launch, the hour after an email goes out, Black Friday. The two minutes at normal load first are there so you have a before to compare against, in the same run and on the same machine.
Why the thresholds are looser
export const aggressiveThresholds = {http_req_duration: ['p(95)<2000'],http_req_failed: ['rate<0.10'],}
A stress test is supposed to hurt. With the normal thresholds it would breach in the first thirty seconds and the run would report failure, which tells you only what you already intended. The looser set lets the run finish so you can watch how it degrades.
The thing you are actually looking for
Two systems at 4x load
Rising latency with a flat error rate is a queue doing its job: requests wait, and they are served. Rising latency and rising errors means something is giving up: a connection pool timeout, a context deadline, an out of memory kill. The second is the one that wakes you up, and it is invisible in any single number.
Watch the API's own memory and the database's CPU during the run, not just k6's output. A system that collapses at 4x almost never does so because of the API process: it runs out of database connections, or the database runs out of CPU.
Spike: the surge, and the recovery
export const options = {stages: [{ duration: '1m', target: 50 }, // baseline{ duration: '10s', target: 1000 }, // SLAM{ duration: '1m', target: 1000 }, // hold the spike{ duration: '10s', target: 50 }, // drop back{ duration: '1m', target: 50 }, // recovery window, watch p95{ duration: '10s', target: 0 },],thresholds: aggressiveThresholds,}
Most people read this script as the 10 second ramp to a thousand users. The important stage is the last minute, back at fifty.
A system that survives a spike and returns to its baseline latency has absorbed it. A system whose p95 is still elevated a minute after the surge has not: something is still draining. A retry storm, a queue that filled and is being worked off, a cache that was evicted and is being rebuilt one request at a time. That is the finding, and it only exists in the recovery window.
A thousand virtual users from one laptop is usually a test of your laptop. If CPU on the k6 machine saturates, every number it reports includes its own queueing. Watch the load generator as carefully as the system under test, and move k6 to a bigger box before concluding anything about the system.
Try it
One. Run the stress test with -e RESOURCE= set to your heaviest list endpoint. Record p95 at the two minute mark and at the seven minute mark.
Two. Record the error rate at the same two points. Classify the system: graceful or collapsing.
Three. Run the spike test. Record p95 during the hold, and again in the last thirty seconds of the recovery window.
Quick check
Spot a typo? Have an idea?
Help us improve this lesson. One click opens a GitHub issue with the lesson URL pre-filled, suggest clearer wording, report a bug, or request more depth. The course keeps improving thanks to learners like you.
Suggest an improvement on GitHub