Tutorial: How to Use Gatling With Maven
Another key integration for enabling CI/CD
In this post, we will see how to use Gatling with Maven, creating an integration via plugins. In the world of CI/CD, attaining this kind of integration has become essential. In particular, I’ll show you how to integrate Gatling, use Scala to develop a simulation, and how to execute this with Maven for generating reports.
Preparing a Project with Maven
First, we will create a new Maven project and add the dependencies. We will be able to incorporate Gatling into an existing project (whether the performance tests are going to exist in the same project as other tests, or on their own) or in a project starting from scratch. If we want to employ Gatling for a Maven project that was started from scratch and made specifically to use Gatling tests and nothing else, it would be useful to build it starting from an archetype (Maven project template), following the steps in this post.
Dependencies and Plugins
To use Gatling with Maven we will need to add a dependency (gatling-charts-highcharts) and a plugin (gatling-maven-plugin) to our project’s pom.xml file. In Maven, plugins are responsible for executing tasks, such as to compile projects, run tests, and create .jar files, among others. Generally, they provide the project with a series of commands that can be executed utilizing the following syntax:
Later we will see how we can utilize some of these commands.
The pom.xml file looks more or less like this:
By adding these lines to our pom.xml file, we are providing the libraries necessary to use Gatling, with which we can execute commands in Maven. These will permit us to build and execute our project, including the generation of reports.
Here is an example of a project running Maven and Gatling, with some prepared simulations on a testing environment. This project was developed starting from the archetype mentioned above.
I tried two different IDEs, one based on Eclipse, and one on IntelliJ. I preferred the second. In order for IntelliJ to work, we must install the Scala plugin, although when a scala file is opened, an install link appears automatically. We will also then have to add the SDK to the project (though the IDE also makes this suggestion and it’s almost automatic).
Prepare a Simulation (Script)
Now let’s develop a script utilizing Scala that will contain the simulation that we wish to run.
How to Prepare a Simple Test
There are various ways to identify the URLs that we wish to use in our simulations, as well as the data needed for their configuration. For this, it’s necessary to capture the HTTP traffic between the client and the server. One of the ways to do this is using the “Network” tab in Chrome’s toolbar, which we can access by pressing the F12 key. Once we activate the network tab, we can load the desired URL, right click on the request and select the option Copy/Copy as cURL (cmd). In the copy of the results, we can find all of the data that will facilitate a correct configuration of the URLs in the script of our simulation
Execution of Gatling with Maven
Now we will use Maven to run our script. For this we will open a terminal and locate the root address of our project and execute the following command:
Maven will recognize this as a command thanks to the Gatling plugin that we added to the pom.xml file. This project will also include two other commands that can be used, which are:
During the execution we can see the console output:
By the end of the execution of our script, Gatling will have generated some excellent reports for us to analyze. We can find these results by accessing the directory that appears in the terminal at the end of the script’s execution.
Once it’s opened we will see a web report such as this:
Now you can start to integrate your scripts using the Maven building tool! This will also facilitate the execution of these scripts with Continuous Integration tools such as Jenkins. A special thanks to Oscar Calderin and Iván Serrano for greatly helping to put this tutorial on how to use Gatling with Maven together.
Have any questions? Leave us a comment!