-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Helper for conditional percent statements #239
Comments
After thinking about it, I feel like a modulo on the VU ID ( You could do something rather overblown where some VUs get certain variables assigned, but honestly, it'd accomplish the exact same thing. |
This proposal will mostly be covered by the changes in #1007 |
Closing this, since the just-merged #1007 implements native support for multi-scenario tests, just in another way |
@na--
with let's say only 2 VUs (or scaling up or ramping up number of VUs) and an nonfixed number of total requests? Or is this ones of the things that isn't covered (as you wrote |
As I explained in the community forum, VUs can't be shared between scenarios. But in a single scenario, you can easily implement a random weighted distribution of its iteration. Posting the same example implementation of a "randomSwitch" function here as well, in case someone stumbles on this issue and not the forum thread: import { sleep } from 'k6';
function weightedSwitch(weightedFuncs) {
var funcIntervals = new Array(weightedFuncs.length)
var weightSum = 0;
for (var i = 0; i < weightedFuncs.length; i++) {
funcIntervals[i] = {
start: weightSum,
end: weightSum + weightedFuncs[i][0],
func: weightedFuncs[i][1],
}
weightSum += weightedFuncs[i][0];
}
if (Math.abs(weightSum - 1) > 0.0001) {
throw new Error('the sum of function weights should be 1 (100%), but is ' + weightSum);
}
return function (val) {
var guess, min = 0, max = funcIntervals.length - 1;;
while (min <= max) {
guess = Math.floor((max + min) / 2);
if (val >= funcIntervals[guess].end) {
min = guess + 1;
} else if (val < funcIntervals[guess].start) {
max = guess - 1;
} else {
return funcIntervals[guess].func;
}
}
}
}
export let options = {
duration: '1m',
vus: 2,
};
var getFunction = weightedSwitch([
[0.1, () => "scenario 0 (10%)"],
[0.1, () => "scenario 1 (10%)"],
[0.21, () => "scenario 2 (21%)"],
[0.39, () => "scenario 3 (39%)"],
[0.199, () => "scenario 4 (19.9%)"],
[0.001, () => "scenario 5 (0.1%)"],
])
export default function () {
var rand = Math.random();
var f = getFunction(rand);
console.log(`VU ${__VU}, iter ${__ITER}, rand ${rand.toFixed(4)} executed ${f()}`);
sleep(1);
} I don’t make any guarantees that I don’t have an off-by-one error in the binary search code or something like that, I haven’t tested this code much, but it seems to work well enough… Running it should result in something like this:
|
We discussed in the Slack channel the case of executing some flow only for a specific percent of the current execution users.
These gists provide some use cases:
https://gist.github.com/ppcano/14d66b7b3ec6993ed63eaed8f2ebecae
https://gist.github.com/ppcano/ed545a322c1ed4b6ee61f0fe34ac6694
A helper to manage this case may be useful. Below an example to illustrate the case (not a syntax proposal):
The text was updated successfully, but these errors were encountered: