Search code examples
k6

Duplicate entity error when creating entities with multiple VUs in k6


I am quite new to k6 and we have the following edge case.

We need to create a big amount of data for a reasonably good time. The entity that we are trying to create has an array of Uids(permissionsUids) as a payload for which there cannot be a duplicate permutation and I am getting the permissions(set of 10) via a GET request and generating all the possible permutations(1023) which are returned as an array of arrays.

The problem is that due to the concurrency sometimes the VUs try to create the entity with data that is already created by another VU and I think this might be normal and how k6 lifecycle works as this is quite an edge case but I wanted to be sure. I have the following options in k6:

export const options = {
    vus: 100,
    iterations: 1023
};

My setup method gets and returns all the combinations in an array of arrays.

And in my default function I did the iterationsPerVU and uniqueIndex calculations in order to narrow the failed requests down to being between 2%-5%:

    const iterationsPerVU = Math.floor(options.iterations / options.vus);
    const uniqueIndex = (__VU - 1) * iterationsPerVU + __ITER;

With the uniqueIndex I am just getting the element from the array of arrays variable and trying to create the entity with it.

Again I guess this might be an edge case and k6 might not be the tool for this but would be glad if there is an answer to this either being beneficial one or not.

Thank you for your time reading this, have a nice day!


Solution

  • scenario.iterationInTest of k6/exeuction gives you access to the unique iteration index in the current scenario.

    You can then easily index into a prepopulated shared array:

    import { SharedArray } from 'k6/data';
    import { scenario } from 'k6/execution';
    
    export const options = {
        vus: 100,
        iterations: 1023,
    };
    
    const data = new SharedArray('numbers', () => {
        return [...Array(1023).keys()];
    });
    
    export default function() {
        console.log(`item: ${data[scenario.iterationInTest]}`);
        console.log(`VU: ${__VU}`);
        console.log(`ITER: ${__ITER}`);
    }
    

    If you check the logs, you will see that each item of the array is printed exactly once.