TESTEROPS

A pragmatic approach to QA and OPS

Gatling with Typescript

In the realm of performance testing.

No! scratch that. I’m not gonna spoon feed your people with some ChatGPT generated BS. I am going to write my own experience about this.

I don’t know about you guys but when I hear the word Gatling, this comes to my mind –

Photo by Orbital 101 Studio on Pexels.com

The famous Gatling gun. Designed by Richard J Gatling in 1861 and patented in 1862, Gatling gun was designed as a rapid fire multi barrel gun for firing multiple rounds very quickly at the enemy. Still to this day, this rapid fire arm exists and is still very well known – you can see it in the video about how current attack helicopters use this.

If you compare this to what a lot of folks do in load testing – doesn’t it sound familiar? I mean, here too, lots of requests are being fired at the server, at some specific hit rate. Off course, the main intent is not to kill someone ( or some server ).

Features

Gatling has some very unique features to offer –

  • Async nature and lightweight threads : Under the hood, Gatling utilises the Akka toolkit, which allows Gatling to simulate multiple users in a single thread. Due to this, using Gatling you can simulate very large payloads on a single system without fussing too much about resources.
  • Support for various protocols : Gatling supports a lot of protocols – HTTP/HTTPs, Websockets for the open source version. MQTT and gRPC are supported in Enterprise version.
  • Reports and Assertions : Gatling gives a detailed HTML report out of the box and also provides nice detailed assertions to be applied on the response.
  • Friendly GUI : If you’re not very keen on the scripting, there is a nice GUI interface, that lets you record your scenarios and then generate simulation scripts
  • Language support : Out of the box, Gatling supports Scala scripting, but with version 3.11+, there is a new JS/TS JDK also released – which we will talk about in this article.

Gatling with Typescript

With the release of Gatling v3.11, there was a new JS/TS JDK that was released. I am going to use that to create some simple tests in Gatling. I’ll be using the same Gatling computer platform that is being used by Gatling team to demo the various features.

Let’s dive into the code.

Github Repo

First, I’ll create a new Github repository. I think in that way I can keep a check on the amount of code that I’ve done and also it allows me to keep a track of what changes are done in the repo. There are other benefits –

I’ve created this repo – https://github.com/testerops01/gatling_w_ts.git so I’ll be posting my code in this repo.

Clone this repo in a directory of your choice using git clone command and then open the directory in a Visual Studio Code IDE.

Initialise

Let’s start by creating a new package.json file – to do so, run npm init in a new terminal instance ( In VS Code -> Terminal -> New Terminal )

I’ve selected mostly the default options given and then entered yes when it asked to confirm

Packages and Dependencies

Let’s add required packages to the project. We’ll need the following packages

  • @gatling.io/cli
  • @gatling.io/core
  • @gatling.io/http
  • prettier
  • rimraf
  • typescript

So lets install this packages using

npm install typescript rimraf prettier @gatling.io/cli @gatling.io/core @gatling.io/http

Now let’s create a tsconfig.json file and then add the following in it

{
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "target",
    "lib": ["es2021", "dom"],
    "target": "es2021",
//    "module": "node16",
//    "moduleResolution": "node16",
    "module": "ES2020",
    "moduleResolution": "Node",
//    "esModuleInterop": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true
  },
  "include": ["tests/**/*.ts"]
}

Write First Test

Now let’s first test with Gatling and Typescript. We’ll start by creating a src folder in the project root. We will create our first test first_test.gatling.ts file inside this src folder.

Libraries Import

Now we first need to import couple of libraries that we would need

First Simulation

To start with the simulation ( as it is called in Gatling), we would need to export a simulation in our file. Let’s do that

The simulation function takes a function as argument, which is setUp. The setUp method actually lets you define what protocol to be used, what is the number of users and time period you want to run your test – we’ll see this in later stages.

Add the Protocol

Now, lets add a simple http protocol related things in the simulation function. Here we can define the baseURL, the headers, the auth etc. We will set the baseURL, plus some headers.

In Gatling, you can use the default methods like acceptHeader, or contentTypeHeader or you can use the header method to add a custom header

So this is also possible

and this is also possible – there are some custom methods already defined

Define the scenario

Next step is to give the test the actual test scenario. So let’s say in this scenario, we want to hit the /computers/ path in the URL and perform some actions. So this is how we would define it using scenario

You can name the scenario ( also in fact you can name your request as well ) and then use the appropriate http method like get, put , post etc. to perform the actions.

The scenarios must have an exec() method so that it is an executable scenario to be attached with the test.

Setup and Injection

In the last step, we will define the setUp method that we had passed in the simulation function.

In the setUp method, we have passed the scenario that we are testing, and then we have used the injectOpen method to pass on how we want the load to be done on the website.

Gatling supports two work load models – Open and Closed. To read more about which one is more suitable for you, head over to the injection documentation .

The constantUsersPerSec is very self explanatory – we want to simulate a traffic of 2 new users hitting the website for 60 seconds, and we want to use the httpProtocol that we defined earlier.

Run tests

In order to run tests, we need to make two changes in the package.json file

  • add type : module in the json file
  • add "main": "target/bundle",

Once added, run this command

npx gatling run --simulation first_test

This will begin the execution, with some installation of GraalVM and Coursier. Once done, the execution will start

Once the report is generated, it will show the same in the terminal

Reports

Once the reports is generated inside the target directory, you can open it in a HTML view – using Chrome or any other browser and it will show the report

So this is how you can run a sample test with Gatling , using Typescript. This support came in the 3.11 release. So if you’re running any version before that, I am not sure if this is supported.

In the next tutorial, I will blog about how to run multiple scenarios in a single test and how to feed data using external files in Gatling.

You can find the code snippets used in this blog in this Github repo.