The k6 API does not provide an API to exit a load test. The fail does not cover this case.
A common workaround is to configure a threshold that aborts the test when the threshold is crossed.
import { sleep } from "k6";
import http from "k6/http";
import { Counter } from "k6/metrics";
export let options = {
stages: [
{ duration: "30s", target: 50 }
],
thresholds: {
"myThresholdMetric": [{threshold: "count<1", abortOnFail: true}]
}
};
const myThresholdMetric = new Counter("myThresholdMetric");
export default function() {
http.get("https://test.loadimpact.com/");
if (__VU === 1 && __ITER === 1) {
myThresholdMetric.add(1);
}
sleep(3);
};
But this or other workarounds have a few inconveniences or limitations:
if (condition) {
exit();
}
exit the load test on a non-error mode. exit(0);
exit codes. Related issue: Different exit codes for different errorsexit(5);
exit(10, “service was not available”);
setup/teardown methods.export function teardown(time) {
if (condition) {
exit(1);
}
}
The exit API is a suggestion, but in any case, I think it could be a good addition if k6 provides an API to support these cases during local and cloud execution. Some users have already asked for a similar utility.
This is a partial duplicate of an older issue, but I'll close the old one and leave this one open, since this is more detailed and somewhat easier to find. Just a couple of comments on the proposed API:
abort() as the name of the new function, as proposed in the other issue. It's makes it more clear that we're aborting the whole test. exit() also has connotations of immediate termination, while k6 actually has to perform some housekeeping when it aborts tests, like sending latest metrics to collectors, etc.import { abort, sleep } from "k6";Since we now have the powerful scenario's. Would it be possible to abort a scenario so the next one starts?
That would be a massive win for me.
Most helpful comment
This is a partial duplicate of an older issue, but I'll close the old one and leave this one open, since this is more detailed and somewhat easier to find. Just a couple of comments on the proposed API:
abort()as the name of the new function, as proposed in the other issue. It's makes it more clear that we're aborting the whole test.exit()also has connotations of immediate termination, while k6 actually has to perform some housekeeping when it aborts tests, like sending latest metrics to collectors, etc.import { abort, sleep } from "k6";