In this blog we will know about the check function and thresholds that is commonly used in k6 load testing.
What is check function in k6?
A check is a test condition that can give a truthy or falsy result. The sets parameter contains one or more checks, and the check() function will return false if any of them fail.
Let say we are writing a load test script where you need to perform some checks when we receive the response for given endpoint.
We can use check function to add checks in your load test script. Following is the function signature:
check( val, sets, [tags] )
Parameter | Type | Description |
val | any | Value to test |
sets | object | Tests (checks) to run on the value. |
tags(optional) | object | Extra tags to attach to metrics emitted. |
Returns
Type | Description |
boolean | true if all checks have succeeded, false otherwise. |
Checks :
Checks are like assertions, but they don’t halt execution. Instead, they store the result of the check, pass or fail, and let the script continue.
The following points show some ways how we can use checks.
- Checks for HTTP response code
- Checks for text in response body
- Check for response body size.
Demo
Output:
Thresholds in k6
Thresholds are pass/fail criteria that specify the performance expectations of the system under test.
For example, you can use thresholds to test that your system meets the following expectations:
- Less than 1% of requests return an error.
- 95% of requests have a response time below 200 ms.
- 99% of requests have a response time below 400 ms.
- A specific endpoint always responds within 300 ms.
- Any conditions for a custom metric.
Thresholds analyze the performance metrics and determine whether the final results passed or failed the test.
In thresholds we have to mention Aggregation and metric.
Metric Type | AGGREGATION METHODS |
Counter | count and rate |
Gauge | value |
Rate | rate |
Trend | avg, min, max, med and p(N) where N is a number between 0.0 and 100.0 meaning the percentile value to look at, e.g. p(99.99) means the 99.99th percentile. The unit for these values is milliseconds. |