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:
- 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."
6. Sample Data Files
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:
- Open Developer Tools:
- Perform the Action:
- Filter for Relevant Requests:
- Export the HAR File:
- 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