How to overwrite values from config file dynamically

I want to config my scenario using config file. However, some parameters in certain scenarios, I would like to set dynamically. For example in the config below for scenario 1, I would like to set start rate and target dynamically by env variables or from CLI when running the script. How can I to achieve this? Is there an example somewhere?

 "scenarios": {
    "scenario1": {
      "executor": "ramping-arrival-rate",
      "exec": "exec1",
      "preAllocatedVUs": 50,
      "maxVUs": 100,
      "startRate": 10,
      "timeUnit": "1s",
      "stages": [
        {
          "target": 10, "duration": "10s"
        }
      ]
    },

Hi @Jac !

Config files are static JSON files, and there you can’t use environment variables. However, you can easily achieve that migration to the options inside your scripts.

import { sleep } from "k6";

export let options = {
   "scenarios": {
      "scenario1": {
        "executor": "ramping-arrival-rate",
        "preAllocatedVUs": 50,
        "maxVUs": 100,
        "startRate": __ENV.MY_RATE,
        "timeUnit": "1s",
        "stages": [
          {
            "target": 10, "duration": "1m"
          }
        ]
      },
   }
}

export default function () {
   console.log("hey");

   sleep(1);
}
 MY_RATE=15 k6 run script.js

Let me know if that answers,
Cheers!

Hey @olegbespalov
I need to use config file, because I want to be able to run same code with different configuration.
I only need to overwrite certain values in the code if they are provided in env variables. Is it possible?

I think what I need is here: Options reference
Because if I provide env variables or CLI options it will overwrite the config file. Right? However, I do not know how to overwrite certain stages for particular scenario. Is there any example?

Hi @Jac,

As shown in Scenario config file - #2 by nedyalko you can load the config file as a normal file, parse it from JSON and then set it to export const options. You can also just before that change w/e options you want.

This also (as shown there) can be done only for some option keys - you don’t need to write the whole configuration, as needed for --config.

And you can use env variables to choose the “configuration file” an then what the replace for your needs.

Hope this helps you!

Thanks @mstoykov ! It is helpful.