Smoke + load tests
Sanity check + steady-state expected traffic.
Grit scaffolds six load tests into tests/k6/. They all run the same user journey from lib/common.js and differ only in their stages, which is the shape of the load over time. This lesson is the first two, and they are the two you will actually run most weeks.
Smoke: does the test work?
Two virtual users for thirty seconds. That is not a load test, and it is not meant to be:
import { userJourney, defaultThresholds } from './lib/common.js'// k6 only runs setup() when the entry script exports it.export { setup } from './lib/common.js'export const options = {vus: 2,duration: '30s',thresholds: defaultThresholds,}export default userJourney
Run it before every other type. A stress test that fails tells you nothing until you know the script itself works, because the two look identical from the outside: requests failing, thresholds breached, a red summary. Smoke separates "the system broke" from "my test was never right".
This is not hypothetical. The suite Grit shipped until v3.432.0 asked for /health, a path no Grit API has ever served, and /api/blogs, which only a project with a Blog resource serves. Every request 404d, every threshold breached, and all six tests reported a broken system before measuring anything about it. Nobody noticed, because nobody ran smoke first and asked why a two user test was failing.
What a passing smoke run looks like
✓ health: status 200✓ profile: status 200checks_succeeded...: 100.00% 60 out of 60http_req_failed....: 0.00% 0 out of 62http_req_duration..: p(95)=3.42ms p(99)=69.28mshttp_reqs..........: 62 1.98/s
Two numbers are worth noting now. 62 requests, not 60: the two extra are the sign-in in setup(), which runs once and is not part of any iteration. And p99 is twenty times p95 on a run this small, because one request was the first to touch a cold code path. At 60 requests, p99 is a single request. It becomes a number worth reading at thousands.
Average load: the baseline
export const options = {stages: [{ duration: '2m', target: 100 }, // ramp up{ duration: '5m', target: 100 }, // hold (steady state){ duration: '2m', target: 0 }, // ramp down],thresholds: defaultThresholds,}
The hold is the point. A ramp measures a system that is still filling its connection pool, warming its caches and compiling nothing in particular; the five minutes at a hundred users is the only part that describes normal operation. Report p95 from the hold, not from the whole run.
The thresholds are the same ones smoke uses, and they are what turns this into a regression check rather than a number you write down:
export const defaultThresholds = {http_req_duration: ['p(95)<500', 'p(99)<1000'],http_req_failed: ['rate<0.01'],checks: ['rate>0.99'],}
A breached threshold makes k6 exit non-zero, which is the whole reason these two tests belong in CI and the other four do not.
Point it at something that costs money
The journey reads /api/health and /api/profile. Both are cheap, and a journey made of cheap requests reports a system faster than yours is. Name one of your own resources:
k6 run -e RESOURCE=products tests/k6/average-load.js# against staging, as an account with a roleBASE_URL=https://staging.example.com \k6 run -e RESOURCE=products \-e LOADTEST_EMAIL=perf@example.com \-e LOADTEST_PASSWORD=... \tests/k6/average-load.js
A resource generated by grit generate resource needs ADMIN or its own permission. The throwaway user the journey registers is a USER, so without LOADTEST_EMAIL you get a run of 403s: fast, consistent and completely meaningless.
Try it
One. Run k6 run tests/k6/smoke.js against your local API. Write down p95 and the request count.
Two. Run it again with -e RESOURCE= set to one of your resources. Write down p95 again.
Three. Run the average load test. Compare its p95 to the smoke run's, and account for the difference.
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