Gatling – Integration with Maven

gatling-logo

                                               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.


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance&quot;
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
<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>

view raw

pom.xml

hosted with ❤ by GitHub

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.


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.


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.

Gatling Maven Demo

Siva Janapati is an Architect with experience in building Cloud Native Microservices architectures, Reactive Systems, Large scale distributed systems, and Serverless Systems. Siva has hands-on in architecture, design, and implementation of scalable systems using Cloud, Java, Go lang, Apache Kafka, Apache Solr, Spring, Spring Boot, Lightbend reactive tech stack, APIGEE edge & on-premise and other open-source, proprietary technologies. Expertise working with and building RESTful, GraphQL APIs. He has successfully delivered multiple applications in retail, telco, and financial services domains. He manages the GitHub(https://github.com/2013techsmarts) where he put the source code of his work related to his blog posts.

Tagged with: , ,
Posted in Testing
2 comments on “Gatling – Integration with Maven
  1. Shile says:

    Hi Siva, I am new to Gatling tool/maven integration. Could you please post step by step of how to integrate maven with Gatling.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Dzone.com
DZone

DZone MVB

Java Code Geeks
Java Code Geeks
OpenSourceForYou