K6: Provide an API to exit a test

Created on 17 Apr 2019  ·  2Comments  ·  Source: loadimpact/k6

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:

  1. The Threshold API works well for evaluating complex conditions but the API is not intuitive or simple when you want just to check a simple condition.
    if (condition) { 
        exit();
    }
  1. It cannot exit the load test on a non-error mode.
exit(0);
  1. It cannot provide different exit codes. Related issue: Different exit codes for different errors
exit(5);
  1. it cannot provide user message
exit(10, “service was not available”);
  1. The API should also work on the 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.

evaluation needed feature ux

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:

  • I prefer 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.
  • This likely shouldn't be a global function, rather it would probably be better if users have to import it as import { abort, sleep } from "k6";

All 2 comments

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:

  • I prefer 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.
  • This likely shouldn't be a global function, rather it would probably be better if users have to import it as 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.

Was this page helpful?
0 / 5 - 0 ratings