k6 Script Basics, Options and Thresholds

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
}
Note: VUs in k6 are lightweight, event-loop-based workers, not full OS threads, which allows high concurrency from a single machine.
Tip: Start with small VU and duration values to validate scripts before scaling to heavier loads.

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'],
  },
};
Warning: Setting thresholds unrealistically low can cause constant failures; base them on baselines and business SLAs.

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.

🧠 Test Yourself

How do thresholds help when running k6 in CI?