DEV Community

Cover image for Load-Test-as-Code for Non-Coders!
Kaye Alvarado for Developers @ Asurion

Posted on

Load-Test-as-Code for Non-Coders!

This is the third part of the Load Testing with Gatling series. So far, I've talked about the general solution of doing load testing with simple code. But what if I am not a coder and don't want to code at all?

For web applications, users normally test scenarios by clicking multiple things from the browser, inputting some values, submitting forms, etc. To simulate multiple users doing the same thing, over and over again, we'll normally write a request scenario for each action the user performs, then inject this in the gatling code.

Image description

In a complex web application with multiple test scenarios, this will be very tricky, and time-consuming to do. No one wants to manually write 1000 test scenarios, even if you know how to!

Gatling Recorder

Introducing the Gatling Recorder. This java program helps create a simulation code for you, by either acting as a HTTP proxy between browser and server, or converting HAR (Http ARchive) files to scala code.

Image description

But What is a HAR file? 😭

The HTTP Archive format, or HAR, is a JSON-formatted archive file format for logging of a web browser's interaction with a site.

How Do I Do This?

Let's create a very simple scenario in the browser and figure out how to create a simulation code for this. Let's assume that multiple people are doing a quick google search of the word hello.

First open your chrome browser, then click on the three dots in the upper-right hand corner, select More Tools, then select Developer Tools. From the right pane that opens up, click on the Network Tab. This shows all traffic passing through as you browse through the internet.

You'll notice doing any action in the browser triggers a few requests in the backend. Even a simple image display will show a GET request.

Image description

Now, try to search for the word hello, and hit the enter key. In the background, this triggered the GET /search API of Since there's a lot of other requests triggered, let's add a filter to only show requests related to search. Right below the recorder button, there is a filter text box. Type in search? here to filter only APIs with this string.

Image description

Let's keep it simple for now and limit the simulation to the three requests that shows up. In the upper right part of the Developer Tools pane, there is a download button named Export HAR....

Image description

Save this file somewhere in your machine with a .har extension.

Image description

Now, go to your gatling code. In the bin directory, you should be able to see a file named Run this script.

$cd bin
GATLING_HOME is set to /Users/karina.alvarado/workspace
Warning: the fonts "Times" and "Lucida Bright" are not available for the Java logical font "Serif", which may have unexpected appearance or behavior. Re-enable the "Times" font to remove this warning.
Enter fullscreen mode Exit fullscreen mode

This will open up the Gatling Recorder window as shown in the upper part of this article. In the upper-right hand side of the window, there are two options of recorder mode: HTTP Proxy, and HAR Converter. Select HAR Converter since we want to create the simulation code from the HAR file that we exported from the browser previously.

In the Http Archive (HAR) Import section, browse for the file search.har created earlier. Update the package name and class name in the Simulation Information section, and update other resources as needed. Lastly, select the simulations folder where your code will be generated. Once you are satisfied with all the other configurations, click on Start button. It will then notify you that the simulation code has been created.

Image description

When you open up this file, notice that it automatically contains code with all the requests in your HAR file. In case it included other requests that you don't want to include in your testing, you can remove it from the code.

Image description

I've highlighted the important parts of the code that should appear based on the filter that we applied.

package loadtestpackage

import scala.concurrent.duration._

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

class LoadTestSimulation6 extends Simulation {

    val httpProtocol = http
    val scn = scenario("LoadTestSimulation6")


Enter fullscreen mode Exit fullscreen mode

And that's it! You can update a few parts of the code like the injection part of it before running the simulation in your pipeline. Essentially, you can simulate different actions in your browser manually and have Gatling Recorder generate the code for you.

What other useful features of Gatling have you tried? Let me know in the comments!


Top comments (0)