Web performance tests with k6

Web performance tests with k6

Performance testing is a crucial step in ensuring that your website can handle high levels of traffic and provide a smooth user experience. One tool that can help you perform performance tests on your website is k6. In this blog post, we'll go over how to use k6 to test the performance of your website.

Install

You can install k6 very easily with npm or use the manual installer on https://k6.io/

npm install -g k6

Run

Once k6 is installed, you can use it to run performance tests on your website. One of the simplest ways to do this is to create a script that defines the test scenario and then run the script using k6.

import { check } from 'k6';

import http from 'k6/http';
export default function () {
  let res = http.get(`https://google.com`);
  check(res, {
    'is status 200': (r) => r.status === 200,
  });
}

This script uses k6's built-in http module to make a GET request to the specified URL (in this case, https://google.com). It also uses the check function to check that the response has a status of 200, indicating that the request was successful.

To run the script save it as test.js, you can use the following command:

k6 run test.js --vus 20 --duration 30s

The result will be like

    checks.....................: 100.00% ✓ 2936 ✗ 0
    data_received..............: 70 MB   2.3 MB/s
    data_sent..................: 429 kB  14 kB/s
    http_req_blocked...........: avg=839.05µs min=100ns    med=200ns    max=130.71ms p(90)=300ns    p(95)=300ns
    http_req_connecting........: avg=298.5µs  min=0s       med=0s       max=58.12ms  p(90)=0s       p(95)=0s
    http_req_duration..........: avg=204.06ms min=111.92ms med=192.73ms max=794.46ms p(90)=238.85ms p(95)=267.37ms
    http_req_receiving.........: avg=2.74ms   min=42.6µs   med=2.56ms   max=16.55ms  p(90)=4.47ms   p(95)=5.9ms
    http_req_sending...........: avg=26.04µs  min=11.7µs   med=25µs     max=151.4µs  p(90)=28.9µs   p(95)=31.92µs
    http_req_tls_handshaking...: avg=368.08µs min=0s       med=0s       max=72.96ms  p(90)=0s       p(95)=0s
    http_req_waiting...........: avg=201.29ms min=111.85ms med=189.9ms  max=791.47ms p(90)=235.46ms p(95)=264.09ms
    http_reqs..................: 2936    97.212535/s
    iteration_duration.........: avg=205.01ms min=112.02ms med=192.84ms max=794.55ms p(90)=239.62ms p(95)=269.16ms
    iterations.................: 2936    97.212535/s
    vus........................: 20      min=20 max=20
    vus_max....................: 20      min=20 max=20

This is just a simple example of how to use k6 to performance test a website. Keep in mind that you can also use k6 to test other types of web services and can combine multiple APIs to test application performance as well. Additionally, k6 allows to test with different scenarios such as load testing, spike testing, and soak testing.

In summary, k6 is a powerful and flexible tool for performance testing your website. With just a little bit of setup, you can use k6 to quickly and easily test the performance of your website and ensure that it can handle high levels of traffic.