API Load Testing With Gatling
Load testing is an important practice for APIs and applications that have lots of users. For load testing their APIs, developers can choose between many popular open source load testing tools. One of these is Gatling. This blog post will explain how to execute a load test with Gatling for a REST API endpoint that uses the GET method.
Usually, this blog covers load testing with Apache JMeter™. Both Gatling and JMeter are very powerful load testing tools. But one of the advantages Gatling offers Java developers, is the Scala development language for test creation, which is nicer to use in my opinion. Read a more comprehensive comparison of open source performance testing tools here.
If you want to practice yourself, you can find the complete source with all the configs and codes this blog post covers, here. This article will show code fragments. The environment we will be using is Java and Spring Boot API (an open source API framework), together with the Gradle build tool, as usual in my articles. Don’t forget to install Gatling. Let’s get started.
1. First, like in any library, we need to add the dependencies to our build.gradle file.
2. As mentioned before, Gatling uses Scala for test configuration. Therefore, we have to add plugin support to build.gradle. Run the following command:
3. Next we need to add a task for Gradle, so that we can execute our load tests from the command line. To do that, add the following code to the build.gradle file:
This is the default way for creating custom tasks for Gradle. It is a JavaExec type task, with test class dependencies. This means it has nothing to do with main classes. The most important part here is the configuration, where we let Gatling know which simulation (basically that’s a test) to execute, where to store reports and where to get body files (body files are for requests that require having a body).
4. That’s all the basic configuration we have to do for executing a Gatling test. Now let’s create the actual test (simulation). For that, create a BlazeMeterGatlingTest file in src/test/scala folder with the following content:
This our basic load test, which will do following:
It will call http://localhost:16666/api/1.0/arrival/all 1000 times and make sure that we have status 200, meaning that it is fine. You can change this to your own API config and the number of requests, i.e the load.
5. Compared to JMeter, Gatling has an advantage in reporting. After each execution you can see the path to the latest generated report in the command line. In my case, it is following part of the command line output:
And when I point my browser to that path I can see the report shown in the pictures below. In the reports we have lots of useful information, the most useful part is on the first page directly where we can see request count (1000) , passed requests count (OK) and failed request count (KO).
This is the minimum that we need, but for our API improvements we can take a deeper look into the details. For example, in case of errors we will have this:
That’s it! You now know how to run a load test for your API endpoint with Gatling.
Looking to automate your API tests? Get your API Testing started with BlazeMeter.
You might also find these useful:
Interested in writing for our Blog? Send us a pitch!