“How to Perform Load Testing in Go Fiber Applications with Grafana K6?”
Hello, I have recently started developing applications actively using the Go language. In this article, we will develop a simple Go application using the Fiber framework and then create a detailed load test scenario with Grafana K6 and analyze the results.
First, we will create a simple Go Fiber application and connect to a PostgreSQL database using GORM. In this application, we will focus on basic CRUD operations and perform tests on them.
Starting the Go Module: To begin your application, you will first need to create a Go module:
go mod init project-name
go get github.com/gofiber/fiber/v2
Creating the main.go
File: To launch the Fiber application, we can create our main.go
file and fill it as follows:
We can start our simple Fiber application with the command go run main.go
. However, the process doesn't end here. We will make our application more meaningful by adding a PostgreSQL database. In this step, we will connect our Go Fiber application to a PostgreSQL database using the GORM ORM.
PostgreSQL Connection: We will create a PostgreSQL database using AWS Free Tier and connect our Go Fiber application to this AWS database. After creating the PostgreSQL database on AWS, we will connect your application to the AWS PostgreSQL database to perform CRUD operations.
On the Go side, we successfully connected to the AWS PostgreSQL database and received the log output confirming the connection. Additionally, we performed automatic migrations with GORM without any issues. This way, the necessary tables for our application were automatically created in the database.
Our code in the “database.go” file, which handles the necessary database connection, is as follows:
Now that we have completed the necessary configurations, we can perform a load test on our Go Fiber application using Grafana K6.
K6 Load Test Scenario: To create a load test scenario, we will create a file named k6-test.js
. This file will contain the configuration of the K6 test scenario. Below is the content of this test. In our test scenario, we will direct 50 virtual users to three different Product endpoints for 30 seconds.
Our results are as follows:
If you wish, we can extend these test scenarios a bit further, and let’s not forget that these tests are being run on a local server. :)
Results: According to the results of our load test, we can see that we achieved successful results in 90% of the requests. This shows that the application runs stably under a certain load.
Database Details: The AWS PostgreSQL database we used for the tests was a system with 2 CPUs and 1 GB of RAM. This type of setup can provide sufficient performance for small-scale projects and trial use. However, for larger projects and more intense traffic, a more robust setup might be required.
Summary: Load tests are critical for measuring the resilience and performance of our application. We can say that our Go application, which connects to a PostgreSQL database using Fiber and GORM, has passed these tests and has delivered largely successful results.
Additionally, if you would like to check out the repo: https://github.com/TarikSogukpinar/go-k6
Good luck to everyone! See you in the next articles!