Selecting k6 scenario using grafana environment variable

Hello there!

I have a k6 performance test script that I would like to use in a scheduled run. However, it seems that all script need to be run at least once before it is available for a scheduled test. This script is not something that we want to execute during the day, so I tried to build in a default scenario that executes the test a single time, and a breaking test that we want to run at night. I then want to select the scenario to execute based on an environment variable that I set in the Grafana k6 settings. Some example code snippets of how I am trying to achieve this:
Preparing the scenarios:

let scenarios = {
  default: {
    executor: "per-vu-iterations",
    vus: 1,
    iterations: 1,
  },
  breaking: {
    executor: "ramping-vus",
    stages: [
      { duration: "3m", target: 1 },
      { duration: "3m", target: 10 },
      { duration: "3m", target: 20 },
      { duration: "3m", target: 30 },
      { duration: "3m", target: 40 },
      { duration: "3m", target: 50 },
      { duration: "3m", target: 60 },
      { duration: "3m", target: 70 },
      { duration: "3m", target: 80 },
      { duration: "3m", target: 90 },
      { duration: "3m", target: 100 },
    ],
  }
}

Setting up the options:

export const options = {
  scenarios: {},
  // other options here
  thresholds: {
    'http_req_failed{type:vacancy}': [{ threshold: 'rate<0.01', abortOnFail: true }], // http errors should be less than 1%, otherwise, abort
    'http_req_duration{type:vacancy}': ['p(95)<5000'], // 95% of requests should be below 5000ms
  }
}

And selecting the scenario:

if (__ENV.PRODUCTNAME_SCENARIO) {
  options.scenarios[__ENV.PRODUCTNAME_SCENARIO] = scenarios[__ENV.PRODUCTNAME_SCENARIO];
}
else {
  options.scenarios["default"] = scenarios["default"];
}

I run the k6 script in the cloud without any environment variable, and the default scenario is performed. Then, when I add the environment variable to the Grafana k6 settings, it seems to recognize the variable when I look at the script. However, in the overview of the executed test, it tells me no scenario was configured, with or without the environment variable.

What could be the reason that the scenario is not being used correctly, using this setup? Any help would be greatly appreciated.

Hello @tcremers,

Welcome to the forum :tada:

I have literally copy-pasted your script, with a minimalist test function:

let scenarios = {
	default: {
		executor: "per-vu-iterations",
		vus: 1,
		iterations: 1,
	},
	breaking: {
		executor: "ramping-vus",
		stages: [
			{duration: "3m", target: 1},
			{duration: "3m", target: 10},
			{duration: "3m", target: 20},
			{duration: "3m", target: 30},
			{duration: "3m", target: 40},
			{duration: "3m", target: 50},
			{duration: "3m", target: 60},
			{duration: "3m", target: 70},
			{duration: "3m", target: 80},
			{duration: "3m", target: 90},
			{duration: "3m", target: 100},
		],
	}
}

export const options = {
	scenarios: {},
	// other options here
	thresholds: {
		'http_req_failed{type:vacancy}': [{ threshold: 'rate<0.01', abortOnFail: true }], // http errors should be less than 1%, otherwise, abort
		'http_req_duration{type:vacancy}': ['p(95)<5000'], // 95% of requests should be below 5000ms
	}
}

if (__ENV.PRODUCTNAME_SCENARIO) {
	options.scenarios[__ENV.PRODUCTNAME_SCENARIO] = scenarios[__ENV.PRODUCTNAME_SCENARIO];
}
else {
	options.scenarios["default"] = scenarios["default"];
}

export default function () {
	console.log(`Environment variable: ${__ENV.PRODUCTNAME_SCENARIO}`);
}

Executed it once, successfully.
Scheduled it to be executed every hour.
Configured an environment variable named PRODUCTNAME_SCENARIO equals to breaking in my k6 settings.

Then, in the next execution, the breaking scenario has been executed successfully.

So, considering that I couldn’t reproduce your issue, would you mind reviewing everything again (the environment variable name and value is correct, etc), and if everything looks good and still not works, open a support ticket, please?

To do so, you can click on the question mark (?) icon on the top right corner of your Grafana Cloud instance, then go to “Support”, and there you’ll be able to submit a new ticket.

I have no access to your specific instance/test (and I should not for obvious reasons), so let’s follow the process through support and see if they can help. If they manage to reproduce the issue, I’ll be more than happy to contribute and support with your case.

Thanks! :bowing_man:

1 Like