Performance testing with Gatling - integration with Maven
Current post is part of Performance testing with Gatling series in which Gatling performance testing tool is explained in details.
Code samples are available in GitHub sample-performance-with-gatling repository.
Add Maven dependencies
The first step is to create Maven project and add corresponding dependencies to pom.xml file. Only needed is to gatling-charts-highcharts in order to be able to have access to Galling libraries:<dependencies>
<dependency>
<groupId>io.gatling.highcharts</groupId>
<artifactId>gatling-charts-highcharts</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
In GitHub, code version is configured as property for an easy change.
Add Maven plugins
Once dependency to Gatling is added then plugin references should be added. One is for Scala Maven plugin used for compiling/testing/running/documenting Scala code in Maven. Another plugin is Gatling Maven plugin used for running the performance test scenarios:<build>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<groupId>io.gatling</groupId>
<artifactId>gatling-maven-plugin</artifactId>
<version>2.0.0</version>
<configuration>
<jvmArgs>
<jvmArg>-Durl=http://localhost:9000</jvmArg>
<jvmArg>-DnumberOfUsers=10</jvmArg>
<jvmArg>-DnumberOfRepetitions=1</jvmArg>
<jvmArg>-DdurationMinutes=1</jvmArg>
<jvmArg>-DpauseBetweenRequestsMs=3000</jvmArg>
<jvmArg>-Ddebug=true</jvmArg>
</jvmArgs>
</configuration>
</plugin>
</plugins>
</build>
Read configurations from Gatling Maven plugin
In order to make it easy to configure several parameters are being passed as jvmArgs, this makes it very easy to run the tests with different configurations. Those are read in code as Java system property:val numberOfUsers: Int = System.getProperty("numberOfUsers").toInt
val duration: FiniteDuration = System.getProperty("durationMinutes").toInt.minutes
private val url: String = System.getProperty("url")
private val repeatTimes: Int = System.getProperty("numberOfRepetitions").toInt
private val isDebug = System.getProperty("debug").toBoolean
Gatling Maven plugin defaults
By default Gatling Maven plugin uses following default paths, so they do not need to be explicitly provided:<configuration>
<configFolder>src/test/resources</configFolder>
<dataFolder>src/test/resources/data</dataFolder>
<resultsFolder>target/gatling/results</resultsFolder>
<bodiesFolder>src/test/resources/bodies</bodiesFolder>
<simulationsFolder>src/test/scala</simulationsFolder>
</configuration>
Record simulation
Description how to record simulation can be found in Performance testing with Gatling – record and playback post. Once the simulation is recorded it can be modified and added to Maven project. More details of what a recorded simulation consists of can be found in Performance testing with Gatling – recorded simulation explanation post.Running simulation
Gatling simulations are run with the mvn gatling:execute command. The important part is to provide which is the simulation class to be run. One is to use a <simulationClass> configuration in pom.xml, other and more flexible is to give it in mvn command: mvn gatling:execute -Dgatling.simulationClass={FULL_PATH_TO_SIMULATION_CLASS}, example is: mvn gatling:execute -Dgatling.simulationClass=com.automationrhapsody.gatling.simulations.original.ProductSimulationChange default configuration
Default Gatling configuration can be seen in their GitHub: gatling-defaults.conf. If you need to change those when using Gatling with Maven there are two possible options:Change with a file
In order to change default configuration with a file, you have to create a file similar to gatling-defaults.conf but name it gatling.conf. Leave in the file only the configuration that you want to change. Below is a file that makes Gatling not to produce any reports.gatling {
charting {
noReports = true
}
}
This file should be put into configFolder property of gatling-maven-plugin config in pom.xml. By default, this is: src/test/resources, but you can change the default as well.