Before getting started, the prerequisites for this blog are knowing the basics of Apache JMeter and the basic components necessary to run a load test. If you are new to JMeter and want to learn about the basics and how to run your first test in JMeter, please refer to my blog which sets the backdrop for this blog.
Let's get started! 🚀
Well, if you write a normal test in JMeter it looks like a robot sending multiple requests in an even manner over a given period of time frame and conditions. Believe me or not, that is what it is. What if we want to replicate real-life scenarios and introduce some randomness to the testing scenario? That is where timers come in.
Timers help to introduce randomness to the test. They help us configure the congestion of the requests in an arbitrary way such that it replicates a real-life scenario.
LOAD TESTING != Test Automation
In test automation, we want the tests to complete as soon as possible, whereas in load testing we subject the APIs to a load that emulates the real-life human interaction with the API. There are various types of timers available in Jmeter and you can have a look at them by doing a right-click on the test plan and then clicking on timers.
For our example here, we will move on with Gaussian Random Timer. Gaussian random timer delays each user request for a random amount of time. When you try to configure it, we need to configure two fields which help introduce randomness to the test.
- Constant Delay Offset
Let's set up deviation as 0.5 seconds and constant delay offset as 1 second. What this essentially would mean is it would introduce randomness between each step of the loop. After each action, the user (which is our test here) would pause between 0.5 to 1.5 seconds randomly before performing the next action.
As the name suggests and if you have written any unit tests, this part might already be clear. An Assertion in JMeter is used to validate the response of the request, that you have sent to the server. An assertion is a process where you verify the expected result with the actual result of the request at run time.
There are various things on which we can add assertions upon:
- Text within a site or url
- Response code
- Request data
How to add assertions?
Right-click on the component where you want to add assertions into, for our example, we will add it to the HTTP Request. In our example, we will be adding a Response assertion.
The response assertion lets you add pattern strings to be compared against various fields of the server response.
For example, you send a user request to the website http://www.google.com and get the server response. You can use Response Assertion to verify if the server response contains the expected pattern string (e.g. “OK” or 200).
Since JMeter is not a browser and HTTP protocols could be complex sometimes, it makes use of HTTP managers to get some assistance with HTTP-related issues. HTTP managers allow for the management and manipulation of things like cache, cookies, and headers. Sometimes, it is difficult to run HTTP load tests without having them configured.
The browser sends an HTTP request header with some additional information to the server. This additional information is required to fulfill the server requirements to respond to that particular request. Each time the browser sends a request to a server attaching the headers with information like Accept-Language, Accept-Encoding, User-Agent, Referer, etc.
JMeter provides an ‘HTTP Header Manager’ element to attach that additional information along with the request.
To add an HTTP manager, right-click on the test plan and go to config elements:
There are various types available here:
- HTTP Cache Manager: It helps manipulate the cache sent with requests, preserve or delete a cache during the test.
- HTTP Cookie Manager: It helps manage the cookies used within the HTTP request. It also helps with authentication.
- HTTP Header Manager: It helps manage the headers of the outbound HTTP request. We can store them for later use.
Adding these helps us manage an HTTP request far better.
Some of the important input fields of an HTTP header manager are mentioned here:
- Name: To provide element name
- Comments: To provide arbitrary comments (if any)
- Headers Stored in the Header Manager: List of the header fields which will be passed with the request.
You can add below generic header fields:
- Accept-Language: It defines the language of the page that the browser prefers to receive in the response. e.g en-GB,en-US;q=0.8,en;q=0.6
- Accept-Encoding: The accept-encoding header tells the server which coding method the server should select before sending the response to a client. e.g. gzip, deflate
- User-Agent: The User-Agent allows to define the type of client and receiving page version that will be correctly displayed in the browser. e.g. Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36
- Referer: It shows that the particular request was sent while switching from a certain page. e.g. When you send a referral URL to your friend to sign-up (like Uber) and his sign-up gives you free rides. The referrer URL contains your code and benefited you.
- Content-Type: It shows the type of content. e.g. application/JSON;charset=UTF-8
- Accept: The type of response which browser accepts and display without any issue. e.g. application/JSON, text/plain, */*
- Origin: The parent URL
- <security check>: This option is available only for secured communication. The tag name could be cfadmin, csrftoken, Authorization etc.
I hope this was great for people who wanted to customize and personalize their JMeter test plans to the next level. I will try to highlight Jmeter CLI in the next one. For now, let’s end it here. Reach out to me on Twitter if you want to give feedback or have any queries. Peace 👋