When I started creating performance tests with Apache JMeter, I quickly realized I didn’t know much about the features that come with the package. I’m writing this piece, especially for those that google first instead of reading manuals. Like myself.
Obviously, there are many ways to organize your tests. My main focus is to show you the concept of the Module Controller. I’m jumping straight to the subject, using the Module Controller with Apache JMeter
I wanted to create a load test with two scenarios. One scenario for accessing user pages and the second scenario to access a customers page. I started off with a single thread group to simulate the first scenario, with a particular number of users, achieving X number of page visits. So far so good.
After that I created a second thread group and started copying samplers that were already created such as Login, Homepage and Logout samplers. The idea of duplicating everything started to bug me.
In this image you can see the duplicate samplers that I was creating.
I pictured myself having to update my samplers after a software change. It would require me to update all copied samplers. Increasing maintenance efforts which in my opinion should be unnecessary. I started looking for a solution.
An “include” crossed my mind which eventually lead me to using the Module Controller.
What is a Module Controller?
The Module Controller enables you to include, or target, Controllers and Test Fragments. It allows you to avoid duplicating Samplers and efficiently use Samplers throughout your tests scenarios.
Group Samplers using Test Fragments
First, create a Test Fragment. Use Test Fragments to group samplers.
Create a Test Fragment by right clicking Test Plan
Second, add your samplers to test fragment. I’m creating a Test Fragment for a HTTP sampler accessing a particular screen named Users Overview.
Create a HTTP sampler by right clicking the Test Fragment.
Create a Thread Group
We are going to build our test scenario. Third, create your Thread Group. Which will reside on the same level as Test Fragments.
Create a Thread Group by right clicking Test Plan. This is where you can configure the number of virtual users for your scenario.
Now we have created a test fragment with a sampler. And a Thread group.
Currently, the Thread Group is empty and not doing anything when the test is executed.
Create a Module Controller
The Thread Group does not have anything to do yet. Fourth, add a Module Controller to the Thread Group.
Create a Module Controller by right clicking the Thread Group.
Target a Test Fragment
The Module Controller can target an element. Fifth and last, target your created Test Fragment. Now the Thread Group has something to do, which is navigating to the Users Overview screen.
Select your Test Fragment.
This way I have configured my two test scenarios.
All Samplers are separated in Test Fragments and my two scenarios are pointing to unique Samplers.
I’ve reached my goal: the Samplers in the Test Fragment are now run by the Module Controller. Now I only have unique Samplers. Used by different scenarios.
Having unique Sampler names will help you generate clear performance test reports.
Some tips for start using a Module Controller
“When code causing failures is copied, the failures may continue to exist if the developer is not aware of such copies.”
- Avoid Duplicating samplers and Re-Use samplers.
- Group Samplers in Test Fragments.
- Target Test Fragments one or multiple times as you desire.
- Efficiently built flows for different Thread Groups.
- Target Test Fragments in a specific order to build your scenarios.
Now Try it Yourself
- Create a Test Fragment
- Create a Sampler to the Test Fragment
- Create a Thread Group
- Create a Module Controller inside the Thread Group
- Target your Test Fragment inside your Module Controller
You are now able to organize your tests efficiently using the JMeter Module Controller.
I hope you’ve enjoyed reading this article.