To use k6 effectively, you need to understand the basic script structure, options and thresholds that control when a test is considered successful. These concepts let you model load and define pass/fail criteria inline with your test code.
Script Structure, VUs and Duration
A k6 script typically exports an options object that configures virtual users (VUs), duration and scenarios, plus a default function that runs for each VU. You can run simple constant-load tests by specifying vus and duration, or use more advanced scenario definitions.
export const options = {
vus: 20,
duration: '1m',
};
export default function () {
// test logic here
}
Using Thresholds as Built-In Quality Gates
Thresholds in k6 express performance goals directly in codeβfor example, requiring that the 95th percentile of a metric stays below a limit. If thresholds are violated, k6 exits with a non-zero status, which CI tools can treat as a failed job.
export const options = {
vus: 50,
duration: '2m',
thresholds: {
http_req_duration: ['p(95)<800', 'avg<400'],
http_req_failed: ['rate<0.01'],
},
};
With thresholds, performance expectations become part of your test definition rather than a separate document.
Common Mistakes
Mistake 1 β Running k6 scripts without thresholds
This loses pass/fail clarity.
β Wrong: Manually scanning metrics to decide if a run was acceptable.
β Correct: Encode key limits as thresholds so failures are explicit.
Mistake 2 β Treating VUs as a direct proxy for real users
This oversimplifies behaviour.
β Wrong: Assuming β100 VUs equals 100 humans.β
β Correct: Interpret VUs in combination with request rates and think time.