In this article, we will see how to use Gatling with Maven. This approach enables us to integrate Gatling as part of continuous integration. Along with that, we will see how to externalize properties used in simulation script and the dynamic data feeding using Gatling feeders. Let us see the above said features in action.
As a first step create a maven project and add the below Gatling dependencies.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>org.smarttechie</groupId> | |
<artifactId>Gatling-maven-demo</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<packaging>jar</packaging> | |
<name>Gatling-maven-demo</name> | |
<description>Demo project for Gatling Maven</description> | |
<properties> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | |
<java.version>1.8</java.version> | |
<gatling.version>2.2.3</gatling.version> | |
<gatling-plugin.version>2.2.1</gatling-plugin.version> | |
<scala-maven-plugin.version>3.2.2</scala-maven-plugin.version> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>io.gatling.highcharts</groupId> | |
<artifactId>gatling-charts-highcharts</artifactId> | |
<version>${gatling.version}</version> | |
<scope>test</scope> | |
</dependency> | |
</dependencies> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>net.alchim31.maven</groupId> | |
<artifactId>scala-maven-plugin</artifactId> | |
<version>${scala-maven-plugin.version}</version> | |
</plugin> | |
<plugin> | |
<groupId>io.gatling</groupId> | |
<artifactId>gatling-maven-plugin</artifactId> | |
<version>${gatling-plugin.version}</version> | |
<executions> | |
<execution> | |
<configuration> | |
<configFolder>src/test/resources</configFolder> | |
<dataFolder>src/test/resources/data</dataFolder> | |
<resultsFolder>src/test/results</resultsFolder> | |
<requestBodiesFolder>src/test/resources/bodies</requestBodiesFolder> | |
<simulationsFolder>src/test/simulations</simulationsFolder> | |
<runMultipleSimulations>true</runMultipleSimulations> | |
</configuration> | |
<goals> | |
<goal>execute</goal> | |
</goals> | |
</execution> | |
</executions> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
Now, we will externalize the baseURL property used in our simulation. For this add the application.properties file under src/test/resources and use ConfigFactory to get access to the properties. The sample simulation is given below.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.smarttechie | |
import io.gatling.core.Predef._ | |
import io.gatling.http.Predef._ | |
import scala.concurrent.duration._ | |
import com.typesafe.config._ | |
class SampleRESTEndPointPrefSimulation extends Simulation { | |
val conf = ConfigFactory.load() | |
val baseUrl = conf.getString("baseUrl") | |
val httpProtocol = http.baseURL(baseUrl) //sample comment | |
val restEndPoint = "/posts" | |
val restEndpointScenario = scenario("Posts_Pref_Simulation") | |
.exec(http("request_1") | |
.get(restEndPoint)) | |
setUp(restEndpointScenario.inject(rampUsers(1000) over (10 seconds))).protocols(httpProtocol) | |
} |
Now, we will see how to add dynamic data used as part of the simulation. For this, I am using Gatling’s CSVFeeder. You can get more info on Gatling’s feeders http://gatling.io/docs/2.2.3/session/feeder.html. Add the feeder file under src/test/resources/data. The sample simulation using csv feeder is given below.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.smarttechie | |
import io.gatling.core.Predef._ | |
import io.gatling.http.Predef._ | |
import scala.concurrent.duration._ | |
import com.typesafe.config._ | |
class SampleFeederRESTEndPointPrefSimulation extends Simulation { | |
val conf = ConfigFactory.load() | |
val postsFeed = csv("posts.csv").circular //CSV Feeder | |
val baseUrl = conf.getString("baseUrl") | |
val httpProtocol = http.baseURL(baseUrl) //sample comment | |
val restEndPoint = "/posts/${post_id}" // The value of the post_id filed from the feed will go here. | |
val restEndpointScenario = scenario("Posts_Pref_Feed_Simulation") | |
.feed(postsFeed) //pass the feed for scenario | |
.exec(http("request_1") | |
.get(restEndPoint)) | |
setUp(restEndpointScenario.inject(rampUsers(1000) over (10 seconds))).protocols(httpProtocol) | |
} |
Run mvn gatling:execute to execute Gatling project which will run the simulations. The entire project used for this article is available on GitHub.
Hi Siva, I am new to Gatling tool/maven integration. Could you please post step by step of how to integrate maven with Gatling.
If you follow the article you are able to setup Gatling with Maven. What information you are looking specifically.