我是k6的新手,而对JS则是新手。我正在尝试从DB中的列导出的平面文件中读取列表。我想打开此文件并遍历整个列表,并将每个项目附加为HTTP调用的查询参数。我不知道如何进行。
例如,如果这是我的文件,
employees.txt
ExecutionPhase.BEFORE_TEST_METHOD
&这是我的k6脚本( perf-employee.js )
01111
02222
06666
04444
09999
任何关于前进方向的指导将不胜感激。
答案 0 :(得分:2)
open()
returns一个简单的字符串(如果使用b
标志,则为二进制),因此您可以通过将其转换为数组来解析它(每一行都是自己的)数组行)和JavaScript String.split()
方法。或者,如果您想读取更复杂的数据文件,请使用JSON和JSON.parse()
方法将其直接转换为JavaScript对象-请查看上面链接中的第一个示例。
然后使用k6的execution context variables,您可以执行以下操作:
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...