k6

Using unique parameterized data with multiple scenarios


I have a k6 load test script that has multiple scenarios. One scenario needs parameterized unique data to be used. To simpify, one scenario simulates load by (anonymous) viewers of the data while another scenario simulates upserting data by logged in users.

I have an issue in properly fetching parameterized data from json for the logged in user scenario. If I only run this scenario, everything works fine, but once the viewing scenario is added, the indexing fails.

My parameterized data is like

{
  ddd: [
    {a: 1, b: 2},
    {a: 3, b: 4}
  ]
}

And in the script I access this first with a SharedArray in the init phase as

const ddd = new SharedArray(
  'ddd',
  () => JSON.parse(open('./data.json')).ddd
);

and later in the test functions as

const d = ddd[vu.idInTest - 1];

After I've added second scenario, the indexing gets mixed and the second scenario picks up the data. I guess this makes sense, since the VU IDs are shared between scenarios.

I've tried scenario.iterationInTest to access the data, and even added an empty list to data file for this, but still it's mixed.

How can I have separated data per scenario without them getting mixed?


Solution

  • If you simply want to "separate" the data for different scenarios, introduce another nesting level:

    {
      ddd: [
        {
          scenario1: [
            {a: 1, b: 2},
            {a: 3, b: 4}
          ],
          scenario2: [
            ...
          ]
        }
      ]
    }
    

    In your test functions, you would then access the respective scenario data:

    const d = ddd[0].scenario1[vu.idInTest - 1];
    

    (and maybe use the modulo operator to not read beyond the array's end: ddd[0].scenario1[(vu.idInTest - 1) % ddd[0].scenario1.length]).

    Depending on how you want to select the data for each VU or each iteration, you might want to use a different property from the k6/execution module.