Examine Your App's Speed and Bottlenecks: A Tutorial to Mastering Performance Testing with K6!

Written by gultm | Published 2023/12/28
Tech Story Tags: devops | performance-testing | highload | golang | automated-testing | software-testing | javascript | performance-testing-with-k6

TLDRPerformance tests are useful for uncovering bottlenecks, and scalability issues. In this article, I want to show you how to build several testing scenarios for your service using K6.via the TL;DR App

In the world of software development, where user experience depends on the speed and reliability of applications, the importance of performance testing cannot be overestimated.

Especially if you're about to launch a new service or endpoint and don't know what to expect from it when it is faced with a surge of users or an unexpected traffic spike, it's an exact case when you want to have performance testing.

Performance tests are useful for uncovering bottlenecks, and scalability issues.

In this article, I want to show you how to build several testing scenarios for your service using K6.

For demonstration purposes, we create an API service in Go-lang:

// main.go
package main

import (
	"fmt"
  	"net/http"
)

func main() {
	http.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprint(w, "Hello, World!")
	})

	http.ListenAndServe(":8080", nil)
}

Run the API with:

go run main.go

This API will be running on http://localhost:8080/hello.

Once our API service is running, we’re ready to create some performance tests.

We’re going to use K6 so you need to install it first. You can install K6 by following the instructions on the official website.

Now, let's create a simple K6 script to test this API (you can create different scripts for different scenarios). Create a file named script.js:

// script.js
import http from 'k6/http';
import { check, sleep } from 'k6';

export let options = {
    stages: [
        { duration: '30s', target: 10 },  // Ramp-up to 10 virtual users over 30 seconds
        { duration: '1m', target: 10 },   // Stay at 10 virtual users for 1 minute
        { duration: '30s', target: 0 },   // Ramp-down to 0 virtual users over 30 seconds
    ],
};

export default function () {
    let res = http.get('http://localhost:8080/hello');
    check(res, {
        'status is 200': (r) => r.status === 200,
    });

    // Simulate a user "thinking" by sleeping for a short duration
    sleep(5);
}

This script defines a stress scenario with a ramp-up to 10 virtual users, maintaining that load for 1 minute, and then ramping down to 0 users. Adjust the target and duration in the options.stages array according to your specific testing needs.

We have our very first scenario for the performance test and are ready to see the results.

Open a terminal and run the K6 test:

k6 run script.js

As a result of running this test, you’ll get an output in your console:

This output is instrumental when running your performance tests from a single point of launching tests. If you want to run tests from several pods (machines) I recommend you to use metrics and performance dashboards instead of this output to understand service behaviour under high loads.


Written by gultm | I'm a software engineer.
Published by HackerNoon on 2023/12/28