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.
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.
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.
Now, try to search for the word hello
, and hit the enter key. In the background, this triggered the GET /search API of www.google.com
. 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.
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...
.
Save this file somewhere in your machine with a .har
extension.
Now, go to your gatling code. In the bin directory, you should be able to see a file named recorder.sh
. Run this script.
$cd bin
$./recorder.sh
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.
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.
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.
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
.baseUrl("https://www.google.com")
.inferHtmlResources()
val scn = scenario("LoadTestSimulation6")
.exec(http("request_5")
.get("/search?q=hello&rlz=1C5CHFA_enHK983HK983&oq=hello&aqs=chrome.0.69i59j46i433i512l2j0i433i512j0i131i433j46i199i433i465i512j46i131i433i512j0i433i512j0i512j0i271.2914j0j15&sourceid=chrome&ie=UTF-8")
.headers(headers_5),
http("request_18")
.get("/complete/search?q=hello&cp=0&client=desktop-gws-wiz-on-focus-serp&xssi=t&hl=en-PH&authuser=0&pq=hello&psi=mRB1Y7SrCIr8wQPUyJjYCQ.1668616346541&ofp=EAEyiQEKDwoNaGVsbG8gbWVhbmluZwoQCg5oZWxsbyBtYWdhemluZQoOCgxoZWxsbyBseXJpY3MKEAoOaGVsbG8gaW4gaGluZGkKDQoLaGVsbG8gd29ybGQKFQoTaGVsbG8gcHJvbnVuY2lhdGlvbgoMCgpoZWxsbyBzb25nCgwKCmhlbGxvIHNpbmcQRw&dpr=1")
.headers(headers_18),
http("request_19")
.get("/complete/search?q&cp=0&client=gws-wiz-serp&xssi=t&hl=en-PH&authuser=0&pq=hello&psi=mRB1Y7SrCIr8wQPUyJjYCQ.1668616346541&dpr=1")
.headers(headers_18)
)
setUp(scn.inject(atOnceUsers(1))).protocols(httpProtocol)
}
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!
References:
https://gatling.io/docs/gatling/reference/current/http/recorder/
Top comments (0)