Performance testing using Jmeter
These days when everything is moving to web and users are increasing day by day so it is better to keep your application ready and prevent the failure which occurred due to performance measures.
To achieve this, we need to load test our application then it can be more predictable that “On a given configuration how our application will believe” and we can make some code optimization or tune our servers accordingly.
In this blog, we will understand how load tests are done with the practical implementation of scripts.
Please note, for this blog we will be considering a REST API which accepts Username and Password as JSON or XML and returns status code as 200 if user authenticated successfully and respective errors for other cases, here all steps are related to POST method, however you can change it accordingly your need, it is just a matter of changing the Request type and data in the JMeter test plan, everything else would remain the same.
How to start the performance test?
For this you need to follow the steps below:
Step 1. Create a JMeter test plan
Step 2. Run test plan
Step 3. Generate the report using performance number
Step 4. Bench-marking OR Publishing report
Step 1:- To create a test plan to follow the steps mentioned in this section,
⦁ Launch Jmeter, you’ll see Test Plan and WorkBench in the left panel.
⦁ Right click on TestPlan and then select Add -> Threads (Users) -> Thread Group. Change its name to REST_Performance_Test or anything which suits you.
⦁ Now right click on Thread Group and select Add -> Config Element -> HTTP Request Defaults.
⦁ Mention domain name or IP address of the web server in field “Server Name or IP”, for example “myrestapi.com” OR 192.168.1.1, you can change it accordingly if it is a local run, make sure you don’t mentioned prefix in this, like; http://. Also change it’s name to WebServer.
⦁ Now it’s time to define what test we are going to perform, for this we need to add an HTTP sampler, right click on Thread Group and select Add -> Sampler -> HTTP Request and change its name to MyRestAPI or as per your API.
⦁ Then change field “Method” to POST and “Path” to ‘/user_auth_api’ or to URI which denotes the REST API for your test.
⦁ Click on “Body Data” tab and enter JSON or XML request, for our case, it is:- { “username” : “username”, “password” : “password@123” }
⦁ Till here we are done with the process of sending the requests to the server, now we need to add things in our plan which can validate the response that we get from server. To do this, right click on Thread Group and select Add -> Assertion -> Response Assertion, give a name to it, say “Verify_Response”.
⦁ Now select the radio button next to “Response Code” and also check “Ignore Status” checkbox then select “Equals” radio button under “Pattern Matching Rules” and click ‘Add’ button at the bottom, it will add a blank line to “Patterns to Test” box, mention ‘200’ in that.
⦁ As of last step of the test plan add listeners to the test plan, click on the “Thread Group” the Add -> Listener -> View Result Tree and again Add -> Listener -> Aggregated Report. These JMeter elements are for storing the responses of your HTTP requests and showing a visual representation of the data.
Now save you JMeter test plan, Step 1 completes here and your test plan looks like as follows:-
and if you take a look of test plan XML, it will look like,
If you want to change anything in your plan, you can make them directly to its XML as well.
Step 2:- Run test plan
From UI, To run JMeter test plan, you just need to click Run button at the top or press “Ctrl + R ”, once it is completed it will show request statues in the “View Result Tree” and a complete summary in the “Aggregate Report” listener.
From Command Line, It is recommended that you use JMeter UI only to create the test plans and analysis of performance number, always try to execute the scripts from the command line, it makes the results more stable and relevant. To run the test plan from the command line follow the step below:-
⦁ Go to the bin folder of Jmeter
⦁ Type “jmeter.sh -n -t <TestPlanPath> -l <LogFilePath>” -> for Unix
“jmeter.bat -n -t <TestPlanPath> -l <LogFilePath>” -> for Windows
For further information of command line options, please refer to Apache JMeter Documentation
Step 3:- Generate the report using performance number
Now you have the performance number with you, the number can be taken by changing Number of Thread (users), Ramp-up Period (in seconds) and Loop Count as per given configuration. Once you have done with all your test a report can be made by mentioned following facts:-
⦁ Total sample was taken
⦁ Min, Max and Average time for a sample
⦁ 90% Line
⦁ Error%
⦁ Error code/message
⦁ Throughput
Something like,
Step 4:- Bench-marking OR Publishing report
If a set of benchmark numbers is available then mentioned it in the step 3 report as a comparison, that is; Comparison of present performance numbers with the benchmark numbers.
And if service has no bench-marking done earlier then set it by getting threshold limit from the facts in step 3.