Automate your Load Testing with Gatling - THE SEQUEL!
Tags: testing, tutorial, automation, gatling
Published: 2020-09-21 02:08:00

This post is a sequel to [my first Gatling post](https://dev.to/leading-edje/automated-performance-testing-with-gatling-a04).  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](https://dev.to/leading-edje/automated-performance-testing-with-gatling-a04) 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
```
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](https://dev-to-uploads.s3.amazonaws.com/i/0xauadqwg0icnbfyxwve.png)

And you may also need to mark your `src/test` folder as Test Sources Root.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/08inx7xfncx32xbc98y1.png)

Take a look at your `pom.xml` file.  There you will see the dependencies for Gatling.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/iyv7zdjrmqr8dihdvj66.png)

# Running the recorder
In my [first post](https://dev.to/leading-edje/automated-performance-testing-with-gatling-a04) 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](https://dev-to-uploads.s3.amazonaws.com/i/bmmpymeqafwoe4qdprih.png)

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!  <small>(See the [previous post](https://dev.to/leading-edje/automated-performance-testing-with-gatling-a04) for a demo of the Recorder.)</small>

# 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](https://www.jetbrains.com/idea/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](https://gatling.io/docs/current/general/simulation_setup/) for more details.

And there we have it.  The final product should look like this:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/uvrp5boa7ucba8j6af6x.png)

# 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
```
Another option is to run `src/test/resources/Engine.scala`.  This is the same utility we used in the [first post](https://dev.to/leading-edje/automated-performance-testing-with-gatling-a04) 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](https://dev-to-uploads.s3.amazonaws.com/i/t45v78rbctjlwzqr2qrn.png)

# 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.

<a href="https://dev.to/leading-edje">
  ![Smart EDJE Image](https://dev-to-uploads.s3.amazonaws.com/i/5uo60qforg9yqdpgzncq.png)
<a/>