Context
Question
Where should I randomize both the image and de id that will be used in the requests? At "init context" or "vu context"?
Code considering "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),
})
});
}
Code considering "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 Given that you want it to be random - "vu context"
As explained in k6 test lifecycle the init context is executed once per VU (and at least 1 more before the test starts).
This means that if you do your random number generation in the init context you will get the same "random" number for each iteration of the different VUs. This still means that different VUs will have different random values they won't change between iterations if this is okay with your use case that is perfectly fine.
But I guess what you want is to constantly generate a new random id on each iteration and use the corresponding id and image. This though means that you will need to have an array of images generated in the init context as open
isn't available in vu code. so instead of open(....getRandomInt...)
in the vu code you should have images[getRandomInt(0,6)]
.
Also for the record each VU will get it's OWN copy of the images so this might be a problem with memory if they are large or you just don't have enough memory for the amount of VUs you want to use.