Filed under: Load testing, Scala, Test automation, Tools, — Tags: Gatling — Thomas Sundberg — 2016-08-04
The documentation for Gatling tells us that "Gatling is an open-source load testing framework based on Scala, Akka and Netty". I have heard good things about it but never tried it.
The technical parts, Scala, Akka and Netty, were less interesting from my perspective. The interesting part was its developer-friendly DSL, Domain Specific Language. I have used tools like JMeter and I have never been impressed by their user interfaces. They are too complicated. As a developer, I like when I can specify my load script using my regular tooling. That is, an editor, a build tool and a command line.
The developer-friendly DSL is Scala. This means that I have the power of a complete programming language when I write my load script. I haven't used Scala before, but it turned out that it wasn't very difficult. I started from their demonstration project and was able to get something up and running in a few minutes, something I can run from a command line using Maven. The demo project I started from turned out be be overly complicated and I was able to remove lots of things. Things that probably are interesting, but not for me as an absolute beginner.
Using Maven also allows me to easily include load testing in a build of any Maven based project I want. Allowing load testing to be a part of the regular CI build is not only possible, it is trivial.
An example test script looks like this:
src/test/scala/se/thinkcode/LoadtestScript.scala
package se.thinkcode import io.gatling.core.Predef._ import io.gatling.core.scenario.Simulation import io.gatling.http.Predef._ import scala.concurrent.duration._ class LoadtestScript extends Simulation { val httpConf = http .baseURL("http://computer-database.gatling.io") .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.8; rv:16.0) Gecko/20100101 Firefox/16.0") val computerDatabaseScenario = scenario("Filter") .exec(http("Filter computers") .get("/computers") .formParam("f", "Macbook")) .pause(200 milliseconds, 1000 milliseconds) .exec(http("Look at a Macbook") .get("/computers/473")) setUp( computerDatabaseScenario.inject( constantUsersPerSec(3) during (10 seconds) ).protocols(httpConf) ) }
The details to notice here are
The execution of this script is done using a Maven plugin. A complete Maven project look like this:
pom.xml
<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>se.thinkcode.blog</groupId> <artifactId>gatling-load-testing</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>io.gatling.highcharts</groupId> <artifactId>gatling-charts-highcharts</artifactId> <version>2.2.2</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>net.alchim31.maven</groupId> <artifactId>scala-maven-plugin</artifactId> <version>3.2.2</version> </plugin> <plugin> <groupId>io.gatling</groupId> <artifactId>gatling-maven-plugin</artifactId> <version>2.2.0</version> <executions> <execution> <phase>integration-test</phase> <goals> <goal>execute</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
I have attached the execution to the integration-test
phase. This allows me to execute the load test
during a regular Maven build. To execute this project, call Maven like this:
mvn install
This will execute the load test and generate a nice report of the result.
The file structure I used looks like this:
gatling-load-testing |-- pom.xml `-- src `-- test `-- scala `-- se `-- thinkcode `-- LoadtestScript.scala
The execution above will create a test report in target/gatling/loadtestscrip-
.
The documentation was a bit hard to find, the pages I had most help from were these: