Framework for automated load testing using Apache JMeter

Below is an example automated load testing framework using Apache JMeter. The framework will include a modular test plan structure, parameterization for flexibility, and automation via the command line and CI/CD.

1. Directory Structure

Organize the framework as follows:

2. Example JMeter Test Plan Structure
LoginTestPlan.jmx
  • Thread Group: Simulates multiple users.
  • Threads (users): 100
  • Ramp-up: 30 seconds
  • Duration: 5 minutes
  • HTTP Request Sampler:
    • Sends login requests to the server.
    • URL: ${base_url}/login
    • Method: POST
    • Body:
  • CSV Data Set Config: Reads dynamic test data from data/user_credentials.csv:
  • File Path: ../data/user_credentials.csv
  • Variable Names: username,password
  • Response Assertions:
    • Validate status code = 200.
    • Check if the response contains "Login Successful."
3. Parameterization (Using env.properties)
config/env.properties:
Load the file in JMeter using a Properties File Reader. Replace hardcoded values with properties like ${__P(base_url)}.
4. Automation Script
Automate execution using the following shell script:
5. Jenkins Pipeline Integration
ci/jenkinsfile:

6. Sample Data Files

data/user_credentials.csv:

data/api_parameters.csv:

7. Results and Reporting

  • Raw Test Results: Stored in results/Results.jtl.
  • HTML Report: Auto-generated in results/HTML Report/index.html.
  • CI Integration: Jenkins publishes reports after execution.

8. Benefits of This Framework

  • Reusability: Modular scripts allow reuse across different scenarios.
  • Scalability: Easily adjust load by modifying environment variables.
  • Automation: Seamless integration with CI/CD tools.
  • Parameterization: Dynamic test data for scalability and flexibility.

Creating a .jmx file for load testing in Apache JMeter involves setting up a test plan, defining test scenarios, and saving it as a .jmx file. Here’s how you can create a basic .jmx file step-by-step:

1. Install JMeter

  • Download and install Apache JMeter from the official website: JMeter Downloads.
  • Extract the archive and navigate to the bin directory.
  • Launch JMeter by running jmeter.bat (Windows) or jmeter (Linux/Mac).

2. Create a Test Plan

Open JMeter: Once launched, a default test plan will appear.

Rename Test Plan: Right-click on Test Plan > Rename > Name it (e.g., LoginLoadTest).

3. Add Thread Group

A Thread Group defines the user load for the test.

Add Thread Group: Right-click on the Test Plan > Add > Threads (Users) > Thread Group.

Configure Thread Group:

  • Number of Threads (Users): 100 (simulates 100 users).
  • Ramp-up Period: 30 seconds (time to start all users).
  • Loop Count: Forever or a specific number.

4. Add HTTP Request Sampler

The HTTP Request Sampler defines the API or web request to be tested.

Add HTTP Request: Right-click on the Thread Group > Add > Sampler > HTTP Request.

Configure HTTP Request:

  • Server Name or IP: example.com (your target server).
  • Protocol: https (or http).
  • Path: /api/login (your API endpoint).
  • Method: POST (GET, PUT, DELETE based on your API).
  • Body Data:

5. Parameterize the Test

To supply dynamic data for each user: Add CSV Data Set Config: Right-click on the Thread Group > Add > Config Element > CSV Data Set Config.

  • Configure CSV Data Set: Filename: Path to your CSV file (e.g., ../data/user_credentials.csv).
  • Variable Names: username,password (corresponding to your CSV headers).
  • Recycle on EOF: True.
  • Stop Thread on EOF: False.

6. Add Assertions

Assertions validate server responses.

Add Response Assertion: Right-click on HTTP Request > Add > Assertions > Response Assertion.

Configure Response Assertion:

  • Field to Test: Text Response.
  • Pattern Matching Rules: Contains.
  • Patterns to Test: Add keywords like "Login Successful" or expected response content.

7. Add Listeners

Listeners generate reports or display test results.

Add View Results in Table: Right-click on Thread Group > Add > Listener > View Results in Table.

Add Summary Report: Right-click on Thread Group > Add > Listener > Summary Report.

8. Save the Test Plan

Go to File > Save As.

Save the test plan as LoginLoadTest.jmx.

9. Run the Test

Click the green Start button in the JMeter toolbar.

Monitor the results in the Listeners (e.g., Summary Report or Table).

10. Advanced Features

For more advanced tests:

  • Add Pre-Processors (e.g., for dynamic tokens).
  • Add Post-Processors (e.g., Regular Expression Extractor for capturing server responses).

Include Timers to add delays between requests.

Process Of creating JMX file:

Creating a .jmx file using browser developer tools involves capturing HTTP requests made during interaction with a website or API. These captured requests can then be exported and imported into JMeter for load testing. Here’s how to do it step-by-step:

1. Capture HTTP Requests with Developer Tools
  • Open Developer Tools:
In your browser (Chrome, Edge, or Firefox), press F12 or right-click and select Inspect.
Navigate to the Network tab.
  • Perform the Action:
Perform the actions on the website that you want to simulate (e.g., login, search, or other workflows).
All network requests made by the browser will be displayed in the Network tab.
  • Filter for Relevant Requests:
Use the filters (e.g., XHR, Fetch, or All) to focus on API or HTTP requests.
  • Export the HAR File:
Right-click anywhere in the Network panel and select Save All as HAR or Export HAR.
Save the .har file (HTTP Archive) to your system.

2. Convert HAR File to JMX File
To convert the .har file into a .jmx file for JMeter, you can use the following tools or plugins:
  • BlazeMeter Converter:
    • Go to BlazeMeter Converter Tool:
    • Visit the BlazeMeter HAR-to-JMX Converter.
  • Upload the HAR File:
    • Drag and drop your .har file into the tool or click to upload.
  • Download the JMX File:
    • The tool will generate a .jmx file based on your captured requests.
    • Download and save the .jmx file.
  • Manually Import Using JMeter
Install JMeter Plugins:

Install the JMeter JSON Plugin to handle HAR files (if not installed already).
Convert HAR File Manually:

Use a HAR-to-JMX conversion tool or script (e.g., using Python libraries like har2jmx).
Install the library:
bash
Copy code
pip install har2jmx
Convert the file:
bash
Copy code
har2jmx my_file.har -o my_test_plan.jmx
3. Load and Edit the JMX File in JMeter
Open JMeter:

Launch JMeter and open the .jmx file.
Verify and Edit the Test Plan:

Ensure thread groups, samplers, and parameters are correctly set.
Edit configurations like:
Server names and endpoints.
Number of users (threads).
Ramp-up period.
Dynamic data parameterization.
Add Listeners:

Add listeners like Summary Report, View Results Tree, etc., to monitor test execution.
4. Execute and Validate
Save the .jmx file after making any modifications.
Run the test plan by clicking the green Start button.
Analyze results using the added listeners.
Example HAR Workflow
For instance, if you captured a login request, the converted .jmx will include:

HTTP Sampler: POST request with login credentials.
Thread Group: Single user or multiple users based on the configuration.
Listeners: To view the response status.
This process allows you to automate web interactions or API calls for performance testing efficiently. Let me know if you'd like help with specific parts of this!
Next Post Previous Post
No Comment
Add Comment
comment url