Hi @Explorit
Welcome to the community forum
Without more details of your case, I would say maybe it does not matter if it’s the same VU that executes the flows, and that you might get a similar result combining scenarios or with other mechanisms. If what you need to achieve is a “random” number of executions for different flows/scenarios, and not that each VU changes the flow randomly after each iteration.
If you haven’t read those yet, I recommend a read of k6 testing guides depending on your use case:
That said, I’m thinking you can achieve what you described with a script similar to the one below (or any alternatives, like a switch/case inside the default function):
import exec from 'k6/execution';
import { sleep } from 'k6';
import { randomIntBetween } from 'https://jslib.k6.io/k6-utils/1.2.0/index.js';
export const options = {
vus: 2,
duration: "10s",
};
var flows = [flow_zero, flow_one, flow_two];
function flow_zero() {
console.log('VU: ', exec.vu.idInTest, 'Iteration: ', exec.vu.iterationInInstance, 'flow 0');
}
function flow_one() {
console.log('VU: ', exec.vu.idInTest, 'Iteration: ', exec.vu.iterationInInstance, 'flow 1');
}
function flow_two() {
console.log('VU: ', exec.vu.idInTest, 'Iteration: ', exec.vu.iterationInInstance, 'flow 2');
}
export default function () {
let i = randomIntBetween(0, flows.length - 1);
flows[i]();
sleep(1);
}
Executing this, we get random flows executed randomly on each VU iteration.
scenarios: (100.00%) 1 scenario, 2 max VUs, 40s max duration (incl. graceful stop):
* default: 2 looping VUs for 10s (gracefulStop: 30s)
INFO[0000] VU: 2 Iteration: 0 flow 2 source=console
INFO[0000] VU: 1 Iteration: 0 flow 1 source=console
INFO[0001] VU: 2 Iteration: 1 flow 2 source=console
INFO[0001] VU: 1 Iteration: 1 flow 0 source=console
INFO[0002] VU: 2 Iteration: 2 flow 1 source=console
INFO[0002] VU: 1 Iteration: 2 flow 1 source=console
INFO[0003] VU: 1 Iteration: 3 flow 2 source=console
INFO[0003] VU: 2 Iteration: 3 flow 0 source=console
INFO[0004] VU: 1 Iteration: 4 flow 2 source=console
INFO[0004] VU: 2 Iteration: 4 flow 2 source=console
INFO[0005] VU: 1 Iteration: 5 flow 1 source=console
INFO[0005] VU: 2 Iteration: 5 flow 2 source=console
INFO[0006] VU: 1 Iteration: 6 flow 0 source=console
INFO[0006] VU: 2 Iteration: 6 flow 2 source=console
INFO[0007] VU: 2 Iteration: 7 flow 2 source=console
INFO[0007] VU: 1 Iteration: 7 flow 0 source=console
INFO[0008] VU: 2 Iteration: 8 flow 1 source=console
INFO[0008] VU: 1 Iteration: 8 flow 2 source=console
INFO[0009] VU: 2 Iteration: 9 flow 2 source=console
INFO[0009] VU: 1 Iteration: 9 flow 1 source=console
data_received........: 0 B 0 B/s
data_sent............: 0 B 0 B/s
iteration_duration...: avg=1s min=1s med=1s max=1s p(90)=1s p(95)=1s
iterations...........: 20 1.997874/s
vus..................: 2 min=2 max=2
vus_max..............: 2 min=2 max=2
running (10.0s), 0/2 VUs, 20 complete and 0 interrupted iterations
default ✓ [======================================] 2 VUs 10s
I hope this helps.
Cheers!