使用随机图像的 K6 压力测试

K6 Stress Test with shuffled images

上下文

问题

我应该在哪里随机化将在请求中使用的图像和 de id?在 "init context" 或 "vu context"?

代码考虑 "init 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")),
        })
    });
}

tl;dr 鉴于您希望它是随机的 - "vu context"

k6 test lifecycle 中所述,初始化上下文每个 VU 执行 一次(在测试开始前至少执行 1 次)。

这意味着如果您在 init 上下文中生成随机数,您将在不同 VU 的每次迭代中获得相同的 "random" 数字。这仍然意味着不同的 VU 将具有 不同的 随机值,如果您的用例完全没问题,它们不会在迭代之间改变。

但我猜你想要的是在每次迭代中不断生成一个新的随机id并使用相应的id和图像。这虽然意味着您将需要在初始化上下文中生成一组图像,因为 open 在 vu 代码中不可用。所以你应该 images[getRandomInt(0,6)].

而不是 vu 代码中的 open(....getRandomInt...)

此外,每个 VU 都会获得它的 OWN 图像副本,因此如果它们很大或者您没有足够的内存,这可能是内存问题为您要使用的 VU 数量。