JMeter
Table of contents
- What is JMeter?
- Configuring JMeter
- JMeter Components
- Quick Start Guide
- API Request and Response Overview
- JMeter for API Testing
- JMeter for UI Testing
What is JMeter?
JMeter is an open-source performance testing tool that is widely used for testing the performance and load of web applications. It allows users to simulate various scenarios and measure the performance of the target application under different loads and conditions. It is a versatile and powerful tool that helps developers and testers ensure the reliability, scalability, and efficiency of their web applications.
Configuring JMeter
- Go to the official JMeter website and choose the appropriate version of JMeter for your operating system (e.g., Windows, Linux, macOS) and click on the download link.
- Once the download is complete, extract the downloaded file to a directory of your choice. This will create the JMeter installation directory.
- Before running JMeter, make sure you have Java Development Kit (JDK) installed on your system. JMeter requires JDK to run.
- Configure the environment variables for Java. This step may vary depending on your operating system.
For Windows
: Set JAVA_HOME environment variable to the directory where JDK is installed
(e.g., C:\Program Files\Java\jdk1.x.x_xx).For Linux/macOS
: Set JAVA_HOME environment variable in your shell profile
(e.g., ~/.bash_profile). - Open a command prompt or terminal and navigate to the JMeter bin directory.
- Run the JMeter executable file:
For Windows
: Run jmeter.bat
For Linux/macOS
: Run ./jmeter.sh - JMeter will start, and you can begin using it to create and execute performance tests.
Make sure to refer to the official JMeter documentation for detailed instructions.
JMeter Components
JMeter has several components. Below is a explanation of each component:
- Thread Group: Manages the runtime and number limitations of requests. It is essential for conducting performance tests.
- Sampler: Generates requests for various protocols and is primarily used for editing user request data.
- Configuration Element: Assists in simplifying the editing of request data by providing auxiliary functionalities.
- Pre-Processor: Manages data before a request is sent.
- Post-Processor: Manages data after a response is received.
- Timer: Controls the runtime of requests.
- Assertion: Performs expected result checks on response content.
- Logic Controller: Controls the execution order and rules of various requests or elements.
- Listener: Displays the final results, which can be presented in graphical or data format.
Quick Start Guide
- Clone the jmeter repository.
- Open a shell window and navigate to the bin directory where jmeter is installed.
- Type the command
./jmeter.sh
- The Apache JMeter application will be launched.
- Open the jmx file under the TestPlans folder - this folder contains two file
API_Test_Plan.jmx
andUI_Test_Plan.jmx
. - With the jmx file opened, click on the green
Run
button to run the jmeter tests and view the results using a listener (e.g. Summary Report).
IMPORTANT
This repository uses Chrome version 113.0.5672.63 for UI tests.
API Request Response Overview
For the purpose of this test, we will use one of the NASA Api’s.
Request
GET /insight_weather/
curl --request GET \
--url 'https://api.nasa.gov/insight_weather/?api_key=IxpTyqs8LnclBrUq88cIdf99zftbzPgHYbmEzb1V&feedtype=json&ver=1.0' \
| Parameter | In | Type | Required |
| :— | :— | :— |:— |
| api_key
| query
| string
| true
|
| feedtype
| query
| string
| true
|
| ver
| query
| number
| true
|
Response
Response: 200 OK
JMeter for API Testing
-
Launch JMeter
Open a shell window, navigate to the bin directory where JMeter is installed and provide the JMeter executable file.
bin % ./jmeter.sh
-
Create an API Test Plan
In the JMeter interface, rename the default Test Plan to ‘API Test Plan`.
-
Add a Thread Group
Right-click on the
API Test Plan
and select “Add” -> “Threads (Users)” -> “Thread Group”. This will create a Thread Group element, which represents a group of users. -
Configure the Thread Group
Set the desired number of threads (virtual users) and the ramp-up period (time to start all threads) in the Thread Group element. You can also specify the loop count and duration of the test.
-
Add an HTTP Request Sampler
Right-click on the Thread Group, select “Add” -> “Sampler” -> “HTTP Request”. This will add an HTTP Request Sampler to the Thread Group.
-
Configure the HTTP Request Sampler Provide the necessary details such as the server name or IP address, port, protocol, path, and method (GET, POST, etc.) in the HTTP Request Sampler. You can also add parameters, headers, and body data as required.
-
Add Listeners Listeners are used to view and analyze the test results. Right-click on the Thread Group, select “Add” -> “Listener” and choose the desired listener, such as “View Results Tree” or “Summary Report”.
-
Run the Test Click on the “Play” button in the JMeter toolbar to start the test execution. JMeter will send requests to the specified API endpoints based on the configured settings.
-
Analyze the Results Once the test execution is complete, analyze the results using the chosen listeners. You can view response times, error rates, and other metrics to evaluate the performance and functionality of the API.
-
Customize and Enhance the Test JMeter provides various options to customize and enhance your API tests. You can add assertions to validate response data, use timers to introduce delays between requests, and incorporate logic controllers to create complex test scenarios.
JMeter for UI Testing
-
Create a UI Test Plan
In the JMeter interface, rename the default Test Plan to ‘UI Test Plan`.
-
Add and Configure a Thread Group
Right-click on the
UI Test Plan
and select “Add” -> “Threads (Users)” -> “Thread Group”. This will create a Thread Group element, which represents a group of users. Set the desired number of threads (virtual users) and the ramp-up period (time to start all threads) in the Thread Group element. You can also specify the loop count and duration of the test. -
Download Chrome Driver
Download the chromedriver compatible with your chrome browser version from [here] (https://chromedriver.storage.googleapis.com/). Rename it to
chromedriver
and place it in your jmeter bin directory. -
Add ChromeDriver Config Element
Right-click on the
UI Test Plan
and select “Add” -> “Config Element” -> “Chrome Driver Config”. -
Configure the Chrome Driver Config
Click on the
Chrome Driver Config
, click on the “Chrome” tab in the center. Add the path to your config${__P(driverPath, chromedriver)}
-
Configure the Web Driver Sampler
Right-click on the
UI Test Plan
and select “Add” -> “Sampler” -> “Web Driver Sampler”. -
Configure the script for Web Driver Sampler
Click on the
Web Driver Sampler
and place the below script in thescript
section.WDS.sampleResult.sampleStart() WDS.browser.get('http://jmeter-plugins.org') WDS.sampleResult.sampleEnd()
-
Add Listeners
Listeners are used to view and analyze the test results. Right-click on the Thread Group, select “Add” -> “Listener” -> “Summary Report”.
-
Run the Test
Click on the
Play
button in the JMeter toolbar to start the test execution. JMeter will open Chrome browser sessions based on your configured thread group settings. -
Analyze the Results
Once the test execution is complete, analyze the results using the chosen listeners. You can view response times, error rates, and other metrics to evaluate the performance and functionality of the UI in the Summary Report.