API Automation and Performance Testing Via Jmeter

Ved Prakash Arya
TechBlog
Published in
5 min readJun 9, 2020

For any application performance is the most important part, we consider any application good only if its performance is good.

To test the performance of application we can use any testing tool but Jmeter is most popular testing tool among others, as it's open-source and supports a multi-threading framework and have the support of many protocols.

So for testing the performance of application we need the Jmeter script for all the APIs which can be developed easily from the Jmeter tool.

Once ready with Jmeter script we can use any Java build tool to execute this script like we can use an Ant build tool to execute the Jmeter script to generate .jtl file which is nothing but an XML file and later same .jtl file can be used by Ant build tool to generate attractive HTML where we can easily check all the details request/response like average, min and max time, the total number of requests along with failure message if any.

So let's begin with creating a simple Jmeter Script, for creating Jmeter script we need to install Jmeter on our system. Since Jmeter is developed in Java so we also need JRE on our system to execute Jmeter application.

Step 1: Install Java

Step 2: Download the latest version of apache Jmeter and extract it at the desired location. I will be using “/home/ved/Downloads/apache-jmeter-5.2.1" as the Jmeter path.

Step 3: Open Jmeter, it will open with a default Test plan.

Jmeter

Change the name of the Test plan (Brightlab Test Plan), Jmeter has the facility to define some variable in Test Plan so that we can use those user-defined variables in the rest of the script. So let's add 2 user-defined variables
1. host:- we will use the host variable to store the URL of the application.
2. users:- we will use users variable to store the number of users.
To declare any variable in Jmeter we need to just type name and its value, but if we want to pass the value of the same variable from Ant script then we need to use Jmeter property syntax which is ${__P(variable_Name,default_value)}
So for the host, we will use
name as host and value as ${__P(host,platform.brightlab.com)}
similarly for users
name as users and value as ${__P(users,1)}

Test Plan

Now we can add Thread Group to the Test Plan and give some name(users), hereunder “Number of threads” instead of hard coding value we can use user-defined variable “users” like ${users}

Thread Group

Then we can add Http Request Sampler to Thread Group and give some name (Open Brightlab). In Http Request, we have options like protocol, server name, method, path, and may more but for simplicity, we will use only these 3 attributes.
protocol=https
method=Get
server name = ${host} instead of hard coding we can use a variable which we have already defined in Test Plan.

Sampler

Finally, we can add View Result Tree Listener to Test Plan so that we can see the response.
Now we can save this Jmeter script (MyJmeterScript.jmx)
On click of the run button, we can see the response in View Result Tree Listener.

So here we are done with Jmeter script, which we can use for API automation and performance testing.

To execute Jmeter script with Ant build tool we need to prepare a build.xml file which will have details like which .jmx file to execute and what should be the file name of .jtl file, for how many users it should run the script, also
once Jmeter script execution complete then which .jtl file should be used to generate HTML report and with which name and many more details we can provide in build.xml

In sample build.xml we need to change the value of below properties
jmeter.home=your Jmeter folder path
report.title=The the title you want in HTML report
test=Name of you Jmeter script file with .jmx
With these changes, once we will execute the ant script it will execute Jmeter script for 1 user, and generate .jtl and .htm files with the same .jmx file name.
Let me explain the build.xml file a little more.
So this sample build.xml has a total of 7 targets, the target can be understood as a task.
target-> all: by default we have given to execute this target will is dependent on other targets, so it will execute other targets first.
target-> run_MyScript : To run Jmeter script.
target-> generate_Html_report : To print report generated date and time.
target-> xslt-report : To use .jtl file and generate html report.
target-> copy-images : To copy expand and collapse images.
target-> verify-images : To verify if images are present.
target-> _message_xalan : To verify if required jar files are present.

To run build.xml we need to install Ant in our system.

Once the ant is installed we can create a new folder with any name(extra) and need to copy below files in same
Jeter script file i.e MyJmeterScript.jmx
build.xml file
jmeter-results-detail-report_21.xsl (This XSL file which will be used to generate HTML report)
expand.png as build.xml will look for same while generating HTML report
collapse.png as build.xml will look for same while generating HTML report
ant-jmeter-1.1.1.jar
All files are available at GitHub.

To execute build.xml, open terminal, go to folder and type ant -f build.xml

any command to execute build.xml

After successful execution we can see a .jtl and .html file in folder(extra)

Html File
html Preview

To execute the Ant script at the regular interval we can either use the cronTab feature of Linus OS or use Jenkins's job to build an Ant script periodically.

Conclusion: We can use an Ant build tool to execute any Jmeter script for any number of parallel/concurrent users and instantly generate an attractive HTML report and easily identify if any API is not working as expected.

--

--