Thursday, 7 July 2022

API Testing and Performance Testing Using JMeter




To ensure the quality of the software product different tests has been performed. Once such test is Load Testing. Load Testing is a kind of Performance Testing that helps in determining how the application behaves when multiple users/requests hit the application simultaneously.

From many tools helping us to resolve this, for now we will see how Apache JMeter (JMeter) an Open Source tool helps us to achieve this, what are the pros and cons using this tool.


What is JMeter and Why do we need 

JMeter is an Open Source testing desktop based application. It is 100% pure Java application used as a performance testing tool for analysing and measuring the performance in variety of graphical reports.

We can perform load testing, stress testing and functional testing for the applications.

If there are any flaws in application such as buffer overflow, memory leaks using this we can easily simulate the situation in our own environment, and avoid the Prod issue surprises.

Using JMeter we can achieve all this easily, with minimum to no knowledge in coding. Yay!!


Installation

Please follow the below detailed steps on the installation of JMeter on the respective operating system. 

  • Windows & Linux: https://jmeter.apache.org/usermanual/get-started.html 

  • Mac: https://psychowhiz.medium.com/install-jmeter-on-mac-25531bc2b2ad


JMeter Supports

JMeter supports following and more types of the applications that can be tested with the tool.

  • Websites – HTTPS and HTTP

  • Web services – REST, SOAP and Graphql

  • Database servers

  • FTP servers

  • LDAP servers

  • Mail servers – SMTP, POP3, IMAP

  • TCP Servers


Elements of JMeter

There are different elements in JMeter each performing a task to achieve our goal. Below are few elements which will be important to know perquisites:

  • Thread Group: Collection of Threads, each thread represents one user accessing the application in the thread. Ideally it simulates one real user making one request to the application server. We can have multiple number of threads configured here.

  • Samplers: Type of request, such as FTP, HTTP, etc.

  • Listeners: Shows the results in different formats.

  • Configuration: Setup defaults and variables for later use by the samplers.

  • Assertions: Validate response from the server is expected or not.


Load Testing

Prerequisites

Here we will test Google Home Page

  • Open installed JMeter

  • Click File -> New

  • Update the Name to GoogleHomePageTestPlan

  • Click Save Icon # it will save as the .jmx file 

Figure 1: Save the test plan into .jmx file


Step 1: Thread Group

In the Thread Group, we need to know about the below properties.

  • Number of Threads: N number of users connecting to the target URL

  • Ramp-Up Period: Allows to include delay while requests hitting the target, for example if we have 10 threads and 5 ramp-up period : the 10 requests should hit the target within 5 seconds adding 0.5 seconds delay between each request. This is primarily used when we don't want to over bun the server and as well as client. But if you want to do the Stress Testing try tweaking the property based on the use case. 

  • Loop Count: N number of time the test case to be executed, for example if you have number of threads 5 and loop count as 2, total 10 times the request will hit the target.


Figure 2: Difference between Thread count and Loop count

Credits: https://medium.com/skyshidigital/performance-testing-with-jmeter-for-dummies-541ea9171464


Steps:

  • Right Click GoogleHomePageTestPlan -> Add -> Threads (Users) -> Thread Group

  • Good practices to rename the name, readability matters. For this am updating the name as GoogleHomePageTestSearch

  • Update “Number of Threads (users)”: 5

Note: Please do not give more than 10, they might block us ;)



Figure 3: Thread group creation page


Step 2: Samplers

  • Right Click GoogleHomePageTestSearch -> Add -> Sampler -> HTTP Request

  • Lets rename again. Update the name with: GoogleHomePageTestSearchHTTPRequest

  • Update the below parameters

    • Server Name of IP: www.google.com

    • Port Number: None [optional]

    • Protocol (http): HTTP / HTTPS [optional]

    • Method: GET 

    • Path: /search

    • Parameters:

      • Click Add

        • Name: q

        • Value: JMeter

Figure 4: HTTP Sampler creation page 


  • For POST Method with JSON input

    • We can give input, under Body Data give the JSON format input

    • If needed you can add sure to add header details under “HTTP Header Manager” and authorization under “HTTP Authorization Manager”. Both are under GoogleHomePageTestSearch -> Add -> Config Element


Step 3: Listeners

  • Right Click GoogleHomePageTestSearchHTTPRequest -> Add -> Listener -> View Results Tree

  • Rename the name. Update the Name: GoogleHomePageTestSearchHTTPRequestResultTree

  • Click the Run -> Green Button

  • View the results in Sampler result, Response data and Request

  • Different types of charts are available to see the results crisply. And the link is https://www.edureka.co/blog/load-testing-using-jmeter/

Figure 5: Result page after execution of the Sampler


Step 4: Configuration – [optional]

  • As this step is optional, but we can take parameters to the search

  • Right Click GoogleHomePageTestSearchHTTPRequest -> Add -> Config Element -> User Defined

  • Setup the Variables

Figure 6: User defined variables page

  • Using the defined variable, we need to use it by enclosing it in ${<variable>}

Figure 7: Redefine value to use the user defined variable

  • After Run: Check the “q” is substituted with the defined variable and HTTP Request is also holding the “q” value 

Figure 8: Result tree, the user defined variable has been used in the GET request

  • The same format can be used while Data Driven Testing, where in you would want to induce dynamic values to the service. We need to configure the CSV Data Set Config under Config Element.


Step 5: Assertions – [optional]

  • The response assertion control panel lets you pattern strings to be compared against various fields of the request or response

  • Right Click GoogleHomePageTestSearchHTTPRequest -> Add -> Assertions -> Response Assertion

  • Setup the Validations: Positive Validation: Checking whether the Response Code is 200

Figure 9: Define the rules to evaluate response from the Sampler execution

  • Result would be positive, as we have that Response Code is 200

Figure 10: Response is 200, hence the test result is shown passed (green)

  • There are many different combination of assertions, including by adding regex rather hard coded values.


Bonus Step

We all like Bonus!!

Record and Run

  • Let's say we have many rest apis to be tested in single page, we got to create so many Samplers manually, which we will get bored doing. For example in Amazon, there will several api hit to form the Home Page, such as Deals, Products, Best Sellers, User Menu and so many others.

  • Here we have a solution to Record and Run. Shared the step by step example in the below reference link. Link is:

    https://jmeter.apache.org/usermanual/jmeter_proxy_step_by_step.pdf

  • Note: For now Firefox browser supports this feature. Also we need to make sure we have access to update the settings in the Firefox.

Command Line

  • You can run the JMeter from the command line, and can have its output redirected to an html as well. The non GUI mode brings in more flexibility when running heavy loads. But the configuration can be still done using the GUI, as well the results can be analyzed. 

  • Sample Command : $ jmeter -n -t GoogleHomePageTestSearch.jmx -l GoogleHomePageTestSearch.jtl -e -o googletestoutput

    • -n: to run in non-gui mode

    • -t: name of JMX file having the Test Plan

    • -l: Name of JTL(JMeter text logs) file to log results

    • -e: generate report dashboard after load test

    • -o: output folder where to generate the report dashboard after load test. Folder must not exist or be empty

  • Output:

Figure 11: HTML output of the command line execution


Pros of JMeter

  • Open Source: JMeter is an open source software, so no licensing costs

  • Ease to Use: The user can install and use JMeter easily

  • Platform independent: As JMeter is 100% Java based, so it is platform independent and can run on multiple platforms.

  • Customisable: Since JMeter is open source, developers can customize its source code as per their specific requirements. Also there is prescript and postscript execution process where we can run customisable code in Java or Jpython.

  • Data driven testing: The CSV Data Set Config, allows you to read different parameters from text file and convert into parameters which can be used in making dynamic requests.

  • Record and Playback: JMeter provides record and playback options alone with drag and drop feature which makes it easier and faster to create scripts.

  • Supports distributed load testing: JMeter supports distributed load testing features in which we can create master-slave setup for carrying out load test on multiple machines. Link is : https://jmeter.apache.org/usermanual/jmeter_distributed_testing_step_by_step.html

  • Good community support and Documentation: JMeter has many online tutorials(edureka I really like) and helping community support. It also has freely available plugins that helps in different aspects of script creation and analysis.

  • Reporting: Helps to visualise test results. As see before, test results can be displayed in a different format such as chart, table, tree, log file, html and etc. 


Cons of JMeter

  • Passwords are saved as plain text, involves risk when the JMX file is accidentally shared

  • It supports only Java or Java backed languages for custom coding


Other Similar Tools

  • LoadView

  • LoadUI

  • NeoLoad

  • WEBLOAD

  • LoadRunner


Conclusion

  • One of the most used tool for performance and api testing. Definitely can be used to make sure the web applications are meeting the performance benchmarks.

  • The feature list of JMeter is exhaustive, this is just a sample of its usage. 

  • The User Manual will have the detailed description of each its features, link is https://jmeter.apache.org/usermanual/index.html


References

https://www.edureka.co/blog/load-testing-using-jmeter/

https://jmeter.apache.org/usermanual/get-started.html

https://www.guru99.com/jmeter-tutorials.html



Original Blog Posted in OSFY
https://www.opensourceforu.com/2021/08/test-your-software-with-jmeter/
For further research and updates maintaining the blog here.

No comments:

Post a Comment

Scarcity Brings Efficiency: Python RAM Optimization

  In today’s world, with the abundance of RAM available, we rarely think about optimizing our code. But sooner or later, we hit the limits a...