Gatling, the load testing tool, is a JVM-based powerhouse, and integrating it with Scala and Maven is a common and effective setup for performance testing.
Let’s get Gatling up and running with Scala and Maven.
First, ensure you have Java Development Kit (JDK) version 8 or later installed. You can check this by running:
java -version
Next, you’ll need Apache Maven. If it’s not installed, download it from the official Maven website and add its bin directory to your system’s PATH. Verify the installation with:
mvn -version
Now, let’s create a new Maven project. Navigate to the directory where you want to create your project and run:
mvn archetype:generate -DgroupId=com.mycompany.loadtests -DartifactId=gatling-loadtest -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false
This command creates a basic Maven project structure. com.mycompany.loadtests is your group ID, and gatling-loadtest is your artifact ID (project name).
Now, change into your new project directory:
cd gatling-loadtest
Open the pom.xml file in your project’s root directory. We need to add the Gatling Maven plugin and the Gatling core dependency. Replace the existing content with this:
<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>com.mycompany.loadtests</groupId>
<artifactId>gatling-loadtest</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<gatling.version>3.8.5</gatling.version>
<scala.version>2.13.8</scala.version>
</properties>
<dependencies>
<dependency>
<groupId>io.gatling</groupId>
<artifactId>gatling-core</artifactId>
<version>${gatling.version}</version>
</dependency>
<dependency>
<groupId>io.gatling</groupId>
<artifactId>gatling-http</artifactId>
<version>${gatling.version}</version>
</dependency>
<dependency>
<groupId>io.gatling</groupId>
<artifactId>gatling-recorder</artifactId>
<version>${gatling.version}</version>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>${scala.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>io.gatling</groupId>
<artifactId>gatling-maven-plugin</artifactId>
<version>${gatling.version}</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>execute</goal>
</goals>
</execution>
</executions>
<configuration>
<simulationsFolder>user-files/simulations</simulationsFolder>
<runDescription>MyFirstGatlingTest</runDescription>
</configuration>
</plugin>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>4.6.0</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<scalaVersion>${scala.version}</scalaVersion>
</configuration>
</plugin>
</plugins>
</build>
</project>
The key additions here are:
- Gatling Dependencies:
gatling-core,gatling-http, andgatling-recorderbring in the necessary Gatling libraries. - Scala Dependency:
scala-libraryis required because Gatling simulations are written in Scala. gatling-maven-plugin: This plugin allows you to run Gatling simulations directly from Maven. We’ve configured it to run during thetestphase and specified thesimulationsFolderwhere your Scala simulation files will reside.scala-maven-plugin: This plugin compiles your Scala code.
Now, create the directory structure for your simulations:
mkdir -p src/main/scala/user-files/simulations
Inside src/main/scala/user-files/simulations, create a new Scala file named BasicSimulation.scala. Replace the default content with a simple Gatling simulation:
import io.gatling.core.Predef._
import io.gatling.http.Predef._
import scala.concurrent.duration._
class BasicSimulation extends Simulation {
val httpProtocol = http
.baseUrl("http://localhost:8080") // Replace with your target base URL
.inferHtmlResources()
.acceptHeader("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
.acceptEncodingHeader("gzip, deflate")
.acceptLanguageHeader("en-US,en;q=0.5")
.userAgentHeader("Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:100.0) Gecko/20100101 Firefox/100.0")
val scn = scenario("My First Scenario")
.exec(http("request_1")
.get("/"))
.pause(5) // Pause for 5 seconds
.exec(http("request_2")
.get("/login"))
.pause(5)
setUp(
scn.inject(atOnceUsers(1)) // Inject 1 user at once
).protocols(httpProtocol)
}
This simulation defines a basic HTTP protocol targeting http://localhost:8080 and a scenario that makes two requests: one to the root (/) and another to /login. It then injects a single user into the scenario.
To run your Gatling simulation, execute the following Maven command in your project’s root directory:
mvn gatling:execute
This command will compile your Scala code, package it, and then run the Gatling simulation defined in BasicSimulation.scala. Gatling will launch, execute the scenario, and generate an HTML report in the target/gatling/ directory. Open the index.html file in this directory to view the results.
The setUp block is where you define the load profile. atOnceUsers(1) is the simplest injection, suitable for initial testing. For more realistic load, you’d use rampUsers or constantUsersPerSec, for example:
setUp(
scn.inject(
rampUsers(100) during (30 seconds), // Ramp up to 100 users over 30 seconds
constantUsersPerSec(10) during (60 seconds) // Maintain 10 users per second for 60 seconds
)
).protocols(httpProtocol)
The simulationsFolder configuration in pom.xml tells the Gatling plugin where to find your simulation files. By default, it’s user-files/simulations. The runDescription in the plugin configuration sets a prefix for the generated reports.
After this, you’ll likely want to explore more advanced Gatling features like feeders for dynamic data, checks for validating responses, and different injection profiles.