How to control the iteration inside default function?

Hi,

So inside the default function i want to execute different get requests for different iteration count. For ex - 10 iteration for the get request. 20 for the next post request etc. Can this be achievable? By default the default function gets executed n no. of times. I want to control the iterations for different code sections inside a single K6 script.
If not what could be other solution using only K6?

Hi @Braj,

You can’t do it with current k6 design. Code in default function, aka VU code, will be run over and over until the test finishes. I invite you to read Test lifecycle to get more understanding about script life cycle.

If your requests do not depend on each others, you can split them into 2 or more scripts.

If i split it into multiple scripts, is there any way i can run it from another K6 script. Not bash script. But JS code.

@Braj,
you can take a look at How to distribute VU's across different scenarios with k6 and use the suggestions there to make something like what you want.

For more specific workaround you will need to provide more information :wink:

1 Like

So the scenario is like this- I have three urls - googlecom, microsoft.com and k6.io. To each one of the url i want to iterate respectively - X, Y and Z times. Can it be achieved in a single script?

@Braj if you want specifically to do X+Y+Z iterations on a single VU - yes. Otherwise … not so much.
To do it on a single VU you will need to:

  1. configure iterations to be equal to X + Y + Z
  2. In your script have:
if (__ITER < X) {
// do requests to google.com
} else if (__ITER < X + Z) {
// do requests to microsoft.com
} else {
// do requests to k6.io
}

After the new executors are merged and released this will be achievable even between VUs. But at the moment all iterations are shared so if you have 2 VUs and 10 iterations - 1 of the VU can do 7 iterations and the other 3 and there is no way to know this and share this information between the VUs.

1 Like

@Braj,

I’m new to k6, but I can suggest you to do :slight_smile:
I have created separate js file (recordings.js) using ts (type script) with all the scripts that you want to execute

//---
import s0 from '../out/profile1.js';
import s1 from '../out/profile2.js';

export let recordings = [
   {key:'profile1',method:s0},
   {key:'profile2',method:s1}
]; 

in my main script

//---
import { recordings } from '../out/recordings.js';

export default function() {
  recordings.map(function(x,i){
    x.method();
 });
});

Hope this would help you

The multi-scenario capabilities of the new k6 v0.27.0 should make this much more natural: