0

Is there a way to use a static variable shared across VUs in K6.

Say

// init code
let x = 0 // i want this to be static
// options
export let options = {
  vus : 10,
  iterations : 10
};
// VU code
export default function() {
  x++;
  console.log(x);
}

When I run this piece of code, the output should be incremental (1 to 10) and not 1 printed 10 times (1 for each VU).

Sid
  • 45
  • 2
  • 9

1 Answers1

2

In k6, each VU is a separate independent JS runtime, so you essentially have 10 copies of x. There is no way around that with stock k6 for now, you have to use some external service as an incrementing counter via HTTP or something like that. Alternatively, if you'll run k6 locally and only on a single instance, you can use this xk6 extension (more info): https://github.com/MStoykov/xk6-counter. It was a PoC originally developed for https://community.k6.io/t/unique-test-data-per-vu-without-reserving-data-upfront/1136/3 , but can be easily extended.

na--
  • 1,016
  • 7
  • 9