Overview:
As part of performance testing, I had to come up with performance test scripts for various use cases / business workflows for our application. When I design my performance test scripts, I will ensure that I have reusable test scripts as mentioned in this article.
JMeter – How To Create Reusable & Modular Test Scripts
If you have not read this article already, I would request you to read the article first and proceed with this article! As part of the above article, you can learn how to properly design your performance test scripts / test plan.
In a high level, I maintain a reusable ‘test script’ modules under ‘Test Fragment’. Then I use Module Controller to call specific modules to create the workflow.
I also create multiple ‘Thread Groups’ for each business functionality / workflow.
Once you have a test plan as mentioned above with multiple thread groups, then let’s see how we could maintain the JMeter test plan using Property files & running a specific thread group on a specific environment in this article!
Creating a Simple Test Plan:
Let’s consider an application with below business functionalities.
- New User Registration
- User Login & Order Creation
- User Login & Product View
- Existing Order Edit/Cancel
- User Search
Lets also assume that we already came up with performance test plan with multiple thread groups as shown above. Each thread group will perform the specific functionality.
Sample Performance Test Requirement:
Now Lets assume we have performance requirements like,
- Test each module independently (just run 1 thread group at a time).
- Test combination of modules (more than 1 thread group / all).
- Test in different environment (same test in different environment – In my project, I also run the same test in 2 different environments – QA & Staging).
Parameterizing Environment Details:
- First, lets create different property files containing the test-environment details for each environment.
# QA.properties
test.environment.hostname=10.11.12.14
test.environment.port=8080
test.environment.protocol=http
# Staging.properties
test.environment.hostname=10.11.12.13
test.environment.port=443
test.environment.protocol=https
- Now add a ‘Property File Reader‘ to read specific environment details at run time.
- Then remove all the Server Name/IP, Port, Protocol details from the HTTP Requests/Samplers in your test plan.
- Let the HTTP samplers have only the Path, Parameters, other request specific information.
- Add a HTTP Request Defaults under Test plan as given below.
(If the HTTP request is missing an information, JMeter will get that from HTTP Request Defaults. So, updating IP, port details in one place will get reflected for the entire test).
Note:
- ${__P(test.environment.hostname)} will retrieve the value of the ip address or hostname given in the property file.
Now your test plan is modified to run the script on any given test environment.
Parameterizing Thread Group Details:
- We have multiple thread groups in our test plan.
- We will be passing thread group user count, ramp-up period, thread group duration information via property files to the test. Lets create a property file as given below.
# Anonymous User - Product Search
group1.usercount=10
group1.rampup=10
group1.duration=600
# New User Registration
group2.usercount=10
group2.rampup=10
group2.duration=600
# User Login & Order Creation
group3.usercount=10
group3.rampup=10
group3.duration=600
# User Login & Existing Product View
group4.usercount=10
group4.rampup=10
group4.duration=600
# BackOffice Admin User Activities - Existing Order Edit/Cancel
group5.usercount=10
group5.rampup=10
group5.duration=600
- All the thread groups should be modified to use the properties as given below.
- To run 1 specific thread group, we can change the user count (number of users) property for the Thread Group. for ex, If we need to run only ‘New User Registration’ module, user count property can be set to 0 for all other modules.
Note:
If the Thread Group user count is 0, JMeter can not execute the Thread Group. By updating thread group user count property to 0, a thread group can be disabled.
- Lets create a multiple property files with different combinations as you might be interested. For ex, anonymoususers-only.properties will have below properties.
# Anonymous User - Product Search - Only
# Anonymous User - Product Search
group1.usercount=100
group1.rampup=100
group1.duration=3600
# New User Registration
group2.usercount=0
group2.rampup=0
group2.duration=0
# User Login & Order Creation
group3.usercount=0
group3.rampup=0
group3.duration=0
# User Login & Existing Product View
group4.usercount=0
group4.rampup=0
group4.duration=0
# BackOffice Admin User Activities - Existing Order Edit/Cancel
group5.usercount=0
group5.rampup=0
group5.duration=0
So, I create multiple property files to control specific thread group execution.
- registration-only.properties
- ordercreation-only.properties
- all-modules.properties..etc
Add one more ‘Property File Reader’ to read specific module you are interested in executing.
Controlling Thread Group & Environment via Command Line:
- At this point, We have created a JMeter test plan as explained above with 2 Property File Readers.
- Now, lets parameterize the environment and module using properties which I will pass via command line at run time.
Rest is simple!!!
- To run all modules on Staging environment
jmeter -n -t test.jmx -l result.jtl -Jenvionment=staging -Jmodule=all-module
- To run all modules on QA environment,
jmeter -n -t test.jmx -l result.jtl -Jenvionment=QA -Jmodule=all-module
- To run order creation module on Staging,
jmeter -n -t test.jmx -l result.jtl -Jenvionment=staging -Jmodule=ordercreation-only
To run registration module on QA,
jmeter -n -t test.jmx -l result.jtl -Jenvionment=QA -Jmodule=registration-only
Triggering Thread Groups via Jenkins:
If we use Jenkins to run our test, these test environment & module can be drop down list containing the possible values & passed as parameters from Jenkins to JMeter.
Thus, We can run specific thread group on specific environment without modifying the JMeter test as given below.
More details on JMeter-Jenkins integration can be found here.
Summary:
By properly designing the JMeter test plan with reusable test scripts, multiple thread groups and Property File Reader etc, we are able to control specific thread group execution at run time.
So we can focus on performance related issues for a specific module instead of running the whole test plan every time.
Happy Testing & Subscribe 🙂