Bulk load files

I want to open bulk of files which I need to get as an environment variables.

So I want to execute like: k6 run --env IMAGES=[image1.png, image2.png]

And my script has:

function toDo(){
  for(let i = 0; i < __ENV.IMAGES.length; i++){
      open(__ENV.IMAGES[i], 'b');
  }
}
export default function (data) {
   toDo();
}

But it’s not possible to use open() not in global scope and I’m not sure that I can add environment variable as array.

Please suggest solution.

Hi @AlexCode, welcome to the forum :slight_smile:

What you want to do is problematic for a couple of reasons, but before I get to that, the reason you can’t call open() from the default() function is because that would be difficult to achieve in distributed execution, but also prohibitively expensive to run in a function that could be called thousands of times during a test run, and would impact the overall test. k6 could probably cache and reuse the result so that the test is not making disk I/O on each iteration, but it’s not a trivial problem to solve.

That said, while you can’t pass arrays in environment variables, you could pass a comma-separated list, e.g. k6 run --env IMAGES=image1.png,image2.png, and then split the env var and load all images globally like this:

let imagePaths = __ENV.IMAGES.split(',');
let images = {};
for (let i = 0; i < imagePaths.length; i++){
  images[imagePaths[i]] = open(imagePaths[i], 'b');
}

This would make the images object available to your default function, which you could then use like this:

export default function () {
  let data = {
    file: http.file(images['image1.png'], 'image1.png'),
  };
  let res = http.post('https://example.com/upload', data);
}

However, keep in mind the following:

  • The init context is evaluated separately for each VU, and the images object will be duplicated in all of them, leading to high RAM usage depending on the amount of VUs and images you’re loading. Since v0.27.0 you can more easily split this data across VUs (see Handling bigger data files), so you could for example load a unique subset of images per VU, if that fits your use case.
  • You might run into binary handling issues as mentioned in #1020 depending on what you want to do with the binary data. If it’s just for this uploading use case the above should work fine, but anything more complex will likely have to wait for #1020.

HTH,

Ivan

Thanks, I’ve done in such way you suggested