Context:
- you want to run different parts of a Test plan a different rate
- you want to be sure one and only one of the samples occur thus you cannot use ThroughputController as you don’t have the guarantee that your sample will run
Example:
- You want to run 4 types of searches:
- Search by Last Name
- Search by Mail
- Search by Phone Number
- Search by Card Number
- You want to use the search result (so be sure it executed once)
SwitchControler or How to run a sampler with a certain percentage:
Create a Test Plan like this one:
- Important parts are:
- JSR223 Sampler + Groovy
- SwitchController which uses ${SWITCH_VALUE} as switch condition, depending on its value, Controller will execute one of the child elements that matches the Switch Value
- 4 samplers (could be transaction controller or any other sampler)
- Content of JSR223 Sampler are the following:
// Array of values that contain the % repartition we want int[] values = (int[])vars.getObject("RANDOM_VALUES"); if(values==null) { values = [0,1,2,3,2,3,1,3,2,3] as int[]; vars.putObject("RANDOM_VALUES",values); } // Increment that will be used in SwitchController value will be between 0 and 2 // because there are 3 TCs Integer increment = (Integer)vars.getObject("INCREMENT"); if(increment==null) { increment = Integer.valueOf(0); } else { increment = Integer.valueOf((increment.intValue()+1)%values.length); } String value = Integer.toString(values[increment.intValue()]); vars.put("SWITCH_VALUE", value); vars.putObject("INCREMENT", increment); SampleResult.setResponseData("SWITCH_VALUE from inside:"+value, null); return "SWITCH_VALUE:"+value;
- Note the following:
- JSR223 Sampler generates SWITCH_VALUE, a number between 0 and 3 (because there are 4 samples)
- The percentages are held in RANDOM_VALUES array, note we have:
- 10% of 0
- 20% of 1
- 30% of 2
- 40% of 3
- The generated number will decide which sampler is ran:
- If 0 is generated, the first one will be run (Sampler-10%)
- If 1 is generated, the second one will be run (Sampler-20%)
- If 2 is generated, the third one will be run (Sampler-30%)
- If 3 is generated, the fourth one will be run (Sampler-40%)
- To use Groovy, just add groovy-all-VERSION.jar (in embeddable folder) in JMETER_HOME/lib folder
- Note that you see in JSR223 Sampler a new feature of upcoming 2.9 version which allows Script (Specific to certain JSR223 implementations) to be compiled and cached increasing dramatically performances of Script code.
That’s it.
Result:
2 VUs with Loop of 50:
Conclusion:
As you can see coding custom code and using it in JMeter is rather easy but always remember:
- Only script when you don’t find the element
- Script efficiently and use the languages that perform well not ones that you master
About the author:
Philippe Mouawad is a developer, committer and member of the JMeter Project Management Committee at Apache.
He is also the co-author of the book Master JMeter : from load testing to DevOps.
He currently works as an Architect and technical expert for Ubik-Ingenierie where he leads among other things the development of UbikLoadPack a set of Commercial Plugins for Apache JMeter allowing to load test different protocols like MPEG-DASH, Http Live Streaming (HLS), HSS, HDS, GWT, JavaSerialization, Oracle applications.
About us:
- We provide professional services for Load Testing
- We’re strong contributors to JMeter and its eco-system
- Ubik Load Pack is a set of Enterprise Grade plugins for Apache JMeter. Our solutions are used by Big players
- Get a Free trial
1 Comment
-
Vincent DABURON
on 3 January 2013I could use also a beanshell sampler to compute the Switch_value like this :
BeanShell compute SWITCH_VALUE
================================
rand = vars.getObject("P_RAND");
if (rand == null) {
rand = new java.util.Random(100);
vars.putObject("P_RAND", rand);
}
int iAlea = rand.nextInt(100);
if (0 <= iAlea && iAlea < 10) {
// 10%
sCase = "0";
}
if (10 <= iAlea && iAlea < 30) {
// 20%
sCase = "1";
}
if (30 <= iAlea && iAlea < 60) {
// 30%
sCase = "2";
}
if (60 <= iAlea && iAlea < 100) {
// 40%
sCase = "3";
}
log.info("sCase = " + sCase);
vars.put("SWITCH_VALUE",sCase);
==============================
The main advantage is that BeanShell is installed by default with JMeter
Comments are closed.
Recent Posts
- How to setup ads insertion on video streaming with AWS Services 26 November 2024
- UbikLoadPack Video Streaming Plugin 10.0.0 26 November 2024
- UbikLoadPack Video Streaming Plugin 9.1.6 26 November 2024