Diving deep into Apache JMeter!
By: Anubhav Deep
Tags: Jmeter, Testing, Load Testing, QA, API

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! 🚀

Timers

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.

Deviation 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.

Effect of adding a Gaussian Random Timer

Assertions

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 Headers Request data Redirects

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.

Response assertion to check if the status code returned is 200

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).

HTTP Managers

Photo by Richy Great on Unsplash

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

: To provide element name Comments : To provide arbitrary comments (if any)

: 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: