Skip to main content

Simple GET Request

The most basic k6 test makes a single HTTP GET request:
import http from 'k6/http';

export default function () {
  http.get('https://quickpizza.grafana.com');
};

Load Test with Stages

Use stages (ramping) to control the number of virtual users over time:
import http from "k6/http";
import { check } from "k6";

/*
 * Stages (aka ramping) is how you, in code, specify the ramping of VUs.
 * That is, how many VUs should be active and generating traffic against
 * the target system at any specific point in time for the duration of
 * the test.
 * 
 * The following stages configuration will result in up-flat-down ramping
 * profile over a 20s total test duration.
 */ 

export let options = {
    stages: [
        // Ramp-up from 1 to 5 VUs in 10s
        { duration: "10s", target: 5 },

        // Stay at rest on 5 VUs for 5s
        { duration: "5s", target: 5 },

        // Ramp-down from 5 to 0 VUs for 5s
        { duration: "5s", target: 0 }
    ]
};

export default function() {
    let res = http.get("http://httpbin.org/");
    check(res, { "status is 200": (r) => r.status === 200 });
}
The stages configuration creates a realistic load pattern: ramp up, sustain load, then ramp down.

HTTP Methods

k6 supports all standard HTTP methods:
import http from "k6/http";
import { check } from "k6";

let res = http.get("http://httpbin.org/get?verb=get");
check(res, {
    "status is 200": (r) => r.status === 200,
    "is verb correct": (r) => r.json().args.verb === "get",
});

Working with JSON

Sending and parsing JSON data:
import http from "k6/http";
import { check } from "k6";

export default function() {
    // Send a JSON encoded POST request
    let body = JSON.stringify({ key: "value" });
    let res = http.post(
      "http://httpbin.org/post", 
      body, 
      { headers: { "Content-Type": "application/json" }}
    );

    // Use JSON.parse to deserialize the JSON (instead of using the r.json() method)
    let j = JSON.parse(res.body);

    // Verify response
    check(res, {
        "status is 200": (r) => r.status === 200,
        "is key correct": (r) => j.json.key === "value",
    });
}

Build docs developers (and LLMs) love