Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes made to variable in setup are not persisted to default #1710

Closed
dafydd-t opened this issue Nov 4, 2020 · 2 comments
Closed

Changes made to variable in setup are not persisted to default #1710

dafydd-t opened this issue Nov 4, 2020 · 2 comments

Comments

@dafydd-t
Copy link

dafydd-t commented Nov 4, 2020

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.

@dafydd-t dafydd-t added the bug label Nov 4, 2020
@mstoykov
Copy link
Collaborator

mstoykov commented Nov 5, 2020

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 :)

@dafydd-t
Copy link
Author

dafydd-t commented Nov 5, 2020

Thank you for explaining! I now understand why the limitation is there.

@dafydd-t dafydd-t closed this as completed Nov 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants