k6

Checks by Looping Over Batch Results

Hello k6 community,

I understand how to perform check() when using a single request result (e.g. http.get()). However, when using http.batch(), is it possible to loop over the array elements to check each one? Instead of having to check() each array element individually.

Instead of this:

const batchResponses = http.batch([
    ["GET", "https://www.example.com/a.html"],
    ["GET", "https://www.example.com/b.html"],
    ["GET", "https://www.example.com/c.html"]
]);
check(batchResponses[0], { "Page a HTTP status 200": res => res.status === 200, });
check(batchResponses[1], { "Page b HTTP status 200": res => res.status === 200, });
check(batchResponses[2], { "Page c HTTP status 200": res => res.status === 200, });

Something like this:

const batchResponses = http.batch([
    ["GET", "https://www.example.com/a.html"],
    ["GET", "https://www.example.com/b.html"],
    ["GET", "https://www.example.com/c.html"]
]);
batchResponses.forEach(element => {
    check(element, { "All HTTP status 200": (r) => r.status == 200 });
});

I tested the above code and the error is TypeError: Object has no member 'forEach'.

Thank you for your help!
Steve

As explained in this issue, currently the result of batch is an Object, with keys such as 0, 1, 2, 3 … . We do intend on fixing this but probably it’s going to be done in the a future version of the whole http api as the current one has some … problems :frowning: .

As a workaround you can see the script below … unfortunately you need to provide the length of the batch ;(
edit: you can use Object.keys(batchResponses).length

import http from "k6/http";
import { check } from "k6";

export default function() {
    const batchResponses = http.batch([
            ["GET", "https://www.example.com/a.html"],
            ["GET", "https://www.example.com/b.html"],
            ["GET", "https://www.example.com/c.html"]
    ]);

    for (let i = 0; i < 3; i++) {
        let element = batchResponses[i];
        check(element, { "All HTTP status 200": (r) => r.status == 200 });
        console.log(element.url);
    }
}

Thank you! This is definitely helpful.