Understanding how batch and VU's work together

Let me apologies in advance is someone has asked this question already and I missed it in my searches. But I am trying to understand how VU’s work with regards to the code in the default function, and how that might affect requests when using batch.

From what I have read, I am getting the impression that if I run my script with --vus 2, then all of the code in the default function will be run by two separate VU’s. Am I correct in my understanding?

The problem I am having is that the code that I have written has roughly three parts to it, a create, update and delete. I can choose the number of updates that take place using environment variables. On top of this, the code is reading an input file that contains a list of ID’s, so each part of the test (create, update, delete) uses these ID’s to build an array of requests, which is then passed into the batch function.

So, am I to understand that using more than one VU means that the batch statement will be executed n times, n being the number of VU’s? I am trying to understand this because I need to ensure that each request in the batch is only sent once.

Any guidance or point in the right direction would be greatly appreciated. **Disclaimer - I am a noob at k6 and java script…if you haven’t already guessed it. :smiley:

Yes and yes :slight_smile: Each VU is a separate independent JavaScript runtime and when you have --vus N for N>1, every VU will run your code independently, in parallel.

This makes things a bit complicated. k6 has the __VU and __ITER execution context variables, to potentially allow you to choose non-overlapping portions of that array for different VUs. Depending on the complexity of your use case, that might or might not be enough, so we’re working on an even more flexible API that would hopefully land soon.

Hi Ned,

Thanks for getting back to me on this. I have changed my code around so that I could use the following:

let subscribersPerVu = Math.floor(common.numberOfSubscribers / options.vus);
let subscriberVuOffset = subscribersPerVu * (__VU - 1)
common.subscriberIdx = subscriberVuOffset + (__ITER * common.numberOfRequest) % subscribersPerVu;

This seems to work pretty good, however I need to debug an issue that seems to increment one of my counters incorrectly. For some reason, even though I set numberOfSubscribers to say 10…its incrementing well into 13. :expressionless: sooo…time for some strategic “console.log” to debug this. :wink:

Cheers Man!