k6

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.