Context
问题
我应该在哪里随机化将在请求中使用的图像和标识?在“初始上下文”还是“ vu上下文”?
考虑“初始上下文”的代码
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上下文”的代码
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("face"+getRandomInt(0,6)+".jpg","b")),
})
});
}
tl; dr假设您希望它是随机的-“ vu context”
如k6 test lifecycle中所述,初始化上下文按每个VU 这意味着,如果您在init上下文中进行随机数生成,则对于不同VU的每次迭代,您将获得相同的“随机”数。这仍然意味着,如果您的用例很好,那么不同的VU将具有 differentopen
。因此,您应该在[v0]代码中不要使用open(....getRandomInt...)
。
也记录下来,每个VU都会得到图像的
OWN
副本,因此,如果图像很大,或者您没有足够的内存来容纳要使用的VU数量,这可能是内存问题。