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. “.header” lines help us to provide 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.