How to load a csv file

Is there a way to load a csv file for use inside the load script. I tried to browserify csvtojson npm package. But after browserify i am getting errors inside the browserified javascript file.

Hi Braj,

csvtojson uses async JS code and promises, at least since version 2.x, which unfortunately is not currently supported by k6. See issue #882 for details.

You might be able to get v1 of it working, although I found another CSV parsing library which seems to be simpler and works well out-of-the-box with k6 (without Browserify): https://github.com/mholt/PapaParse

You can use it like so:

import Papa from "./papaparse.min.js";

const data = open("./data.csv");

export function setup() {
    return Papa.parse(data, { header: true });
}

export default function(results) {
    for (let row of results.data) {
        console.log(`Name: ${row.firstName} ${row.lastName}`)
    }
};

Also note that parsing CSV is relatively straightforward in plain JavaScript, so if you can avoid adding another dependency and write it yourself, that would be preferable in order to reduce memory usage during the test’s runtime and improve overall performance.

Hello!
I use example using Papa Parse to parse a CSV file of username/password pairs to login to the test.k6.io , which is specified on the page https://k6.io/docs/examples/data-parameterization/.

Below is how to randomly take a username/password:

// Pick a random username/password pair
let randomUser = csvData[Math.floor(Math.random() * csvData.length)];
console.log('Random user: ', JSON.stringify(randomUser));

How can I get each virtual user to sequentially take username/password from a csv file, not randomly?

Hi @Sholpan,

that’s a separate question and you should’ve posted it in a new thread, or searched the forum for similar questions first.

Here are some past discussions for how to achieve what you want: