I am working on a script to test my infra using k6.
I would like each VU be assigned to a previous known id obtained from an existing array and that VU execute the instructions from the default function only once at the time the VU is created.
How to iterate over this array, get the id and use it for each VU created by the script?
The initial idea is bellow, but I am sure it is not correct.
export let options = {
stages: [
{ duration: '10s', target: 1 },
{ duration: '10s', target: 10 },
],
};
const ids = [
{'id':1, 'name':'name1'},
{'id':3, 'name':'name3'},
{'id':4, 'name':'name4'},
{'id':18, 'name':'name18'}];
export default function () {
for(var i=0; i<ids.length; i++) {
var user = ids[i];
//do something with user.id
}
}
According to K6 manual, each VU will execute everything is inside the default function, but it would make all VU's execute the for loop and it is not the behavior I want.
I want the VU execute what is inside the for, but using a different id for each new VU. Something like bellow:
export default function () {
var user = ids[i];
//do something with user.id
}