Programster's Blog

Tutorials focusing on Linux, programming, and open-source

Stress Test Your Webserver With Gatling

Steps

Create the necessary directories for gatling to operate in:

mkdir -p $HOME/gatling/conf
mkdir -p $HOME/gatling/user-files/simulations
mkdir -p $HOME/gatling/results

Create A Test Configuration

We need to create a "simulation" file for telling Gatling what we want to do in our test. E.g hit our site with 100 users at once.

Create a file at $HOME/gatling/user-files/simulations/MyBasicSimulation.scala with the following contents.

package computerdatabase // 1

import io.gatling.core.Predef._ // 2
import io.gatling.http.Predef._
import scala.concurrent.duration._

class BasicSimulation extends Simulation 
{
  // get the number of users specified, falls back to 1 if not specified
  val numUsers = Integer.getInteger("users", 1)

  // get the URL specified
  val siteUrl = System.getProperty("siteUrl");

  val httpConf = http.baseUrl(siteUrl)
    .inferHtmlResources()
    .acceptHeader("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
    .doNotTrackHeader("1")
    .acceptLanguageHeader("en-US,en;q=0.5")
    .acceptEncodingHeader("gzip, deflate")
    .userAgentHeader("Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0")

  val scn = scenario("MyBasicSimulation")
    .exec(http("request_1")
    .get("/"))
    .pause(5)

  setUp(scn.inject(atOnceUsers(numUsers))).protocols(httpConf)
}

Run The Test!

Pull the latest version of Gatling with:

docker pull denvazh/gatling:latest

3.2.1 is the latest image at the time of writing this post.

Now execute the following command after editing the siteUrl and users variables to be the site you wish to hit, and the number of users you wish to hit it with respectively.

docker run \
  -e JAVA_OPTS="-Dusers=20 -DsiteUrl=https://www.mydomain.com/" \
  -it \
  --rm \
  -v $HOME/gatling/conf:/opt/gatling/conf \
  -v $HOME/gatling/user-files:/opt/gatling/user-files \
  -v $HOME/gatling/results:/opt/gatling/results \
  denvazh/gatling

The docker container will run, and eventually ask you to enter a name for your test. Enter a name and leave it to run.

Check Out The Results

When the test finishes, you will be told that the results can be accessed at a location. Obviously this location is wrong as you need to take into account how you mounted the volumes. It will be within your gatling/results volume.

Double click the index.html file and view the results in your browser.

References

Last updated: 30th October 2019
First published: 29th October 2019