open()
返回一个简单的字符串(如果您使用该标志,则返回一个二进制文件),因此您可以通过使用 JavaScript方法b
将其转换为一个数组(它自己的数组行中的每一行)来解析它。String.split()
或者,如果您想读取更复杂的数据文件,请使用 JSON 和JSON.parse()
方法将其直接转换为 JavaScript 对象 - 请查看上面链接中的第一个示例。
然后通过使用 k6 的执行上下文变量,您可以执行以下操作:
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);
};
你应该看到类似这样的脚本输出:
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...