For load testing in the vu
stage I generate a lot of objects with unique ids that I put them in the database. I want to delete them during teardown
stage in order not to pollute the database.
When keeping the state like this
let ids = [];
export function setup() {
ids.push('put in setup id');
}
export default function () {
ids.push('put in vu id');
}
export function teardown() {
ids.push('put in teardown id');
console.log('Resources: ' + ids);
}
it doesn't work as the array always contains the data I put in teardown
stage.
Passing data between stages also doesn't work due to well-know Cannot extend Go slice
issue, but even with that, you cannot pass the data from vu
stage to teardown
as it always gets the data from setup
stage.
The only remaining solution is either playing around with console log
or just use a plain preset of ids and use them in tests. Is there another way?
The setup()
, teardown()
, and the VUs' default
functions are executed in completely different JavaScript runtimes. For distributed execution, they may be executed on completely different machines. So you can't just have a global ids
variable that you're able to access from everywhere.
That limitation is the reason why you're supposed to return any data you care about from setup()
- k6 will copy it and pass it as a parameter to the default
function (so you can use whatever resources you set up) and teardown()
(so you can clean them up).
Your example has to look somewhat like this:
export function setup() {
let ids = [];
ids.push('put in setup id');
return ids;
}
export default function (ids) {
// you cannot push to ids here
console.log('Resources: ' + ids);
}
export function teardown(ids) {
console.log('Resources: ' + ids);
}
You can find more information at https://k6.io/docs/using-k6/test-life-cycle