Using global variables in a k6 test within a VU

Hello,
I want to have some shared between the execution of multiple functions of one VU.

What I am doing this is below:

globalThis.Vars["dataOne"] = [];

export function create_data() {
const dataID = rest_call_add_data()
globalThis.Vars["dataOne"].push(dataID);
}

export function get_data() {
const dataID = globalThis.Vars["dataOne"][0];
some_rest_call_to_get_this_data()
}

config_file.json

{
  "thresholds": {},
  "scenarios": {
    "create_data": {
      "executor": "per-vu-iterations",
      "iterations": 5,
      "maxDuration": "5s",
      "vus": 3,
      "exec": "create_data",
      "startTime": "0s"
    },
    "get_data": {
      "executor": "per-vu-iterations",
      "iterations": 5,
      "maxDuration": "5s",
      "vus": 3,
      "exec": "get_data",
      "startTime": "1s"
    }
  }
}

But when I run this, there is not data in the globalVariable.

Am I missing something?

Hi @thenakulchawla !

Out of the box, there is no possibility of sharing the data between VUs and, in your case stages.

I’d recommend checking the Test lifecycle article to get more insights about what and where could be shared.

It could be possible to use some third-party DB like Redis, but it seems more complicated.

In your case, it seems possible to keep both calls in the same iteration or maybe use setup method.

Let me know if that helps,
Cheers!