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    Documentation

Search results for "{{ search.query }}"

No results found for "{{search.query}}". 
View All Results

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.loadimpact.com");
};

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.loadimpact.com/login";
  var payload = JSON.stringify({ email: "aaa", password: "bbb" });
  var params =  { headers: { "Content-Type": "application/json" } }
  http.post(url, payload, params);
};

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

Issue multiple HTTP requests in parallel (like e.g. browsers tend to do).

Issue an HTTP CONNECT request.

Issue an HTTP DELETE request.

Issue an HTTP GET request.

Issue an HTTP OPTIONS request.

Issue an HTTP PATCH request.

Issue an HTTP POST request.

Issue an HTTP PUT request.

Issue any type of HTTP request.

Issue an HTTP TRACE request.

HTTP request tags

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

  • 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.loadimpact.com",
      "status":"200",
      "url":"http://test.loadimpact.com" 
    }
  }
}

Aggregating results for HTTP requests

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/group the results here 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" }
  }
}
{ "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/${}",

HTTP requests