load-testing

K6 Load Testing

Example test:

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

export let options = {
  stages: [
    // { duration: "4m", target: 500 }, // simulate ramp-up of traffic from 1 to 500 users over 4 minutes
    // { duration: "4m", target: 0 }, // ramp down to 0 users
    { duration: "2m", target: 2000 },
    { duration: "2m", target: 0 },
  ],
  thresholds: {
    http_req_duration: ["p(99)<1500"], // 99% of requests must complete below 1.5s
    http_req_failed: ["rate<0.01"], // http errors should be less than 1%
  },
};

export default () => {
  let timestamp = Date.now();

  let res = http.get(`https://example.com/image.jpg?timestamp=${timestamp}`,
  {
    tags: { name: "image_request" }, // Group requests under a single metric series, which is necessary since
                                     // timestamp creates unique urls, normally this would use a lot of ram.
  });

  // console.log('res: ', res.status);
};


Install K6 globally like this:

(https://k6.io/docs/get-started/installation/)


Linux


Debian/Ubuntu

sudo gpg -ksudo gpg --no-default-keyring --keyring /usr/share/keyrings/k6-archive-keyring.gpg --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys C5AD17C747E3415A3642D57D77C6C491D6AC1D69echo "deb [signed-by=/usr/share/keyrings/k6-archive-keyring.gpg] https://dl.k6.io/deb stable main" | sudo tee /etc/apt/sources.list.d/k6.listsudo apt-get updatesudo apt-get install k6

Fedora/CentOS


Using dnf (or yum on older versions):

sudo dnf install https://dl.k6.io/rpm/repo.rpmsudo dnf install k6

MacOS

Using Homebrew

brew install k6

Windows

If you use the Chocolatey package manager


 you can install the unofficial k6 package with:

choco install k6

If you use the Windows Package Manager, install the official packages from the k6 manifests (created by the community)


winget install k6

Alternatively, you can download and run the latest official installer.


Docker

docker pull grafana/k6