k6

A modern load testing tool, using Go and JavaScript

"like unit testing, for performance"

k6 is a modern load testing tool, building on Load Impact's years of experience. It provides a clean, approachable JavaScript scripting API, distributed and cloud execution, and orchestration via a REST API.

Get Started    Guides

batch( requests )

Batch multiple HTTP requests together, to issue them in parallel over multiple TCP connections.

Parameter
Type
Description

requests

array | object

An array or object containing requests, in string or object form.

Returns:

Type
Description

array | object

An array or object containing Response objects.

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

export default function() {
  let responses = http.batch([
    "http://test.loadimpact.com",
    "http://test.loadimpact.com/style.css",
    "http://test.loadimpact.com/images/logo.png",
  ]);
  check(responses[0], {
    "main page status was 200": res => res.status === 200,
  });
};

Batching three URLs for parallel fetching


Request objects

You can also use objects to hold information about a request. Here is an example where we do that in order to send a POST request, plus use custom HTTP headers by adding a Params object to the request:

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

export default function() {
  let req1 = {
    method: "GET",
    url: "http://httpbin.org/get",
  };
  let req2 = {
    method: "GET",
    url: "http://test.loadimpact.com",  
  };
  let req3 = {
    method: "POST",
    url: "http://httpbin.org/post",
    body: {
      hello: "world!",
    },
    params: { headers: { "Content-Type": "application/x-www-form-urlencoded" } }	
  };
  let responses = http.batch( [req1, req2, req3] );
  // httpbin.org should return our POST data in the response body, so
  // we check the third response object to see that the POST worked.
  check(responses[2], {
    "form data OK": (res) => JSON.parse(res.body)["form"]["hello"] == "world!",
  });
};

Note that the requests in the example above may happen in any order, or simultaneously. There is no guarantee that e.g. req1 will happen before req2 or req3


Named requests

Finally, you can also send in named requests by using an object instead of an array as the parameter to http.batch(). In the following example we do this, and we also show that it is possible to mix string URLs and request objects

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

export default function() {
  let requests = {
    "front page": "https://loadimpact.com",
    "features page": {
      method: "GET", 
      url: "https://loadimpact.com/features",
      params: { headers: { "User-Agent": "k6" } },
    },
  };
  let responses = http.batch(requests);
  // when accessing results, we use the name of the request as index
  // in order to find the corresponding Response object
  check(responses["front page"], {
    "front page status was 200": res => res.status === 200,
  });
};