Iterate CSV file records only once

Hello, I have a csv with records which can be used only 1 time. Once a record was used it becomes unuseful due it changes its original state.
So, I need help in k6 to iterate a CSV using each record only once, having more than 1 VU?
I’m using papaparse and a ShareArray object to load and store the CSV records.

Thanks

It is a tricky one. If you know JMeter you would know that it handles csv with no problem. Next request is simply taking next record form your csv and you don’t have to worry which user uses what record and you know that each record can be used only once.
Papaparse provides you a “list”. The solution, or a workaround, I came up with was math. :smiley: It can be adjusted to your specific type of run (stages, iterations, duration…).
K6 provides you two crucial things: __VU and __ITER (see definition here). K6 is running async requests so it is troublesome to handle csv like JMeter does. What I came up with is this:
Let assume we have 100 records and we want to run 9 VU. VUs are numbered from 1 and ITERs are numbered starting from 0. So:
VU 1 will take records 0-10,
VU 2 will take records 11-21,
…,
VU 9 will take records 88-98.

let maxIter = 11 // you should pass that value while running the test, it is maximum number of records a single VU can use
if (__ITER < maxIter) {
    record = csvData[(__VU - 1) * maxIter + __ITER]
    *request using record*
}

The downsize is each VU makes up to 11 requests. Maybe I will inspire you and you will change it to something better. :slight_smile:

1 Like

Hi, here is an update and solution for me. Using this code I could iterate data in order without repeating data.
This is possible using a variable which i called MAX_VUS where i set the number of VUs (the same value that --vus has)

import http from “k6/http”;
import { sleep } from “k6”;

export default function() {
http.get(“http://test.loadimpact.com”);
let index = __VU + __ITER * __ENV.MAX_VUS;
console.log(--> ${index});
};

k6 run -e MAX_VUS=3 --vus 3 --duration 10s instride/part2/testCSV3.js

The logic:
index=__VU+__ITER*__ENV.MAX_VUS-1
// 1= 1 + 0 * 3
// 2= 2 + 0 * 3
// 3= 3 + 0 * 3
// 4= 1 + 1 * 3
// 5= 2 + 1 * 3
// 6= 3 + 1 * 3
// 7= 1 + 2 * 3
// 8= 2 + 2 * 3
// 9= 3 + 2 * 3

Thanks for all the answers

2 Likes

jteruya, note that you do not get an index of “0”. You are “wasting” one record. Maybe change __VU to __VU - 1?

1 Like

You are right !! Shared Array index begins in 0