How to run requests in parallel within same VU?

I’m new to k6 and “inherited” some k6 code from a colleague.

The code looks basically like this:

import { uuidv4 } from "https://jslib.k6.io/k6-utils/1.0.0/index.js"
const testsToRun = [(data: K6Settings) => {
    group("Progression performance ", () => {
		if (__ITER == 0) {
			userId = uuidv4()
		}
		const response = http.post(`http://localhost:3012/my-route`, JSON.stringify({ userId }))
		check(response, { "status was 200": (r) => r.status === 200 })
	})
}]
export default function (data: any) {
	testsToRun.forEach((test: (data: any) => void) => {
		test(data)
	})
}

I think, the crucial part here is that on the first iteration of that Virtual User, we set a uuid which is then sent to our backend as userId.
In my case, performance testing only makes sense if our backend receives requests on that very route with the same userId in parallel (i.e. the second request arrives before the first one finished).
But that’s not the case right now. Requests come in sequentially (first one finishes, then second one arrives).

How can I solve that within one VU?

If I need to use multiple VUs for this: Not all VUs should share the same userId, only some of them. How would I do that?

Hi @cis - Welcome to the community forum! :tada:

You can’t achieve that with one VU. If you want to have multiple parallel request you have to implement you test to use more VUs for request to be executed concurrently.

One way to do this is using scenarios and having the same code executed in different scenarios (learn more about scenarios here), i.e:

  • Three scenarios in total
    • Two scenarios sharing the same userId
    • The other scenario using a different userId

Another way to do this would be, generate two userIds then select some arbitrary VUs (i.e., 2, 4, 6, 8, 9; if you use more than 10VUs) and then perform a check if the running VU is equal with any from the above list → user userId-1, and if not use userId-2.

I hope this is not too complicated and you get the idea.

The below topic is great and might be very beneficial to you.

Hi @cis you can use per-vu-iteration as an executer. Then it will work parallel

If you want some VUs use only same id and the others not. You can develop a code below;

if (__VU==1 || __VU==2) {

// Do something with same user id
}
else {
// Do something with different user id
}

Don t forget __VU call the virtual user ids. For example you set 4 VUs in options. Then __ VU calls 1,2,3,4 as an id in the parallel.Vurtial user id every time starts with 1 and continue sequential. So you can use the this logic

But don t forget , according to me this effects the test . This makes your k6 test slow. I guess this is only solution for u . It depands your API s structure