JMeter-Dynamic Load Testing of Restful APIs
The purpose of this article is to enable you to run the performance/load test of restful APIs using JMeter on pre and post-deployment with config driven approach and also enable the comparison of performance stats with some certain benchmarks. So it can be used to simulate loads of various scenarios and output performance data in several ways, including CSV or an HTML report.
Install Apache JMeter
Prerequisites: Java 8 or Java 9 for Apache JMeter 5.1.1
First, you need to download the JMeter from the Apache JMeter website. Once after you download the zip file then extract it to a location where you want to work with it. Then move to the /bin folder inside that extracted folder and click on the executable Jar file to open the Apache JMeter with GUI.
Create Test Plan
Before writing a test plan let’s see what it is, A Test Plan can be viewed as a container for running tests. It defines what to test and how to go about it. A proper test plan will have some of the elements such as;
- Thread groups
- Logic controllers
- Timers
- Pre and Post Processors
- Configuration Elements etc.
Configuring the Test Plan
The APIs which I’ve chosen to load tests with the Apache JMeter has many endpoints, but I’ve chosen some pretty small endpoint with the different scenarios when it comes to processing time.
As we learned we need to have at least one Thread Group for the test plan in order to run the load test. So let’s first add a thread group to the Test Plan.
Set Config Element
For dynamic test execution, we need to create a CSV file with the number of endpoints details to execute the test on each endpoint one by one.
Once we have created a CSV file now we need to set up a config element “CSV Data Set Config” in under the created thread group to get the following parameters from CSV files.
- testID
- label
- serverAddress
- path
- method
- expectedCode
- payload
While setting up this config element, we need to specify the CSV file path along with variable names as per the given columns in CSV file to use that at the appropriate place.
Add HTTP Request Sampler Under Transaction Controller
As we have multiple endpoints in our CSV configuration, we need to set up a ‘Transaction Controller’ to execute all endpoints sequentially as per the given order in the CSV file and also we can have a dynamic controller name as you can see in the below image, the name is being set up based on the user-defined variables extracted from the CSV file. E.g. ${testID} — ${__threadGroupName} / (${label} ) = Test1-Load Test of Rest APIs-Get Available Status
After configuring the Transaction Controller, now we have to configure the ‘HTTP Request’ sampler under the Transaction Controller, which is the most important part of this test script. So to enable dynamic execution, we need to parameterize this sampler based on defined variables in the config element which are being extracted from the CSV configuration file as you can see in the below image.
For executing the Post endpoints, we need to pass a Payload in JSON format with request headers to create or update any object. So for that, we need to define Content_Type for JSON payload in ‘HTTP Header Manager’ by setting up the value as ‘application/json’, and if you will not define Content-Type then the payload will be passed as a text value, which will cause to be a bad request while executing the API endpoint.
Set Assertions
In your performance test, you can apply some assertions based on some provided benchmarks. Those assertions can be applied on response body, response headers, response code, request data, etc, but for time being I am applying the assertion on response code only against expected response code given in my CSV file for each endpoint.
Setup-Teardown Thread Group
It is a special form of Thread Group used to perform necessary actions after the execution of a regular thread group completes. The behavior of threads mentioned under Setup Thread Group is exactly the same as a normal thread group.
So here I am using this thread group for generating the HTML report using Beanshell sampler after executing all the tests. Moreover, you can use this to send out an email along with the result using the SMTP sampler.
Generate HTML Report via Beanshell Sampler
BeanShell is one of the most advanced JMeter built-in components. It supports Java syntax and extends it with scripting features like loose types, commands, and method closures. So here I am using Beanshell to generate an HTML report by executing cmd commands. Those commands are containing some directory URLs that come from the CSV configuration file to read the CSV results and place an HTML report, also the script is creating a compressed file of HTML report with the current timestamp for the report history.
cmdObject = Runtime.getRuntime().exec(“C:/Windows/System32/cmd.exe /c ${root_dir} & cd ${JMeterDir} & jmeter -g ${result_log_path} -o ${report_dir} & jar -cMf ${compress_report_dir}HTMLReport_%time:~0,2%%time:~3,2%%time:~6,2%_%date:~-10,2%%date:~-7,2%%date:~-4,4%.rar ${report_dir}”)
Listeners
A listener is a component that shows the results of the samples. The results can be shown in a tree, tables, graphs or simply written to a log file. To view the contents of a response from any given sampler, add either of the Listeners “View Results Tree” or “Summary Report” to view the result within a test plan and “Simple Data Writer” to write the results in a CSV file.
Run Load Test
Now we have everything set up correctly. Now it’s high time to run the load test. In order to do so, we need to reconfigure our Thread Group element in our Test Plan to have several properties related to Thread. To do that click on the Thread Group and add the following properties to it. Because to be a load test we should provide some heavy load to the API’s endpoint. By changing the following parameters in Thread Group it allows JMeters to perform a proper load test with a load.
Those changed values are the three particularly important properties which influence a load test:
Number of Threads (users): The number of users that JMeter will attempt to simulate.
Ramp-Up Period (in seconds): The duration of time that JMeter will distribute the start of the threads over.
Loop Count: The number of times to execute the test.
After saving the test plan, you can run the test from the console by clicking on the Play button, and the test execution will be started and you can view the instant test results on your added listeners or later on HTML report. But according to the best practices, it is recommended to execute the test plan from the command line instead of GUI mode for better performance.
View Result on Listeners
The test results will look as follow on the following listener.
View Result on HTML Report
HTML report contains the following attributes as it is being shown in the below images.
Conclusion
The Apache JMeter application is open-source software, a 100% pure Java application designed to load test functional behavior and measure performance. It was originally designed for testing Web services but has since expanded to other test functions. This is a very powerful tool, and there are a lot more you can do with this tool such as Distributed testing, Test Recording, etc. So here we simply have done with the Rest API load test, further, you can play around with this tool and read more from the documentation available on the different forums to build more powerful load tests.