How to Perform Gatling Load Testing and API Testing
How to Perform Gatling Load Testing and API Testing
If you ever made research about performance testing with the help of some tools, you’ve probably heard of Gatling. Right now, it’s one of the most popular performance testing tools in the market and there are many reasons for that.
First of all, it is open source with its special design for continuous load testing and integration with your development pipeline. And unlike JMeter, Gatling is a code-based tool and the only limit for your load test is your coding skills and imagination. In terms of coding, Gatling load testing is based on Scala language.
There are some requirements if you want to perform Gatling load testing. Let’s continue step by step.
1- Install Java 8 JDK
It is needed because Gatling needs Java to run. You can download Java 8 JDK from Oracle Java Downloads link. Explanation for the installation process is also available via link.
2- Install Apache Maven
Maven’s plugin for Gatling will help us to build and compile our codes and simulations. You can download it via this link.
3- Install Gatling
You can download the Community Edition of Gatling via this link.
4- Install IntelliJ Idea
With its plugin built for Scala, IntelliJ Idea becomes an amazing choice for Gatling load testing and performance testing. You can download the Community version via this link.
5- Install Scala Plugin on IntelliJ Idea
Once you install IntelliJ Idea, you should also install Scala plugin that was designed for Scala projects.
If you performed the listed actions above, we can start with creating a Gatling project on IntelliJ Idea.
Open the Terminal/Command Line Interface from your computer. Then navigate to the folder that will store your project and then paste the command below:
mvn archetype:generate -DarchetypeGroupId=io.gatling.highcharts -DarchetypeArtifactId=gatling-highcharts-maven-archetype
After running this command, you will be asked to define group id, artifact id, version. You can define these values by checking available versions from this link.
Then your project files and folders will be created on your project folder. You can open this project folder on IntelliJ Idea by clicking on “Open” button on the first screen.
After the steps above, your IntelliJ Idea window will look like this. In src/test path, you will see two folders named “resources” and “scala”. Resources will contain your test data and necessary request bodies. In “scala” folder, our simulations will be stored, and we will be using them as our test scripts.
You can also use pom.xml file to manipulate and compile your configuration elements.
We will be using the APIs from https://reqres.in/, which is a dummy API website.
Now, open the Engine object under src/test/scala path. You will see a warning like “No Scala SDK in Module.” To solve this issue, click on Setup Scala SDK and then click OK from the popped-up window. Your Scala SDK will be installed.
Now we are ready to write our first test for a Gatling load testing! For our first request, let’s start with “GET Single User” API.
Under scala folder, create a package named “simulations”. And create a file named “TestAPISimulation.scala”. Now write the following code into your TestAPISimulation class:
Now let’s take a look at our code line by line.
With import lines, we made the necessary importing operations.
Our TestAPISimulation class will be extending Simulation from io.gatling.core.scenario.Simulation.
You can think “val” keyword as an expression for variable. With httpConf variable, we defined our baseUrl for the targeted server. “.custom-header” lines help us to provide custom-header information for our requests.
“scn” variable gives us the opportunity to define requests for our scenario. We give a name for the scenario, and then with .exec() method, we created our GET request for /api/users/2 endpoint. And .check() method allows us to make an assertion for response code.
Lastly, we decide about our load with setUp function.
After performing all these steps, we are ready to run our test.
Running your simulation is very easy. First, right click on your “Engine” object in scala folder, then click “Run Engine” button. After building, you will see that the terminal screen on IntelliJ Idea is prompted. Since we only have one Simulation, it will automatically choose our simulation and ask for a run description.
After the execution of the test, your console screen should look like this. You can see some stats about your testing session. And what is more, you will have an html test report page once your test finishes. You can open this report document on any browser and investigate the performance of your system under test.
By changing the “atOnceUsers” value in setUp method, you can manage your thread count and tps values.
And that is all for creating a simple API & Gatling load testing.
The good news is, Loadium is fully compatible with Gatling as well. You can upload your Gatling load testing scripts and perform a load test! Loadium with Gatling can be utilized in a variety of ways. You may use it to create an automated simulation for website performance testing, for example.
If you are curious about Loadium, request a demo from here.