Gatling Introduction For Java Section-B
Gatling Introduction For Java Section-B
Introduction for
Java Users
B1 - Section Overview 3
B3 - Gatling Recorder 5
HTTP Proxy Recorder Mode 6
HAR Converter Mode 10
The application has a few typical features that you would expect such as:
Have a look around the application, then we can move on to using the Gatling recorder to
create a Gatling script.
NOTE: The application is running in “read-only” mode. This means that any changes you
(i.e. creating a computer, updating a computer etc.) are not persisted on the backend and
hence won’t be visible in the application.
This has no impact on the Gatling scripts that you will write against this application, which
are ultimately for training purposes.
If you downloaded Gatling directly from the Gatling website, then you can launch the
recorder by browsing into the /bin directory and running recorder.bat if you are on Windows
or recorder.sh if you are on a Mac.
Alternatively, if you are using Gatling through a build tool directly in the IDE, you can launch
the recorder by simply running the Recorder class directly:
Gatling Recorder
Refer to the Gatling documentation for full details on the Gatling recorder, including how to
run in different HTTPS modes.
For our application here, we will be leaving all the options as default, but we should click No
static resources in the bottom-right corner. This will ensure that we don’t capture unwanted
transactions such as JavaScript and images.
Click Start and the Gatling recorder will begin to run, capturing traffic on port 8000.
The next thing to do is to configure our web browser to use this proxy. The Firefox web
browser works best for this, as it is more lenient with it’s HTTPS recording settings than
some of the other browsers.
Open your browser settings in Firefox and configure the Network Settings as below:
The Computer Database website should now load, and we can begin to capture our user
journey as normal. Before we capture each transaction, it can be a good practice to add a
tag in the Gatling Recorder:
A tag will add a comment into our generated Gatling script, making it easier to understand
which transaction we have captured.
You can now record your user journey through the application. A few ideas of transactions to
record are:
Browse to that folder on your computer, and you can open the script in a text editor to take a
look at it.
We will go over the Gatling script that we have captured here shortly.
The second recorder mode is HAR Converter mode. HAR stands for HTTP Archive File, and
these files can be generated from traffic in your web browser.
This mode is a lot easier to use for HTTPS recordings, as it doesn’t require the setup of any
special certificate
To generate a HAR file, open the Google Chrome browser and head over to the Gatling
Computer Database website. Open Developer Tools in Chrome and click on the Network
tab, make sure the red recording icon is displayed, and preserve log is ticked:
Refresh the page you are on to capture the traffic of loading that page. You should see the
traffic start to appear in dev tools.
Save this file somewhere on your computer that you will remember.
Once you have located the file, click the No static resources button in the Gatling recorder.
This will make sure that the generated file doesn’t have any unwanted transactions such as
downloading images or JavaScript files
Now click Start in the Gatling recorder. The recorder will now generate a Gatling script and
save it in the simulations folder that is specified under output. You can open this script now
in a text editor to look around and begin editing.
When you open the script for the first time, it will likely look something like the one below:
import java.time.Duration;
import java.util.*;
import io.gatling.javaapi.core.*;
import io.gatling.javaapi.http.*;
import io.gatling.javaapi.jdbc.*;
.acceptHeader("text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,
image/webp,*/*;q=0.8")
.acceptEncodingHeader("gzip, deflate")
.acceptLanguageHeader("en-GB,en;q=0.5")
.upgradeInsecureRequestsHeader("1")
.userAgentHeader("Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:98.0)
Gecko/20100101 Firefox/98.0");
// … other headers
{
setUp(scn.injectOpen(atOnceUsers(1))).protocols(httpProtocol);
}
Tidy Headers
A good first step is to tidy up the headers in the script, as there is a lot of unnecessary data
captured here that we don’t need. A better practice is to either add headers under the
httpProtocol, or to each individual transaction.
Go through the script and delete all the val headers entries, as well as the corresponding
headers under each transaction name. This will make our script a lot more readable.
1. Protocol setup - this is where we set the base URL, and also any defaults that we
want to use throughout all the calls in the script. For example, if we wanted a certain
HTTP header to be included in every transaction, we would add it here.
3. Load Simulation Design - the setUp block at the end is the load simulation design.
This is where we set the number of users to execute, the speed in which to ramp
those users etc.
To make our script more readable, and to give ourselves the possibility of reusing code
within our script, a good practice is to refactor these steps into their own static constants. For
example, with the search steps above, we can refactor into a static constant with a
ChainBuilder like this:
ChainBuilder searchForComputer =
exec(http("LoadHomePage")
.get("/computers"))
.pause(2)
.exec(http("SearchComputers")
.get("/computers?f=MacBook")
.pause(2)
.exec(http("SelectComputer")
.get("/computers/89"))
.pause(2);
We can then call those scenarios in our load simulation design like so:
{
setUp(
admins.injectOpen(atOnceUsers(1)),
users.injectOpen(atOnceUsers(1))
)
.protocols(httpProtocol);
}
You can read much more about scenario creation in Gatling in the documentation -
https://gatling.io/docs/current/general/scenario/
Gatling has feeders for this purpose, and you can read more about them in the
documentation here - https://gatling.io/docs/gatling/reference/current/core/session/feeder/
In our script, we are going to create a CSV file in the resources/data folder of our project
called search.csv. We can add the following data to it:
searchCriterion,searchComputerName
MacBook,MacBook Pro
Eee,ASUS Eee PC 1005PE
HP,HP Mini 1000
The CSV file has 2 headings (searchCriterion and searchComputerName), and 3 rows of
test data under each of those headings.
We can then add this CSV file into our script and hook it up to our search Transaction like
so:
FeederBuilder.Batchable searchFeeder =
csv("data/search.csv").random();
ChainBuilder searchForComputer =
exec(http("LoadHomePage")
.get("/computers"))
.pause(2)
.feed(searchFeeder)
.exec(http("SearchComputers_#{searchCriterion}")
.get("/computers?f=#{searchCriterion}")
Now instead of searching for a hard-coded computer name, Gatling will take a random value
for #{searchCriterion} from the test data and insert it into the script.
Further down in our search method, we can see that we have hardcoded the computer that
we are selecting as /computers/89 :
It would be better if we could extract the URL to access here from the previous request (i.e.
when we search for a computer). We can do this extraction through a process called
correlation by using a check in Gatling.
For our script, we are going to use a CSS check to extract the URL of the computer we are
searching for, save it into a parameter called computerURL, then use that parameter in a
subsequent call:
ChainBuilder searchForComputer =
exec(http("LoadHomePage")
.get("/computers"))
.pause(2)
.feed(searchFeeder)