Automate your Load Testing with Gatling - THE SEQUEL!

Dennis Whalen - Sep 21 '20 - - Dev Community

This post is a sequel to my first Gatling post. I know what you're thinking, isn't the sequel always lame? Well yes, that is usually true. Here's hoping I can reverse that trend! With this post we'll get a little deeper into the nitty gritty of Gatling.

My previous post started with a general overview of performance testing, and then dove into using the Gatling Recorder to build a Gatling script. In those examples we used the Gatling bundle, but there is another way.

The Gatling bundle is a standalone tool, and is not really the tool you'll use in when leveraging Gatling in an enterprise environment. Gatling can also be used with a build tool such as Maven, sbt, or Gradle. Let's bootstrap a new Maven project by using the Maven archetype.

Bootstrapping a new project for Gatling

You can use your IDE to create a new project using an archetype, but let's just do it from the command line:

mvn archetype:generate -DarchetypeGroupId=io.gatling.highcharts -DarchetypeArtifactId=gatling-highcharts-maven-archetype
Enter fullscreen mode Exit fullscreen mode

You'll be prompted to enter a groupId, artifactId, version, and package. Once you confirm your selections, a new project will be created. After that you may also need to setup your Scala SDK.
Alt Text

And you may also need to mark your src/test folder as Test Sources Root.
Alt Text

Take a look at your pom.xml file. There you will see the dependencies for Gatling.
Alt Text

Running the recorder

In my first post I opened the recorder from the bin folder of the Gatling bundle. With the Maven project you can open it by running src/test/resources/Recorder.scala. After a few seconds you should see the recorder utility, which can be used to record scripts. Once opened, the Recorder functionality is the same as I described previously. The Recorder utility should look like this:
Alt Text

I just showed you how to run the Recorder from the Maven project. So now it's time to use the Recorder right? Sorry no, plot twist!

The Recorder can be used to generate a script that will run, but of course it's important to understand what that generated code is doing. IMHO the Recorder alone is not sufficient to get you far with Gatling. Instead of using the Recorder, let's create a script from scratch! (See the previous post for a demo of the Recorder.)

Writing your first script from scratch

  1. To create the script I will be using the Community Edition of IntelliJ IDEA. If you don't already have it, you can download and install. Once installed, open the project you created previously.
  2. Create a new package in the src/test/scala folder named scripts.
  3. Create a new Scala class in that package named HelloGatling and open it for editing. This is going to be our script.
  4. There are 2 imports that are required for all Gatling scripts, so let's start by adding those to the class:

    import io.gatling.core.Predef._
    import io.gatling.http.Predef._
    
  5. Your class will need to extend the Gatling Simulation class to make it into a Gatling script:

    class HelloGatling extends Simulation {
    
    }
    
  6. Now we're ready to start working on our class. Let's start with the http config, which defines the base URL and will sets up our header to send json formatted data.

    val httpConfig = http
        .baseUrl("http://computer-database.gatling.io")
        .header("Accept", "application/json")
    
  7. Now we're ready to build the scenario, which will describe all the interactions our script makes with the application. For this demo our script will access the Gatling sample web site. Let's first access the home page:

    val myScenario = scenario("Add a new computer scenario")
       .exec(
         http("load the Home Page")
           .get("/")
       )
    

    With this code we have created a scenario with a single step. We give text descriptions for the scenario and for the step. These descriptions will show up on the Gatling report.
    The get statement accesses the Home page.

  8. Now we can add the remaining steps that [1]access the Add New Computer page and then [2]post the new computer. The complete scenario looks like this:

    val myScenario = scenario("Add a new computer scenario")
      .exec(
        http("load the Home Page")
          .get("/")
      )
      .pause(5)
    
      .exec(
        http("get the Add New Computer page")
          .get("/computers/new")
      )
      .pause(5,10)
    
      .exec(
        http("Post the new computer")
          .post("/computers")
          .formParam("name", "Ionic Defibulizer")
          .formParam("introduced", "2020-01-01")
          .formParam("discontinued", "2020-06-30")
          .formParam("company", "37")
      )
    

    In addition to the 2 new steps, we also have also added a pause between them. pause(5) will pause 5 seconds and pause(5,10) will pause a random amount of time between 5 and 10 seconds. Pause time is there to simulate how a user will interact with the page. Realistic pause time is critical to building realistic scenarios.

  9. OK we have our scenario built. Now we need to define how our users will be added. For this script we are going to run 10 concurrent users for 30 seconds:
    setUp(myScenario.inject(constantUsersPerSec(10) during (30 seconds)).protocols(httpConfig))
    We'll also need to import the DurationInt from Scala so that seconds will resolve:

    import scala.concurrent.duration.DurationInt
    

    Gatling provides a number of ways to add users. Checkout out their documentation for more details.

And there we have it. The final product should look like this:
Alt Text

Running the script

We have a couple of ways to run the script. The easiest way is likely from the command line with Maven:

mvn gatling:test -Dgatling.simulationClass=scala.scripts.HelloGatling
Enter fullscreen mode Exit fullscreen mode

Another option is to run src/test/resources/Engine.scala. This is the same utility we used in the first post when we ran Gatling from the bin folder.

Regardless of the method you choose, your newly created script should run and the results should look something like this:
Alt Text

Wrap-up

So there is your first script from scratch. It's not a whole lot different from the recorded script but hopefully you have a better understanding of how to create a new Gatling project with Maven, and some clarity on the inner workings of a script.


Smart EDJE Image

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .