Share Writable or Mutable data between iterations in a VU

Hello , is it possible for VU to maintain a copy of writable data that can be passed to all the iterations as it runs ? I am using share-iterations executor where we pass data through setup , but when iterations run , we need to check and re-cache some of the expired data and have the following iterations from the same VU reuse the updated data.

@rahulgouthamgs If I understand your problem correctly, you could use the init stage of the test to initialize such writable variable. “All code that is outside of a lifecycle function is code in the init context”.

So for example running the folowing test script for 10 iterations with 3 VUs:

import exec from 'k6/execution';

let i = 0;

export default function (data) {
  i++;
  console.log(`vu-id:${exec.vu.idInTest} - i:`+i);
}

produces the following output:

INFO[0000] vu-id:2 - i:1                                 source=console
INFO[0000] vu-id:2 - i:2                                 source=console
INFO[0000] vu-id:3 - i:1                                 source=console
INFO[0000] vu-id:2 - i:3                                 source=console
INFO[0000] vu-id:3 - i:2                                 source=console
INFO[0000] vu-id:2 - i:4                                 source=console
INFO[0000] vu-id:1 - i:1                                 source=console
INFO[0000] vu-id:2 - i:5                                 source=console
INFO[0000] vu-id:3 - i:3                                 source=console
INFO[0000] vu-id:1 - i:2                                 source=console

so the variable i is shared between the iterations of the same VU.
Documentation about the init stage

1 Like