I have defined the locale variable in init function and displaying the value in init & default function. Value is getting changed in default function as init function is getting executed multiple times.
Output:
INFO[0001] Init= fr_fr source=console
INFO[0001] CSV_Prod= fr_fr source=console
INFO[0001] CSV_SDP= fr_fr source=console
execution: local
script: .\csv_locale.js
output: -
scenarios: (100.00%) 1 scenario, 1 max VUs, 10m30s max duration (incl. graceful stop):
* default: 1 iterations for each of 1 VUs (maxDuration: 10m0s, gracefulStop: 30s)
INFO[0001] Init= fr_be source=console
INFO[0001] VU: 1 - ITER: 0 source=console
INFO[0001] RandomProduct from fr_be= 0641939001 source=console
Yes I have script with shared array which need to be declared in INIT function. I am using same variable in shared array(INIT) and group(export default function), value of variable is getting changed due to multiple execution of init function.
Again the init is executed once per each VU that needs to initialized. There is no way that we can have a VU without running it’s init context and there are no variables in the init context of a VU before we run the init context.
I would suggest that you try to show how this is breaking something for you with a some full example - it can be a much smaller version of your real script.
Refer output mention above.
locale value is getting changed in export function from init function. The cause of this is multiple execution of init function resulting to multiple value of locale, export function is taking the latest value of locale.
This is not true though. The init context is executed multiple times, sure, but only once in every VU Every VU is just an independent JavaScript runtime, it has its own scope and variables and context. And, for a VU to be initialized, it needs to execute the global scope (the so called “init context”). So the exported default function will use the value of locale that was initialized in the init context of that VU.
I feel like you are conflicting stuff so I will try to explain what happens in your particular script.
First k6 will start by parsing the script and then execute it in order to see that everything seems fine.
This will execute the “init” code once and will then check that for example the options are valid, you have a default function. There was no exception while executing it and stuff like that.
That particular execution will also execute the new SharedArray code and will call the provided function as explained in the documentation.
Then after that depending on the options k6 will start creating new VUs that will be used for the actual execution of the test.
Those VUS will once again need to run the whole init code as … well that is how JavaScript works - they need to run the code in order for it to know what locale as a constant is for example. Or that there is a function exported as a default.
For your particular case that also means that the function to SharedArray will not be executed again and instead the result from the original call will be used. Which is exactly how SharedArray was designed to work.
I don’t understand what you are trying to do in order to help you to tell you what is wrong.