Modifying data from setup() in default function

Hello!
Maybe there is already some related topic, but haven’t found it.

I have a following case:
Need to create some array, where will be pushed some values from different VUs during a test executions. And then I want to go through this array in teardown() and execute some requests there.

Thought about a variable in init stage, but it is visible only in default and is created for each VU separately.
Then tried using data like this:

 export function setup() {
    let array = [];
    return array
}

export default function(data) {
    data.push(__ITER)
}

export function teardown(data) {
    for (let i = 0; i < data.length; i++) {
        console.log(` ITER: ${data[i]} `) 
    }
} 

But then I got this error:

TypeError: Cannot extend Go slice
at push (native)
So the questions are:

  1. Is it possible somehow to collect data from all VUs and then use it in teardown function?
  2. If not, is this possible for 1 VU? Because I’m still not able to modify data in default

Hi,

unfortunately this specific use case is not supported due to the way execution is implemented: setup() and teardown() are run in separate temporary JavaScript VMs, and default() is run by each VU also in a separate JS VM. This means that sharing data is tricky, but not impossible.

Passing the data returned from setup() to default() and teardown() is a convenient way of sharing some fixed data required for execution or cleanup (login credentials, headers, tags, etc.), but is not meant to be modified during a test run.

I would suggest looking into other ways of doing this. For example, write to a Redis server in default(), and read from it in teardown(). I’m not sure if the Node Redis client would work as is in k6, but you’ll have to experiment and get creative. :slight_smile: This will probably impact your performance, but currently I don’t see a better approach.

Hope this helps,

Ivan

1 Like