Website Load Test with k6

Hi,

Today I want to share with you how to load test a website application, or API with K6. With Load Testing, you can know how many concurrent users your website can handle.

K6.io is a load testing for engineering, runs in Node JS environment, free, and very easy to use. K6 is available on multiple platforms, from Windows, Linux, and Mac OS, here's a guide for cross-platform installation.

Installation
k6 has packages for Linux, Mac, and Windows. As alternatives, you can also using a Docker container or a standalone binary.

K6 Installation

Debian / Ubuntu 🔗

sudo gpg --no-default-keyring --keyring /usr/share/keyrings/k6-archive-keyring.gpg --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys C5AD17C747E3415A3642D57D77C6C491D6AC1D69
echo "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.list
sudo apt-get update
sudo apt-get install k6

Fedora/CentOS 🔗

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

macOS 🔗

using homebrew

brew install k6

Windows🔗

using chocolately package manager or Winget-cli or msi installer or portable

choco install k6
winget install k6

Docker 🔗

docker pull grafana/k6

Start Using K6

In this case, I want to install default wordpress application installed on VPS - Compute Engine (GCP) with specification 2vCPU and 1 GB RAM with EasyEngine and load test server responses with K6

VPS Specification Software
e2-micro (Compute Engine GCP) EasyEngine to install WP + NGINX easily
2vCPU (Intel Xeon) PHP 8.0.20
1GB RAM MariaDB-1:10.5.4
10GB Diskspace Wordpress 6.0
Ubuntu 22.04
Install Wordpress 6 with Easy Engine

and I use a VM = n2-standard-4 on Google Cloud Compute Engine (4vCPU with 16GB RAM) to install k6.

instance-1 with wordpress installed and k6 instance with k6 installed

After installation of k6 successfully ( K6 - - 0.38.3 version), let's try to create an initial test to ensure k6 can run

nano initial-test.js
import http from "k6/http";
import { sleep } from "k6";

export default function () {
  http.get("https://gcp.ns1.my.id/2022/06/22/hello-world/");
  sleep(1);
}

Save with ctrl+x, and then run with command k6 run initial-test.js

K6 run initial-test.js

From the above execution results obtained some initial parameters:

  • output : -, because there are no output settings to be assigned to a particular file, in this case, it is directly displayed on the screen.
  • script : script .js to be executed K6
  • iteration : how many iterations/loops to do
  • vus : virtual users, how many simulated users are there for load testing
  • max : max vus each iteration

You can read furthermore about running k6 on their documentation :

Running k6
Follow along to learn how to run a test, add virtual users, increase the test duration, and ramp the number of requests up and down as the test runs.

Thanks for reading!