So im having a little trouble. I need to iterate through an array of 1000+ id’s. Each id is unique and I’ll probably store in a shared array (since each iteration needs access to the id’s).
However my issue is: I need to be able to (for each loop of the default function) to iterate up by 1 into the next id.
IE: I have an array of 1000’s ID’s.
I need to make a request ONE TIME for each ID. IE /users/ /users/ etc…
Is the best way to just define a let count = 0
and set iterations to 1000 and VU’s to 1 and then just up the count each time? With a setup() function loading all the data into a shared array.
Hi @mercfh
Have you had a look at Data Parameterization? Using scenario.iterationInTest
?
Cheers!
Yeah, I did have a confusion: Does it start at 0 and move up? or are they just using a random unique iterator number?
exec.scenario.iterationInTest
worked for me. Im a little bit confused at vu.idInTest
at what it does. From my understanding if I have 100 records and 5 VU’s. Each VU would would get a unique ID, but VU’s may share ID’s (but not in the same VU).
OR
Lets say 5 VU’s split up 100 iterations/records. In that case maybe VU 1 would get ID’s 0-19, VU 2 would get 20-39 etc… (I know thats not how it works exactly, but curious if they share id’s).
If it’s the second one, for a single scenario/test I am not seeing the difference between vu.IdinTest
and scenario.IterationInTest
Hi @mercfh
For this kind of doubts I recommend creating scripts to understand the behavior. And if the documentation does not make it clear it’s always great to report this, so the docs can be improved.
For example, for VU IDs and iterations, documented in https://k6.io/docs/javascript-api/k6-execution/:
import exec from 'k6/execution';
export const options = {
scenarios: {
'first-scenario': {
executor: 'shared-iterations',
vus: 1,
iterations: 2,
maxDuration: '1m',
},
'second-scenario': {
executor: 'shared-iterations',
vus: 2,
iterations: 4,
maxDuration: '1m',
},
},
};
export default function () {
console.log("scenario.name:", exec.scenario.name, "scenario.iterationInTest:", exec.scenario.iterationInTest, "vu.idInTest:", exec.vu.idInTest, "vu.iterationInScenario", exec.vu.iterationInScenario);
}
INFO[0000] scenario.name: first-scenario scenario.iterationInTest: 0 vu.idInTest: 2 vu.iterationInScenario 0 source=console
INFO[0000] scenario.name: second-scenario scenario.iterationInTest: 0 vu.idInTest: 1 vu.iterationInScenario 0 source=console
INFO[0000] scenario.name: first-scenario scenario.iterationInTest: 1 vu.idInTest: 2 vu.iterationInScenario 1 source=console
INFO[0000] scenario.name: second-scenario scenario.iterationInTest: 1 vu.idInTest: 3 vu.iterationInScenario 0 source=console
INFO[0000] scenario.name: second-scenario scenario.iterationInTest: 2 vu.idInTest: 1 vu.iterationInScenario 1 source=console
INFO[0000] scenario.name: second-scenario scenario.iterationInTest: 3 vu.idInTest: 3 vu.iterationInScenario 1 source=console
The different values are documented:
scenario.IterationInTest
: The unique and zero-based sequential number of the current iteration in the scenario. It is unique in all k6 execution modes - in local, cloud and distributed/segmented test runs. However, while every instance will get non-overlapping index values in cloud/distributed tests, they might iterate over them at different speeds, so the values won’t be sequential across them.
vu.idInTest
: The globally unique (across the whole test run) identifier of the VU.
The difference between these is that scenario.IterationInTest
is scenario-based, while vu.idInTest
is for the global test (which can have one or more scenarios).
I hope this helps.
Cheers!