I wanted to know if it’s possible some how to check the image resolution with K6.
Our tool allows us to do some transformations on an image, and we want to test how fast that service is.
I added below code in the K6 default function to be able to verify transformation took place. But I am running into an issue.
var img = new Image();
img.onload = function() {
console.log(this.width + 'x' + this.height);
}
img.src = '${__ENV.URL}/transform/${image}.jpg';
ReferenceError: Image is not defined
Is something like this currently possible with K6?
that’s not easily done with plain k6. The reason you’re getting that ReferenceError is because the Image() API is not part of the ECMAScript standard, which k6 exclusively supports.
Technically, you could work with the binary image data and manually decode the specific image format and extract the width and height from the image header. You’d need to consult the specification on how the image data is structured for your specific format, locate the the header and extract the metadata that you need. That’s a bit out of scope for what we can help you here, but it should be something like:
const response = http.get('https://example.com/image.png', { responseType: 'binary' });
const imgData = new Uint8Array(response.body);
// decode image data ...
You can find tutorials online for how to do this. See this and this for PNG, for example. Or you might want to try pure JS decoders, e.g. pngjs. Just keep in mind that any libraries you import shouldn’t depend on NodeJS or browser APIs to work with k6.
Alternatively, you could give xk6-browser a try. It’s a k6 extension that allows you to run JS directly in the browser. So you would be able to do something like:
Thanks for the thorough explanation much appreciated @imiric !
I will check them both. The XK6 browser looks really interesting for my use case, did not know this existed.
It works for me when I run it locally with one of the examples as shown on the docs from Github.
When I try to run this(which also comes from the docs: it will return an error
So the JS API docs are up-to-date with the latest stable version (v0.4.0), while the README on GitHub shows examples based on the in-development main branch, where we did this breaking import change. This will soon be released as v0.5.0, and the JS docs will be updated to match.