Hi all, in the previous article we learned how to perform web load test and performance test with JMeter. In this article, I will explain to you how to do API load testing, API performance testing with JMeter. In order to test your API’s, you need to know their details. You need to know request URL, request type, request parameters if available, and also response details.

I will explain how to do API performance testing (or API Load Testing) with an example. Our performance test scenario and requirements are as follows:

API Performance Testing Scenario and Request Details

Users first get Token from the API.

Url: https://test-token.isinolsun.com/api/token

Method:  POST

Header Variables:
Accept-Language
: tr-TR
Device-Id: b19333e86bcbc036
Authorization: Bearer
Source-Type: Android
Source-Version: 2.1.7

Request Parameters:
client_id=BlueCollarAndroidPublicClient
client_secret=66EE4C21-4B07-4B1E-9E95-E25976C64776
client_type=4
grant_type=password

ResponseGet access token with access_token key and use it for later requests.

Then, Check the device is registered or not.

Url: https://test-api.isinolsun.com/common/v2/api/account/anonymous

Method:  GET

Header Variables:
Accept-Language
: tr-TR
Device-Id: b19333e86bcbc036
Authorization: Bearer
Source-Type: Android
Source-Version: 2.1.7
Authorization: Bearer ${token}

Request Parameters:
anonymousId=b19333e86bcbc036

Then, Check force update.

Urlhttps://test-api.isinolsun.com/common/v2/api/settings/applications/versionstates

Method:  GET

Header Variables:
Accept-Language
: tr-TR
Device-Id: b19333e86bcbc036
Authorization: Bearer
Source-Type: Android
Source-Version: 2.1.7
Authorization: Bearer ${token}

Then, Check sponsored Job Post Check.

Url: https://test-api.isinolsun.com/common/v2/api/settings/jobs/displaytypes

Method:  GET

Header Variables:
Accept-Language
: tr-TR
Device-Id: b19333e86bcbc036
Authorization: Bearer
Source-Type: Android
Source-Version: 2.1.7
Authorization: Bearer ${token}

Request Parameters:
pageNumber=0
pageSize=20

Then, do a search with no keyword.

Url: https://test-api.isinolsun.com/jb/v2/api/jobs/search/details

Method:  GET

Header Variables:
Accept-Language
: tr-TR
Device-Id: b19333e86bcbc036
Authorization: Bearer
Source-Type: Android
Source-Version: 2.1.7
Authorization: Bearer ${token}

Request Parameters:
pageNumber=0
pageSize=20
keyword=
latitude=41.0266502
longitude=29.1157803
distanceRange.from=0.0
distanceRange.to=20.0
hasLatitude=true
hasLongitude=true
isDistanceSortEnabled=false
showOnlyNativeJobs=false
isNearByCandidateSortEnabled=false
isNewJobFilterEnabled=true
positionId
=0

Then, for search a waiter (garson) for first 3 pages.

Url: https://test-api.isinolsun.com/jb/v2/api/jobs/search/details

Method:  GET

Header Variables:
Accept-Language
: tr-TR
Device-Id: b19333e86bcbc036
Authorization: Bearer
Source-Type: Android
Source-Version: 2.1.7
Authorization: Bearer ${token}

Request Parameters:
pageNumber=0
pageSize=20
keyword=garson
latitude=41.0266502
longitude=29.1157803
distanceRange.from=0.0
distanceRange.to=20.0
hasLatitude=true
hasLongitude=true
isDistanceSortEnabled=false
showOnlyNativeJobs=false
isNearByCandidateSortEnabled=false
isNewJobFilterEnabled=true
positionId
=0

Response: Get jobId and use it for later requests.

Then, go to a random waiter job detail.

Urlhttps://test-api.isinolsun.com/jb/v2/api/jobs

Method:  GET

Header Variables:
Accept-Language
: tr-TR
Device-Id: b19333e86bcbc036
Authorization: Bearer
Source-Type: Android
Source-Version: 2.1.7
Authorization: Bearer ${token}

Request Parameters:
id=${jobId}

How to Create API Performance Test with JMeter

First, let’s add HTTP Request Defaults and User Defined Variables Config Elements

Then, set the user-defined variables in User Defined Variables screen as shown below.

Then, click the HTTP Request Defaults and set the global URL as shown below.

Then, add a Thread Group as shown below and name it “IsinOlsun Thread Group”

For, this example I just want to set the thread numbers just one as shown below. When our scenario will work perfect, then you can increase these numbers.

Then, add an HTTP Header Manager. For all requests header variables which are shown below, are common.

Now, let’s create our first request. The request which gets the Token. Right-click the Thread Group, then select, Add->Sampler->HTTP Request.

And then, fill the required fields based on our token request details: header variables, request parameters, URL and request method should be filled based on our request details.

When we get the token response we should get the token and use it for further requests. Thus, we need to add a JSON extractor and capture the token as shown below.

When we run the request, we can see the results with “View Results Tree” listener. So, Let’s add a View Results Tree in our project as shown below.

Now, lets click run icon and run the first request and check its response in View Result Tree. Also, select “JSON Path Tester” and get the token value as shown below.

Let’s click the JSON Extractor, and use the “.access_token” value to capture token value as shown below. From, now on we can use ${token} variable for further requests as a token.

And now, we can go on with the second request. It checks the device is registered or not. We need to add an HTTP Header Manager for the token.

And request will be like below. For the request details, you need the check first part and Check Device request details.

Then, we need to check Force Update. In order to check this, I will add an HTTP Header Manager and set the Bearer ${token} as the previous request. After this, we need to set the request as shown below.

Let’s go on with the check a sponsored job. Again we should add an HTTP Header Manager and set the Bearer ${token} as the previous requests. Then, fill the fields as described at the sponsored job request details.

Now, it is time to do a search with no keyword. We did the same things as I described at former request descriptions. Adding an HTTP Header Manager for Bearer ${token} and then fill the required fields based on request details.

Now, we will search the “garson” (waiter) keyword 3 times for 1st page, 2nd page, and 3rd page. In order to do this, we have to increase the pageNo request parameter each time by one. Thus, we need to use Loop Controller and BeanShell Sampler. In BeanShell Sampler, we can write a script to do increment operation. First, add a Loop Controller and set Loop Count as 3.

Right-click to Loop Controller and add a BeanShell Sampler.

Now, let’s write a bean shell script to increase page number.

Now, we can add an HTTP Request for “garson” (waiter) search according to Page Number and also an HTTP Header Manager for the token. It is similar as we saw previous examples.

In order to get jobIds, we need to run the JMeter script to get the response and use JSON Path Tester to create a JSON expression to get JobIds. You can also use POSTMAN to get the response and then use jsonpath.com website to write a JSON expression.

When we form the JSON Path Expression which gathers all JobIds, then we can add a JSON Extractor.

And we can now create an HTTP Request for Job Detail. We should use ${jobId} which we gathered the previous request as an id. Also, you need to add an HTTP Header Manager for Bearer ${token}.

Finally, add a View Results Tree to see and check the details of requests and responses.

You can also add some think times etc. too. This is a prototype of an API Performance JMeter project. Now, it is time to run the project.

As you see, everything looks great. Now, you can save the .jmx file and if you have a cloud performance testing provider such as Loadium, you can upload your .jmx file and create a load on your APIs and check their performances. At Kariyer.net, we are using Loadium as a load generator service. We create a load very easily and then run the test and see the results simultaneously. If you don’t have cloud virtual user generation service, then you can create the load on your local servers and you can use Grafana as a monitoring tool. While running your tests, do not use listeners. They use a huge amount of RAM. Also, run your tests in non-gui mode when they look stable.

jmeter -n -t IsinOlsun.jmx -l IsinOlsunResults.jtl

-n [This specifies JMeter is to run in non-gui mode]

-t  [name of JMX file that contains the Test Plan]

-l  [name of JTL file to log sample results to]

-j  [name of JMeter run log file].

In this article, I tried to explain how to do API performance test by using Apache JMeter. I hope you enjoyed reading it.

Here is the GitHub link of the project: https://github.com/swtestacademy/apiperformancetestingbyjmeter

Thanks.
-Onur Baskirt