Incremental loop until threshold is met

Hello,

Does k6 support any form of incremental looping until a condition is met?

For example, We are looking to find a rough estimate of the max v users before the average response time for a HTTP request is over 1000ms.

Is it possible to create a loop that will constantly run the same scenario, with an increased amount of V users until the average response rate is 1000ms?

Hi @AndreSC, welcome the community forum :tada:!

There is no such functionality, but you can use either the ramping-vus (also known as stages) or the ramping-arrival-rate executor to increase the number of VUs or started iterations respectively.

Here is an example for arrival-rate, the one for ramping-vus/stages is similar:

import http from 'k6/http';

function generateStages(min, max, step, duration) {
	var result = [];
	for (var i=min; i <=max; i+=step) {
		result.push({ target: i, duration: '0s' }) // jump to next target
		result.push({ target: i, duration: duration }) // keep it for duration
	}
	return result;
}

export let options = {
	discardResponseBodies: true,
	scenarios: {
		contacts: {
			executor: 'ramping-arrival-rate',
			startRate: 10,
			timeUnit: '1s',
			preAllocatedVUs: 50,
			maxVUs: 100,
			stages: generateStages(50, 200, 10, '1m'),
		},
	},
};
export default function () {
	http.get('https://test.k6.io/');
}

Here we even generate the stages for the arrival rate programmatically for easier experiment. You will still need to set some upper limit, but it’s fine.

You can read the documentation of the corresponding executors to better understand what they do and how to configure them further.

Hope this helps and good luck!

1 Like