Check status 200 for batch requests

I am doing a batch request for a series of urls and when I do a check I get 0% for my checks but when I change the checks to check[0] I get 100%. Does anyone have an idea what’s going on?

import http from 'k6/http';

import { group, sleep, check } from "k6";

export let options = {

    stages: [

        { duration: "30s", target: 50 },

        { duration: "30s", target: 50 },

        { duration: "30s", target: 0 },

      ],

    thresholds: {

      'http_req_duration': ['avg < 5000'],

    },

};

export default function () {

    group(' elasticsearch', function () {

      group(':9200', function () {

         let responses = http.batch([

            ['GET', 'http://chartfa.........'],

            ['POST', 'http://10.1.11.2'],

            ['POST', 'http://10.1.11.2],

            ['POST', 'http://10.1.11.25],

            ['POST', 'http://10.1.11.25],

            ['POST', 'http://10.1.11.25],

            ['POST', 'http://10.1.11.2],

            ['POST', 'http://10.1.11.2'],

            ['GET', 'http://10.1.11.25],

            ['GET', 'http://10.1.11.25'],

            ['GET', 'http://10.1.11.25'],

            ['POST', 'http://10.1.11.2'],

            ['POST', 'http://10.1.11.25,

            ['POST', 'http://10.1.11.25],

            ['POST', 'http://10.1.11.25],

            ['POST', 'http://10.1.11.2'],

            ['POST', 'http://10.1.11.25],

            ['GET', 'http://10.1.11.254],

            ['POST', 'http://10.1.11.2'],

            ['POST', 'http://10.1.11.2'],

            ['POST', 'http://10.1.11.2'],

            ['POST', 'http://10.1.11.2'],

            ['POST', 'http://10.1.11.2'],

            ['POST', 'http://10.1.11.2'],

        

        ])
        //check repsonses doesn't work
        //check responses[0] does work
        check(responses, {

            'good to go': (r)=> r.status === 200

        })

        

    });

       
  })

  sleep(1)

}

Hi @ralex7 ,

As is documented in the http.batch documentation, it returns an array if you have called it with an array.
Then check just provides its first argument to w/e lambda/function you use to do the checks. So in your case when you just use responses you are checking if that array http.batch returns have a status equal to 200 … which it doesn’t so it fails.

So what you need to do is to iterate over the responses and call check with each of them:

responses.forEach(function(response) {
  check(response, { // use response here
          'good to go': (r)=> r.status === 200
   });
});

1 Like