How to Write your Own Load Test in Gatling

In the previous tutorial, we’ve learned how to record a test scenario, how to edit simulation and how to run the simulation. In this tutorial, we are going to learn how to write our own test using Gatling DSL. In some cases, we want to test our product at the API level, or there might be some functionality(ies) on our web page that we would like to test but could not call them by any UI element. In those cases, we have to write our own load tests. This is done by Gatling DSL (Domain Specific Language) which is based on Scala. But do not worry! You do not have to be a Scala expert to write your own tests.

Let’s start by learning the structure of the test we are going to write!

Gatling Load Test Structure

To have a better understanding of Gatling, we can divide the Gatling load test structure into 4.

Protocol configuration

If you look at the test you have recorded, you will see the configuration of the protocol on top of the class (i.e val httpProtocol). Though Gatling supports few protocols, we will talk about the most commonly used one, HTTP.

On this configuration, we mainly define our base URL and additionally, we can define accept header and user agent etc…

Assume we are writing a test that navigates to amazon.com and does some actions there like shopping, adding to favorites etc. Our base URL will obviously be http://www.amazon.com. To configure our protocol (HTTP, we need to define a value;

val httpProtocol = http.baseUrl("http://www.amazon.com")

Do not forget to put ’http’/’https’ in front of the URL! Otherwise, Gatling would not navigate to the URL.

Header configuration

This part is the configuration of the header of our request.

You may define the header of your request (s) as a value (val) since you may want to use these headers multiple. For instance, assume we have an accept header. The header can be defined as a map;

val headers = Map("Accept" -> "text/html")

or if your header bears more information you can enhance your map;

val header_0 = Map("Content-Type -> "application/javascript", "Accept" -> "text/html")

Scenario

As the name suggests, this is the part where we are going to define the actions we would like to do. In this part, a group of actions is implemented that the user(s) will do.

This is again done by defining a value;

val scn = scenario("Name_of_the_scenario")

each action group is done by adding exec to this scenario;

.exec(http("Name_of_the_action"))
.get("/")
)

This action, as you might guess, will get the defined baseURL. We will see how to write a chain of actions in the next section.

Simulation

We have defined our protocol, header, and scenario so the last step to do is to simulate the user(s). In this part, we will define how our scenario will be simulated.

You may select how to inject users into your scenario according to the type of your load test.

This injection is done by the setup function. For instance, let’s assume we will just inject a simple user, we need to use atOnceUsers;

setUp(scn.inject(atOnceUsers(1))

This will create a user at the beginning of the simulation. You may increase the number to increase the number of users injected at once.

For injecting a number of users in a given time we might use rampUsers;

setUp(scn.inject(rampUsers(10) over (5 seconds)))

You can find the entire simulation setup here.

Scenario-1:

URL: http://www.swtestacademy.com/comment

Method: POST

Protocol: http/1.1

Status: Complete

Response: 200 OK

SSL: Yes

Accept-Language: en-US

Accept: text/html

User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)

Scenario-2:

URL: http://www.swtestacademy.com/category/selenium

Method: GET

Protocol: http/1.1

Status: Complete

Response: 200 OK

SSL: Yes

Content-Type: application/json;charset=UTF-8

User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)

Writing our own tests with Gatling

You may use any editor for writing your tests. For this tutorial, I am going to use Visual Studio Code with ‘scala’ plugin. Create your new file within your editor. Do not forget to save it with .scala extension. You may name it whatever you want, I named it ‘FirstScalaTest.scala’.

I think working with a real-life alike example would benefit you the most for you to understand how you should use Gatling.

Imagine you are given two different scenarios like below:

Those two requests are imaginary and will not work but they will help us learn how to write our own API level tests.

First thing first, we need to import the main Gatling libraries. Those two libraries are necessary for writing a test in GatlingDSL:

import io.gatling.core.Predef._ 
import io.gatling.http.Predef_

Now we are ready to define our class:

class FirstScalaTests extends Simulation {

As you can see, the test class must extend ‘Simulation’ class otherwise Gatling will not realize that it is a test and your test file will not be recognized.

Next, let’s start with the parts we have mentioned above. The first part is protocol configuration. We should define our base URL.

val baseUrl = “http://www.swtestacademy.com”

This is our baseUrl we will add the requested parts later on whilst adding HTTP methods.

Onwards to httpProtocol:

val httpProtocol = htppProtocol 
.baseUrl(baseUrl) 
.userAgentHeader("Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)")

Since the two scenarios are using the same user-agent header it is better to define it here.

It is time to define our headers:

val header_0 = Map("Accept" -> "text/html", “Accept-Language” -> “en-US”)

And for the second scenario:

val header_1 = Map("Content-Type" -> "application/json",”Accept-Charset” -> “utf-8”)

And then comes the scenarios. Depending on your needs, you might need to run them either in a sequence,  separatarely or in parallel. In this example, I am going to run them in parallel.

val scn1 = scenario("Scenario 1") 
.exec(http("Post comment") .post("/comment") 
.header(header_0) .check(status.is(200)) 
) 

val scn2 = scenario("Scenario 2") 
.exec(http("Get Selenium articles") 
.get("/category/selenium") .header(header_1) 
.check(status.not(404), status.not(500)) 
)

Now we can run this scenario with any simulation we want. For this tutorial, I will just use one user at a time. You may take a look at official documention for other types of simulations.

setUp(scn1.inject(atOnceUsers(1)).protocols(httpProtocol), 
scn2.inject(atOnceUsers(2)).protocols(httpProtocol) )

GitHub Project

https://github.com/swtestacademy/ASimpleLoadTest

Congrats! You have created your own Gatling load test!

Thanks
Ege Aksoz

4 thoughts on “How to Write your Own Load Test in Gatling”

  1. Hi Ege,
    Thanks for the clean explanation. My question is does gatling starts up your
    swtestacademy API service or one has to have their API hosted and running and recorded by the gatling recorder somewhere before they run the performance test?
    My understanding is that one doesn’t need to do all these before the performance testing because what if as a developer, I only know the base url and the resources but I don’t know anything else about the API and how to start it.

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.