Exclude `http` requests made in the setup and teardown functions

Hi Community,

Thanks for the great work and effort.

In my test cases, I need to “setup” a database with data, and in the end to “teardown” the database - on every test file (I’m using the handleSummary to write the http_req_duration metrics to JSON and then combine all the JSON files to CSV).

I used the setup/teardown functions to leverage the convenience and also used http.batch for the speed (because we setup/teardown 100/1000/10000 “users” to the database)

But what I noticed eventually at the summary was that the setup/teardown HTTP requests (http.batch) were also included in the http_req_duration etc …

Seems a bit strange, I thought that only the VUs default function is measured.

How can I bypass this issue with minimal refactoring? (lot of code written so far).

Ah, this is similar to Ignore http calls made in Setup or Teardown in results? - #2 by nedyalko, and as I mention there, until we fix Add explicit tracking and ignoring of metrics and sub-metrics · Issue #1321 · grafana/k6 · GitHub there are some workarounds that are required to ignore metrics from setup() and teardown().

The easiest one is to surface a sub-metric of http_req_duration that doesn’t include them. And until we fix that issue, the easiest hack to do that is to set a bogus threshold on it, like this for example:

export let options = {
    thresholds: {
        'http_req_duration{scenario:default}': [`max>=0`],
    },
}

If you have multiple scenarios, you can add a custom tag to each via their tags option and use that instead of scenario:default. The important thing is to have a sub-metric that doesn’t include the metrics from setup() and teardown() and then work with that in handleSummary().

There are some more examples in this docs issue: Add example for sub-metrics by scenario (or other tags) in summary · Issue #205 · grafana/k6-docs · GitHub