Validate thresholds work

Adding thresholds to your script isn’t enough. You need to confirm they actually pass and fail as expected. In this milestone, you run the test twice: once to verify it passes under normal conditions, and once with a deliberately strict threshold to confirm it fails correctly.

To validate your thresholds, complete the following steps:

  1. Run the test:

    Bash
    k6 run baseline.js
  2. At the top of the summary output, look for the THRESHOLDS section and verify that all thresholds pass:

    text
    THRESHOLDS
      checks
        ✓ 'rate>0.99' rate=100.00%
    
      http_req_duration
        ✓ 'p(95)<500' p(95)=54.42ms
    
      http_req_failed
        ✓ 'rate<0.01' rate=0.00%

    A ✓ next to each threshold means it passed. The rate>0.99 on the checks threshold means the check pass rate must be above 99%. This is not related to p99 latency. The exit code should be 0, which signals success.

  3. Verify the exit code in your terminal:

    Bash
    echo $?

    The output should be 0.

  4. Now test that a threshold failure works correctly. Temporarily set a very strict latency threshold that your system can’t meet. Open baseline.js and change the http_req_duration threshold:

    JavaScript
    thresholds: {
      http_req_duration: ['p(95)<1'],
      http_req_failed: ['rate<0.01'],
      checks: ['rate>0.99'],
    },

    A p95 under 1 ms is impossible for any real HTTP request, so this threshold should fail.

  5. Run the test again:

    Bash
    k6 run baseline.js

    The THRESHOLDS section now shows a ✗ next to the failed threshold:

    text
    THRESHOLDS
      checks
        ✓ 'rate>0.99' rate=100.00%
    
      http_req_duration
        ✗ 'p(95)<1' p(95)=54.42ms
    
      http_req_failed
        ✓ 'rate<0.01' rate=0.00%
  6. Verify the exit code is non-zero:

    Bash
    echo $?

    The output should be 99, which is k6’s exit code for threshold failures. This non-zero exit code is what CI/CD systems use to detect a failed test.

  7. Restore your original threshold value before continuing:

    JavaScript
    http_req_duration: ['p(95)<500'],

You’ve confirmed that your thresholds work in both directions: passing when metrics are within bounds and failing when they’re breached.

In the next milestone, you save your results to k6 Cloud for historical tracking and team visibility.


page 8 of 10