技術(tech)

How to get scenario metadata like name or VU in k6 script

 

Let me introduce how to get scenario metadata like scenario name, VU, etc… in k6.

This method is useful if you want to set up any configuration per each scenario name before executing each scenario.

 

Conclusion

Let’s use k6/execution API.

https://k6.io/docs/javascript-api/k6-execution/#test

By using this, you can get metadata that passed from options.

For example, the following option settings have been passed.

https://github.com/gonkunkun/k6-template/blob/main/src/sample-product/configs/smoke.json

The following sample script can be executed to get the option values.

import exec from 'k6/execution'

const scenarios = exec.test.options.scenarios
console.log(scenarios)

 

Output is here.

{
  "sampleScenario1": {
    "executor": "per-vu-iterations",
    "startTime": "0s",
    "gracefulStop": null,
    "env": null,
    "exec": "sampleScenario1",
    "tags": null,
    "vus": 1,
    "iterations": 1,
    "maxDuration": "1m0s"
  },
  "sampleScenario2": {
    "executor": "per-vu-iterations",
    "startTime": "0s",
    "gracefulStop": null,
    "env": null,
    "exec": "sampleScenario2",
    "tags": null,
    "vus": 1,
    "iterations": 1,
    "maxDuration": "1m0s"
  }
}

 

It’s useful if you wanna initialize any settings per scenario names in setup() before executing each scenario.