Complete Load Testing with apache Jmeter !

Badri Paudel
9 min readMay 19, 2022

Getting information about how well your application is doing is crucial thing for software developers or QA. In this article I am going to give a complete load testing overview with some of the tips and tricks including generating reports from those scripts you will write for your APIs.

Apache Jmeter is the open source pure java based application designed to test the performance of the web application even though it has been expanded in other areas as well. In order to follow this tutorial you’ve to download apache download and run.

Depending on where you’ve installed the meter, you can simply navigate to that folder and type jmeter it will launch the Jmeter UI on your computer which looks very similar to this.

launching apache jmeter

Once you get the window just like above you’re ready to write load testing for your APIs or endpoints. So, let’s understand this step by step.

To begin with writing the test for our APIs, let’s add a thread group first. Thread group is basically the top level element that will hold controllers, samplers etc. So to add thread group do the following as shown in screenshot and give the name you like. I will give guidestocode as name.

adding thread group

There you will see some options to fill, by default they’ve value of 1. Let’s understand this step by step as they’re very important to load testing.

options

First one is number of threads [users] , it is basically how many users we want to simulate for the APIs that we are testing ? For this example I am going to put value as 10 i.e for one API I will be testing as if that API will be visited by 10 users.

Second one is Ramp-up Periods : How long one thread will take to start ? For example if number of users is 10 and ramp-up period is 10 seconds as well, then it means each thread will start 1 (10/10) second after the previous thread was begun. Having ramp-up period little higher avoids too many loads to the Jmeter at once.

The final one is loop count, it basically means how many times the user has to be repeated ? For example if loop count is set to 3, this means one user will be in loop for 3 times, thus taking the total samples value to 3 * 10 = 30 samples. As we can’t be sure just by looking at one or two or very little samples, we have to make sure that the result will be fine by the samples take so it is very much recommended that you put enough samples so that the result can be trusted. 30 is the fine value but you can put it even higher depending on how many APIs you have, if you have too many APIs in one thread group , don’t put too high value as it can take several hours to produce result.

setting options values

Multiple controllers can be grouped under one thread group, which ultimately can have samplers which eventually includes the endpoint that we’re going to test. Choose logic controller as follows and give this a name of your choice, I will add the name as login as I will be showing how to test logging in APIs in one of the applications. It is good to add controllers for related endpoints to be tested, however, for small demo like this, it has really little benefits. I can write http requests directly under thread group as well.

Adding controller

Jmeter has an option to add configuration elements like header elements , cookie manager etc. which basically allow us to define what our requests and response will be. For example, Http Header Manager will tell about what content to accept, authentication token if any etc. We can add other cookie information and so on. Let’s add a header element called accept that will tell what content type to accept. Go to the top and right click and config and add http header manager as shown below.

Adding configuration element.

Above diagram tells that application/json is the accepted content type.

We can define user variables and replace them dynamically in many places instead of writing values every time. We can define variable as key value pair in user defined variable section and use them with ${variableName}. Let’s add a variable to see how can we add , I will show you how to use that later in the article.

Adding user defined variables
example of adding user defined variables.

In the above diagram, I have defined two variables called username and password with their value. These variables when referenced, will actually be replaced by their corresponding value. When I want to use username I can use that as ${username} anywhere in my load testing scripts I want. I have just put a random value to show as an example, but be sure to add real values as you require and make sure to add the required variables here.

Add Http Request Default

As it is clear that web has protocol http or https, host such as guidestocode.com, localhost and port. In http request defaults section we can define those so that all of our endpoints by default use those defined credentials. Let’s see how to add them :

Adding https request defaults
Referencing user defined variables in request defaults

Here for example, ${host} is as we discussed earlier, is the host that has been declared in the user defined variable sections. By default all the endpoints will use those default requests.

Http request is basically nothing other than the endpoint we want to test. For example if we want to test domain.com/login, this will be the http request. Let’s add that in our application and see. Add that as follows :

One can add multiple http requests under one logic controller, for example if you’re going to test login functionality in your app and that has multiple request for example /authenticate, /twoFactorVerification etc. you can group all of them in one logic controller called login.

When I login in my app, I see in the network tab that it calls few apis which I will list in login controller as follows.

Above diagram simply tells that request type is get, and endpoint is api/v1/client/current, remember that http default request will be used here if we don’t specify any. If our host is http://app.localhost:8080, every endpoints will be preceded by this. This means, our api will essentially be http://app.localhost:8080/api/v1/client/current, similarly header config will also be used as defined already.

Similarly add all the other related http requests. Make sure that you’ve added all the required variables, that we will use in user defined variables.

For example, if you add /api/login it’s a post request and it requires us to pass username and password, you can add body data as json in Jmeter as follows :

JSON Extractor

To make our test dynamic, we can extract json from the response called Post Json Extractor so that this will replace the current variable value in case we want after we get the response from the previous response. The important use case is that you get authentication-token in response which you will need later to access other endpoints. To extract JSON, you can add post processor :

Json Extractor

In the diagram below, I have extracted token that is in my response and have replaced variable called token using $.access_token, remember that $ is used to access json response, I have used .access_token as it is at the top level. If you had access_token under token in your response, you have to use $.token.access_token

accessing json value from response

Now, we’ve finished configuring endpoints and other configurations for our, it’s time to add visualization things. For example aggregate report, tree result, aggregate graph etc. These are the most common one. But in a second, I will show you how to add csv and report writer to generate nicely visualized dashboard. To add View result tree for example, we basically add listener to the endpoints and they will all be collected in one results tree, graph or whatever we choose, follow the following way :

adding view results tree

Similarly add graph report and others as well if you need. I have added following three :

adding aggregate graph

Similarly, add aggregate report and simple data writer from the listener and give them where to store, a location. Remember they’ve an extension of .csv and .jtl.

Now we’re all set to run our load test and see the result. To run the result click on the green play icon on the top.

Note: It is not recommended to run using GUI mode as told in official docs, but for this tutorial and small test it’s fine. Depending upon thread groups and loop count, it will take some time to run and complete, once it completes you can see the result in listeners.

Generating Dashboard / Graph visualization

Once you have run the above test, now files have been saved to given location in your computer. Just navigate to that report and type the following command and it will produce the result :

jmeter -g fileName -o output

In above command you need to replace fileName with the actual name of the file you’ve used before, for example I have used aggregate-report-writer.jtl , and output is the folder name where you want to store the generated result. It can be any name like jmeter-result or anything. So, my command to generate dashboard will be :

jmeter -g aggregate-report-writer -o output

Once you run your tests, it will save the report in the above folder and after entering mentioned command up there, you’ll have index.html file which can be opened and visualized on the browser. Report will look very similar to this, where you can see average time, median, errors and much more.

Graph result produced from our load test scripts

In this article you learnt how to test the performance of your application. Please don’t forget to share your thoughts in the comment box below , share if this article can help others. Happy coding !!!

Consider following and subscribing to newsletters if you think you can get valuable information in my posts.

Photo by Mike van den Bos on Unsplash

Originally published at https://guidestocode.com on May 19, 2022.

--

--

Badri Paudel

Software Engineer, exploring different languages & frameworks. Working with Java/Groovy, Spring/Grails, Javascript, Postgres & more. Love Sharing what I know.