You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If I declare a variable in the init code, and then amend its value in setup, this change is not persisted to default. I assumed this would be possible and I cannot see any documentation about this being desired behaviour.
Environment
k6 version: running docker image k6:master
OS and version: macOS 10.15.7
Docker version and image, if applicable: k6:master on docker 19.03.13
Expected Behavior
// init
let count = 0;
export function setup() {
count++;
console.log(count) // 1
}
export default data => {
console.log(count) // 1
}
Actual Behavior
// init
let count = 0;
export function setup() {
count++;
console.log(count) // 1
}
export default data => {
console.log(count) // 0
}
Steps to Reproduce the Problem
Run the code snippet above as a test.
The text was updated successfully, but these errors were encountered:
Hi @dafydd-t, This is the expected and wanted behavior.
As is mentioned in numerous places VUs are separate js VMs which barely have anything in common. Copying all the variables from the "setup" VU to the VUs that will execute the "default" function while probably not impossible is likely to be impractical especially in the cloud and in the future distribution setup.
For the record while your example shows a single variable, for this to be actually the behaviour that we want we need to copy essentially the whole js VM status, which is currently not supported by the underlying js implementation we use, and likely never will be.
Maybe we do might need to explain this better but if you look at the test life cycle documentation:
As an added bonus, you can use this to reuse data between iterations (but only for the same VU):
this speifically says the "same" VU, while a little bit below you will see:
Therefore, VU number is 0 while executing the setup and teardown functions.
and from the context above it becomes obvious that the VUs running the "default" function are not the same VUs.
Also in that same Setup and teardown stages section it is explained how to use the data returned by setup to share data from the setup with the VUs executing the "default" function. Again because it is running between different js VMs the data is copied and any changes to it will not be noticed by other VUs.
I hope this helps you to understand why this is the case ... and how it is extremely unlikely to change :). I am going to open a documentation issue and link it to this one so we expand the documentation in order for this to be more clear. If you have any recomendations or would like to propose a change you are welcome :)
If I declare a variable in the init code, and then amend its value in setup, this change is not persisted to default. I assumed this would be possible and I cannot see any documentation about this being desired behaviour.
Environment
k6:master
19.03.13
Expected Behavior
Actual Behavior
Steps to Reproduce the Problem
Run the code snippet above as a test.
The text was updated successfully, but these errors were encountered: