No results for ""

Powered byAlgolia

HTTP Requests

Making HTTP Requests

When creating a new load test, the first thing you'll often do is define the HTTP requests that will be used to test your system. A simple example that just performs a GET request looks like this:

import http from 'k6/http';

export default function() {
  http.get('http://test.k6.io');
}

A slightly more complex request might be e.g. a POST request to authenticate on a site/service:

import http from 'k6/http';

export default function() {
  var url = 'http://test.k6.io/login';
  var payload = JSON.stringify({
    email: 'aaa',
    password: 'bbb',
  });

  var params = {
    headers: {
      'Content-Type': 'application/json',
    },
  };

  http.post(url, payload, params);
}

Available methods

Use the http module to perform all kinds of HTTP requests in your load tests.

Name Value
batch() Issue any type of HTTP request.
del() Issue an HTTP DELETE request.
get() Issue an HTTP GET request.
options() Issue an HTTP OPTIONS request.
patch() Issue an HTTP PATCH request.
post() Issue an HTTP POST request.
put() Issue an HTTP PUT request.
request() Issue multiple HTTP requests in parallel (like e.g. browsers tend to do).

HTTP Request Tags

k6 will automatically apply tags to your HTTP requests. These tags allow you to filter your results during analysis.

Name Description
name Defaults to URL requested
method Request method (GET, POST, PUT etc.)
status response status
url defaults to URL requested

Below you can see how a test result data point (the duration of an HTTP request) is logged, in JSON format, including the various tags mentioned above:

{
  "type": "Point",
  "metric": "http_req_duration",
  "data": {
    "time": "2017-06-02T23:10:29.52444541+02:00",
    "value": 586.831127,
    "tags": {
      "group": "",
      "method": "GET",
      "name": "http://test.k6.io",
      "status": "200",
      "url": "http://test.k6.io"
    }
  }
}

URL Grouping

By default, requests report the name tag with the value of the request URL. For URLs that contain dynamic parts, this might not be desirable since it can introduce a large number of unique URLs in the metrics stream. The below code shows a situation when you'll access 100 different URLs but may want them all reported using one single metric:

for (var id = 1; id <= 100; id++) {
  http.get(`http://example.com/posts/${id}`);
}

// tags.name=\"http://example.com/posts/1\",
// tags.name=\"http://example.com/posts/2\",

You can aggregate data from dynamic URLs by explicitly setting a name tag:

for (var id = 1; id <= 100; id++) {
  http.get(`http://example.com/posts/${id}`, {
    tags: { name: 'PostsItemURL' },
  });
}

// tags.name=\"PostsItemURL\",
// tags.name=\"PostsItemURL\",

Which would produce JSON output like the following:

{
    "type":"Point",
    "metric":"http_req_duration",
    "data": {
        "time":"2017-06-02T23:10:29.52444541+02:00",
        "value":586.831127,
        "tags": {
            "group":"",
            "method":"GET",
            "name":"PostsItemURL",
            "status":"200",
            "url":"http://example.com/1"
        }
    }
}

// and

{
    "type":"Point",
    "metric":"http_req_duration",
    "data": {
        "time":"2017-06-02T23:10:29.58582529+02:00",
        "value":580.839273,
        "tags": {
            "group":"",
            "method":"GET",
            "name":"PostsItemURL",
            "status":"200",
            "url":"http://example.com/2"
        }
    }
}

Note how the name is the same for the two data samples related to two different URLs. Filtering the results on tag name: PostsItemURL will give you a result set including all the data points from all the 100 different URLs.

Additionally, you can use the http.url (v0.16.0) wrapper to set the name tag with a string template value:

for (var id = 1; id <= 100; id++) {
  http.get(http.url`http://example.com/posts/${id}`);
}

// tags.name="http://example.com/posts/${}",
// tags.name="http://example.com/posts/${}",

Inside k6 Cloud Results

k6 Cloud Results's HTTP Table will show all the requests, on an aggregated level per URL.

cloud insights url table