Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Gatling Integration With Maven

DZone's Guide to

Gatling Integration With Maven

Using Gatling with Maven can enhance your Continuous Integration process. You can also externalize the properties used in the simulation script and dynamic data feeding.

Free Resource

Share, secure, distribute, control, and monetize your APIs with the platform built with performance, time-to-value, and growth in mind. Free 90-day trial of 3Scale by Red Hat

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 the properties used in the simulation script and the dynamic data feeding using Gatling feeders.

Let's see the above features in action.

First, 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" 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.

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 here. Add the feeder file under src/test/resources/data. The sample simulation using CSVFeeder 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)
}

The entire project used for this article is available on GitHub.

Explore the core elements of owning an API strategy and best practices for effective API programs. Download the API Owner's Manual, brought to you by 3Scale by Red Hat

Topics:
maven ,gatling ,load test ,integration ,continuous integration

Published at DZone with permission of Siva Prasad Rao Janapati, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}