Gatling | Maven Setup.


Gatling is a highly capable load testing tool. It is designed for ease of use, maintainability and high performance. As this one-liner explanation doesn’t explain the whole concept, please visit this nice presentation: why should we use Gatling and what’s it has for us here. Well, I’ll take you through how we can implement Gatling in a project which uses maven builds.

So, We need to create a maven module/sub-project or a stand-alone maven project for Gatling with a meaningful name e.g. gatling-simulations or gatling-tests etc. So, you may have two cases here first you are implementing a gatling-simulator in a maven project where you’ll make gatling-test as a module of this project, second is when you can make a new Maven Gatling-Simulator standalone. Both cases are almost same just the directory structure may differ. For example:

The approach is exactly same, as both are maven projects. So, let’s start.

Steps of Setup:

        • Create a maven project with a meaningful name e.g. Gatling-test, Gatling-simulations, load-test etc.
        • Add src/test/resources for resources and src/test/scala for Simulator classes.
        • Your pom.xml should look something like:
      <?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>
      	<parent>
      		<groupId>com.example.project</groupId>
      		<artifactId>project</artifactId>
      		<version>0.0.1-SNAPSHOT</version>
      	</parent>
      	<artifactId>gatling-tests</artifactId>
      	<properties>
      		<scala-maven-plugin.version>3.2.2</scala-maven-plugin.version>
      		<checkstyle.skip>true</checkstyle.skip>
      	</properties>
      	<dependencies>
      		<dependency>
      			<groupId>io.gatling.highcharts</groupId>
      			<artifactId>gatling-charts-highcharts</artifactId>
      			<version>2.2.5</version>
      			<scope>test</scope>
      		</dependency>
      		<dependency>
      			<groupId>org.scala-lang</groupId>
      			<artifactId>scala-library</artifactId>
      			<version>2.11.11</version>
      		</dependency>
      		<dependency>
      			<groupId>org.scala-lang</groupId>
      			<artifactId>scala-reflect</artifactId>
      			<version>2.11.11</version>
      		</dependency>
      		<dependency>
      			<groupId>com.google.guava</groupId>
      			<artifactId>guava</artifactId>
      			<version>19.0</version>
      		</dependency>
      		<dependency>
      			<groupId>io.netty</groupId>
      			<artifactId>netty-all</artifactId>
      			<version>4.1.6.Final</version>
      		</dependency>
      		<dependency>
      			<groupId>org.slf4j</groupId>
      			<artifactId>slf4j-api</artifactId>
      			<version>1.7.25</version>
      		</dependency>
      		<dependency>
      			<groupId>com.codahale.metrics</groupId>
      			<artifactId>metrics-core</artifactId>
      			<version>3.0.2</version>
      		</dependency>
      		<dependency>
      			<groupId>com.opencsv</groupId>
      			<artifactId>opencsv</artifactId>
      			<version>3.3</version>
      		</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>2.2.4</version>
      				<executions>
      					<execution>
      						<id>Simulations</id>
      						<phase>load-test</phase> <<!-- or test/verify phase-->
      						<configuration>
      							<configFolder>${project.basedir}/src/test/resources</configFolder>
      							<dataFolder>${project.basedir}/src/test/resources/data</dataFolder>
      							<resultsFolder>${project.basedir}/src/test/results</resultsFolder>
      							<requestBodiesFolder>${project.basedir}/src/test/resources/bodies</requestBodiesFolder>
      							<simulationClass>simulator.package.Simulations</simulationClass>
      						</configuration>
      						<goals>
      							<goal>execute</goal>
      						</goals>
      					</execution>
      				</executions>
      			</plugin>
      		</plugins>
      	</build>
      </project>
      
        • Then gatling.conf must be there:
      #########################
      # Gatling Configuration #
      #########################
      
      # This file contains all the settings configurable for Gatling with their default values
      
      gatling {
        core {
          #outputDirectoryBaseName = "" # The prefix for each simulation result folder (then suffixed by the report generation timestamp)
          #runDescription = ""          # The description for this simulation run, displayed in each report
          #encoding = "utf-8"           # Encoding to use throughout Gatling for file and string manipulation
          #simulationClass = ""         # The FQCN of the simulation to run (when used in conjunction with noReports, the simulation for which assertions will be validated)
          #mute = false                 # When set to true, don't ask for simulation name nor run description (currently only used by Gatling SBT plugin)
      
          extract {
            regex {
              #cacheMaxCapacity = 200 # Cache size for the compiled regexes, set to 0 to disable caching
            }
            xpath {
              #cacheMaxCapacity = 200 # Cache size for the compiled XPath queries,  set to 0 to disable caching
            }
            jsonPath {
              #cacheMaxCapacity = 200 # Cache size for the compiled jsonPath queries, set to 0 to disable caching
              #preferJackson = false  # When set to true, prefer Jackson over Boon for JSON-related operations
              jackson {
                #allowComments = false           # Allow comments in JSON files
                #allowUnquotedFieldNames = false # Allow unquoted JSON fields names
                #allowSingleQuotes = false       # Allow single quoted JSON field names
              }
      
            }
            css {
              #cacheMaxCapacity = 200 # Cache size for the compiled CSS selectors queries,  set to 0 to disable caching
            }
          }
      
          timeOut {
            #simulation = 8640000 # Absolute timeout, in seconds, of a simulation
          }
          directory {
            data = src/test/resource/data               # Folder where user's data (e.g. files used by Feeders) is located
            bodies = src/test/resource/bodies           # Folder where bodies are located
            simulations = src/test/scala/package # Folder where the bundle's simulations are located<
            #reportsOnly = src/test/results                     # If set, name of report folder to look for in order to generate its report
            #binaries = ""                        # If set, name of the folder where compiles classes are located: Defaults to GATLING_HOME/target.
            #results = src/test/results                    # Name of the folder where all reports folder are located
          }
        }
        charting {
          #noReports = false       # When set to true, don't generate HTML reports
          #maxPlotPerSeries = 1000 # Number of points per graph in Gatling reports
          #accuracy = 10           # Accuracy, in milliseconds, of the report's stats
          indicators {
            #lowerBound = 800      # Lower bound for the requests' response time to track in the reports and the console summary
            #higherBound = 1200    # Higher bound for the requests' response time to track in the reports and the console summary
            #percentile1 = 50      # Value for the 1st percentile to track in the reports, the console summary and GraphiteDataWriter
            #percentile2 = 75      # Value for the 2nd percentile to track in the reports, the console summary and GraphiteDataWriter
            #percentile3 = 95      # Value for the 3rd percentile to track in the reports, the console summary and GraphiteDataWriter
            #percentile4 = 99      # Value for the 4th percentile to track in the reports, the console summary and GraphiteDataWriter
          }
        }
        http {
          #elFileBodiesCacheMaxCapacity = 200        # Cache size for request body EL templates, set to 0 to disable
          #rawFileBodiesCacheMaxCapacity = 200       # Cache size for request body Raw templates, set to 0 to disable
          #fetchedCssCacheMaxCapacity = 200          # Cache size for CSS parsed content, set to 0 to disable
          #fetchedHtmlCacheMaxCapacity = 200         # Cache size for HTML parsed content, set to 0 to disable
          #redirectPerUserCacheMaxCapacity = 200     # Per virtual user cache size for permanent redirects, set to 0 to disable
          #expirePerUserCacheMaxCapacity = 200       # Per virtual user cache size for permanent 'Expire' headers, set to 0 to disable
          #lastModifiedPerUserCacheMaxCapacity = 200 # Per virtual user cache size for permanent 'Last-Modified' headers, set to 0 to disable
          #etagPerUserCacheMaxCapacity = 200         # Per virtual user cache size for permanent ETag headers, set to 0 to disable
          #warmUpUrl = "http://gatling.io"           # The URL to use to warm-up the HTTP stack (blank means disabled)
          #enableGA = true                           # Very light Google Analytics, please support
          ssl {
            trustStore {
              #type = ""      # Type of SSLContext's TrustManagers store
              #file = ""      # Location of SSLContext's TrustManagers store
              #password = ""  # Password for SSLContext's TrustManagers store
              #algorithm = "" # Algorithm used by SSLContext's TrustManagers store
            }
            keyStore {
              #type = ""      # Type of SSLContext's KeyManagers store
              #file = ""      # Location of SSLContext's KeyManagers store
              #password = ""  # Password for SSLContext's KeyManagers store
              #algorithm = "" # Algorithm used SSLContext's KeyManagers store
            }
          }
          ahc {
            #allowPoolingConnections = true             # Allow pooling HTTP connections (keep-alive header automatically added)
            #allowPoolingSslConnections = true          # Allow pooling HTTPS connections (keep-alive header automatically added)
            #compressionEnforced = false                # Enforce gzip/deflate when Accept-Encoding header is not defined
            #connectTimeout = 60000                     # Timeout when establishing a connection
            #pooledConnectionIdleTimeout = 60000        # Timeout when a connection stays unused in the pool
            #readTimeout = 60000                        # Timeout when a used connection stays idle
            #connectionTTL = -1                         # Max duration a connection can stay open (-1 means no limit)
            #ioThreadMultiplier = 2                     # Number of Netty worker threads per core
            #maxConnectionsPerHost = -1                 # Max number of connections per host (-1 means no limit)
            #maxConnections = -1                        # Max number of connections (-1 means no limit)
            #maxRetry = 2                               # Number of times that a request should be tried again
            #requestTimeout = 60000                     # Timeout of the requests
            #useProxyProperties = false                 # When set to true, supports standard Proxy System properties
            #webSocketTimeout = 60000                   # Timeout when a used websocket connection stays idle
            #useRelativeURIsWithConnectProxies = true   # When set to true, use relative URIs when talking with an SSL proxy or a WebSocket proxy
            #acceptAnyCertificate = true                # When set to true, doesn't validate SSL certificates
            #httpClientCodecMaxInitialLineLength = 4096 # Maximum length of the initial line of the response (e.g. "HTTP/1.0 200 OK")
            #httpClientCodecMaxHeaderSize = 8192        # Maximum size, in bytes, of each request's headers
            #httpClientCodecMaxChunkSize = 8192         # Maximum length of the content or each chunk
            #keepEncodingHeader = true                  # Don't drop Encoding response header after decoding
            #webSocketMaxFrameSize = 10240              # Maximum frame payload size
            #httpsEnabledProtocols = ""                 # Comma separated enabled protocols for HTTPS, if empty use the JDK defaults
            #httpsEnabledCipherSuites = ""              # Comma separated enabled cipher suites for HTTPS, if empty  use the JDK defaults
            #sslSessionCacheSize = 20000                # SSLSession cache size (set to 0 to disable)
            #sslSessionTimeout = 86400                  # SSLSession timeout (default is 24, like Hotspot)
          }
        }
        data {
          #writers = "console, file" # The lists of DataWriters to which Gatling write simulation data (currently supported : "console", "file", "graphite", "jdbc")
          #reader = file             # The DataReader used by the charting engine for reading simulation results
          console {
            #light = false           # When set to true, displays a light version without detailed request stats
          }
          file {
            #bufferSize = 8192       # FileDataWriter's internal data buffer size, in bytes
          }
          leak {
            #noActivityTimeout = 30  # Period, in seconds, for which Gatling may have no activity before considering a leak may be happening
          }
          jdbc {
            db {
              #url = "jdbc:mysql://localhost:3306/temp" # The JDBC URL used by the JDBC DataWriter
              #username = "root"                        # The database user used by the JDBC DataWriter
              #password = "123123q"                     # The password for the specified user
            }
            #bufferSize = 20                            # The size for each batch of SQL inserts to send to the database
            create {
              #createRunRecordTable = "CREATE TABLE IF NOT EXISTS `RunRecords` ( `id` INT NOT NULL AUTO_INCREMENT , `runDate` DATETIME NULL , `simulationId` VARCHAR(45) NULL , `runDescription` VARCHAR(45) NULL , PRIMARY KEY (`id`) )"
              #createRequestRecordTable = "CREATE TABLE IF NOT EXISTS `RequestRecords` (`id` int(11) NOT NULL AUTO_INCREMENT, `runId` int DEFAULT NULL, `scenario` varchar(45) DEFAULT NULL, `userId` VARCHAR(30) NULL, `name` varchar(50) DEFAULT NULL, `requestStartDate` bigint DEFAULT NULL, `requestEndDate` bigint DEFAULT NULL, `responseStartDate` bigint DEFAULT NULL, `responseEndDate` bigint DEFAULT NULL, `status` varchar(2) DEFAULT NULL, `message` varchar(4500) DEFAULT NULL, `responseTime` bigint DEFAULT NULL, PRIMARY KEY (`id`) )"
              #createScenarioRecordTable = "CREATE TABLE IF NOT EXISTS `ScenarioRecords` (`id` int(11) NOT NULL AUTO_INCREMENT, `runId` int DEFAULT NULL, `scenarioName` varchar(45) DEFAULT NULL, `userId` VARCHAR(30) NULL, `event` varchar(50) DEFAULT NULL, `startDate` bigint DEFAULT NULL, `endDate` bigint DEFAULT NULL, PRIMARY KEY (`id`) )"
              #createGroupRecordTable = "CREATE TABLE IF NOT EXISTS `GroupRecords` (`id` int(11) NOT NULL AUTO_INCREMENT, `runId` int DEFAULT NULL, `scenarioName` varchar(45) DEFAULT NULL, `userId` VARCHAR(30) NULL, `entryDate` bigint DEFAULT NULL, `exitDate` bigint DEFAULT NULL, `status` varchar(2) DEFAULT NULL, PRIMARY KEY (`id`) )"
            }
            insert {
              #insertRunRecord = "INSERT INTO RunRecords (runDate, simulationId, runDescription) VALUES (?,?,?)"
              #insertRequestRecord = "INSERT INTO RequestRecords (runId, scenario, userId, name, requestStartDate, requestEndDate, responseStartDate, responseEndDate, status, message, responseTime) VALUES (?,?,?,?,?,?,?,?,?,?,?)"
              #insertScenarioRecord = "INSERT INTO ScenarioRecords (runId, scenarioName, userId, event, startDate, endDate) VALUES (?,?,?,?,?,?)"
              #insertGroupRecord = "INSERT INTO GroupRecords (runId, scenarioName, userId, entryDate, exitDate, status) VALUES (?,?,?,?,?,?)"
            }
          }
          graphite {
            #light = false              # only send the all* stats
            #host = "localhost"         # The host where the Carbon server is located
            #port = 2003                # The port to which the Carbon server listens to
            #protocol = "tcp"           # The protocol used to send data to Carbon (currently supported : "tcp", "udp")
            #rootPathPrefix = "gatling" # The common prefix of all metrics sent to Graphite
            #bufferSize = 8192          # GraphiteDataWriter's internal data buffer size, in bytes
            #writeInterval = 1          # GraphiteDataWriter's write interval, in seconds
          }
        }
      }
      
          • Then design your simulator in scala:
        package sample.package
        
        import io.gatling.core.Predef._
        import io.gatling.http.Predef._
        import scala.concurrent.duration._
        import scala.util.Random
        import com.typesafe.config._
        import scala.util.control.Breaks._
        
        class Simulations extends Simulation {
        
          val headers_0 = Map(
            "Content-Type" -> "application/json",
            "api-key" -> "123")
        
          val headers_1 = Map("api-key" -> "123")
        
          object getMyObject {
            val validCase = repeat(10) {
              exec(http("Get Positive Case")
                .get("<your-api-url>").check(status.find.in(200))
                .headers(headers_1))
            }
            val inValidCase = repeat(10) {
              exec(http("Get Negative Case")
                .get("<your-api-url>").check(status.find.in(400,401,404,501,502))
                .headers(headers_1))
                .pause(1)
            }
          }
          val httpConfGetMyObject = http
            .baseURL("localhost:8080")
            .acceptHeader("application/json; charset=utf-8")
            .doNotTrackHeader("1")
            .acceptLanguageHeader("en-US,en;q=0.5")
            .acceptEncodingHeader("gzip, deflate")
            .userAgentHeader("Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:16.0) Gecko/20100101 Firefox/16.0")
          val commerceGetValidObjectScenario = scenario("GetMyObject").exec(getMyObject.validCase)
          val commerceGetInValidObjectScenario = scenario("GetInValidMyObject").exec(getMyObject.inValidCase)
          setUp(
            commerceGetValidItenaryScenario.inject(rampUsers(25) over (10 seconds)).protocols(httpConfGetMyObject),
            commerceGetInValidItenaryScenario.inject(rampUsers(25) over (10 seconds)).protocols(httpConfGetMyObject))
        }
        
        

Now your setup is ready to run. The maven command to run your Gatling setup is completely dependent upon the configurations given in your plugin in the build.

Mine is like:

 mvn&nbsp;gatling:test 

or

 mvn&nbsp;gatling:execute 

for Standalone setup.

mvn -pl post-deployment-tests/gatling-test gatling:test 

or

mvn -pl post-deployment-tests/gatling-test gatling:execute

for built-in setup.

Please comment your doubts or queries and share if you liked the post.

Pasted image at 2017_11_27 04_17 PM

This entry was posted in gatling, Scala. Bookmark the permalink.

One Response to Gatling | Maven Setup.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s