语境
- 我正在构建要由 K6 工具加载和执行的 javascript 文件。
- 它将用于压力和尖峰测试。
- 我的 POST 请求将包含 1 个图像和 1 个 ID
- 我想在 7 个已知选项中使用随机图像
- 我想随机生成id
问题
我应该在哪里随机化将在请求中使用的图像和 de id?在“init context”还是“vu context”?
考虑“初始化上下文”的代码
let rand_id = getRandomInt(10000,99999)
let image = open("face"+getRandomInt(0,6)+".jpg","b")
export default function() {
group("post_request", function() {
http.post("https://my_api", {
"id": rand_id,
"image": http.file(image),
})
});
}
考虑“vu context”的代码
let images = []
for (i=0; i <= 6; i++) {
images.push(open("face"+i+".jpg","b"))
}
export default function() {
group("post_request", function() {
http.post("https://my_api", {
"id": getRandomInt(10000,99999),
"image": http.file(open(images[getRandomInt(0,6)],"b")),
})
});
}