How can I add a condition so that this threshold only applies to one test?

Hello!
I have a threshold and I want this threshold to be applied only for one test, if this test falls, the test should stop.

export let options = {
thresholds: {
“Error_Status”: [
{ threshold: ‘rate < 0.01’, abortOnFail: true }
]
}
}

How can I add a condition so that this threshold only applies to one test?

Hi,

can you clarify what you mean by “test”? Typically we refer to “test” as the entire k6 run of a single JS script. In that case your abortOnFail usage will do as you want and abort the entire test run.

If you’re referring to a k6 scenario, then aborting based on a failure of a single scenario is currently not supported, though it is on our roadmap, see issue #1833.

1 Like

You can fail the current VU by using the fail() command.
It isn’t very graceful, but it stops that VU and the others keep going.

@bethschrag fail will fail just the current iteration, but not prevent the VU to start another … it is in practice just throw "something" under the hood, so it is even possible for the js code to prevent the abort.

yes, I decided to abort the test with throw "something"

For completeness: k6 does actually emit a tag per scenario so it is possible to get metrics emitted by a single scenario (a bigger example in v0.27.0 release notes):

import http from 'k6/http';
import { sleep } from 'k6';

export let options = {
    scenarios: {
        my_web_test: { // some arbitrary scenario name
            executor: 'constant-vus',
            vus: 5,
            duration: '30s',
            exec: 'webtest', // the function this scenario will execute
        },
        my_api_test: {
            executor: 'constant-arrival-rate',
            rate: 9, timeUnit: '1m', // 90 iterations per minute, i.e. 1.5 RPS
            duration: '30s',
            preAllocatedVUs: 10, // the size of the VU (i.e. worker) pool for this scenario
            maxVUs: 10, // we don't want to allocate more VUs mid-test in this scenario
            exec: 'apitest', // this scenario is executing different code than the one above!
        },
    },
    discardResponseBodies: true,
    thresholds: {
        'http_req_duration{scenario:my_web_test}': ['p(95)<250', 'p(99)<350'],
        'http_req_duration{scenario:my_api_test}': ['p(95)<250', 'p(99)<350'],
        'http_req_duration': ['p(95)<250', 'p(99)<350'],
    }
};

export function webtest() {
    http.get('https://test.k6.io/contacts.php');
    sleep(Math.random() * 2);
}

export function apitest() {
    http.get(`https://httpbin.test.k6.io/delay/5`); // this takes 5 seconds
}

produces


and you can add abortOnFail and so on.

Calling throw will just stop the current iteration, if that is what you mean by “test” then it will work, but it will just start a new iteration (as per whatever executor you are using) and continue executing as long as it wasn’t the last iteration.

1 Like