1

I am new to k6 & newish to JS. I am trying to read a list from a flat file that I exported from a column in DB. I want to open this file & loop through the list appending each item as a query param for my HTTP call. I cannot figure out how to proceed.

So as an example if this is my file:

employees.txt

01111
02222
06666
04444
09999

& here is my k6 script (perf-employee.js)

import http from "k6/http";
import { sleep } from "k6";

export let options = {
  vus: 3,
  duration: "5s" 
};

var data = {some type of parsing her?}.open("./employees.txt");

export default function() {
  http.get("http://www.example.com/employee?employee_num={number-here}“);
  sleep(1);
};

Any guidance on the direction to proceed would be much appreciated.

OceanBlue
  • 9,142
  • 21
  • 62
  • 84

1 Answers1

4

open() returns a simple string (or a binary if you use the b flag), so you can parse it by just transforming it into an array (each line in it's own array row) with the JavaScript String.split() method. Or if you want to read a more complex data file, use JSON and JSON.parse() method to transform it directly into a JavaScript object - take a look at the first example in the link above.

Then by using k6's execution context variables you can do something like this:

import http from "k6/http";
import { sleep } from "k6";

var data = open("./employees.txt").split(/\r?\n/);

export let options = {
    vus: 3,
    duration: "5s"
};


export default function () {
    var employee = data[__ITER % data.length];
    console.log(`VU ${__VU} on iteration ${__ITER} has employee ID ${employee}...`)
    http.get(`http://www.example.com/employee?employee_num=${employee}`);
    sleep(1);
};

you should see something like this as the script output:

INFO[0001] VU 2 on iteration 0 has employee ID 01111... 
INFO[0001] VU 1 on iteration 0 has employee ID 01111... 
INFO[0001] VU 3 on iteration 0 has employee ID 01111... 
INFO[0002] VU 2 on iteration 1 has employee ID 02222... 
INFO[0002] VU 1 on iteration 1 has employee ID 02222... 
INFO[0002] VU 3 on iteration 1 has employee ID 02222... 
INFO[0003] VU 2 on iteration 2 has employee ID 06666... 
INFO[0003] VU 3 on iteration 2 has employee ID 06666... 
INFO[0003] VU 1 on iteration 2 has employee ID 06666... 
INFO[0004] VU 2 on iteration 3 has employee ID 04444... 
INFO[0004] VU 1 on iteration 3 has employee ID 04444... 
INFO[0004] VU 3 on iteration 3 has employee ID 04444... 
INFO[0005] VU 2 on iteration 4 has employee ID 09999... 
INFO[0005] VU 1 on iteration 4 has employee ID 09999... 
INFO[0005] VU 3 on iteration 4 has employee ID 09999... 
na--
  • 1,016
  • 7
  • 9