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...