JMeter Switch Controller or How to Run a sampler with a certain Percentage

From UbikWiki

Contents

NEW !!!

UP TO DATE VERSION CAN BE FOUND HERE

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

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)

How-To:

Create a Test Plan like this one:

Image:JMETER_AVERAGE_TEST_PLAN.png

  • Important parts are:
    • BeanShell Sampler
    • SwitchController which uses ${SWITCH_VALUE} as switch condition
    • 4 samplers (could be transaction controller or any other sampler)
  • Content of BeanShell Sampler are the following:
// Array of values that contain the % repartition we want
int[] values = (int[])vars.getObject("RANDOM_VALUES");
if(values==null) {
	values = new int[] {0,1,2,3,2,3,1,3,2,3};
	vars.putObject("RANDOM_VALUES",values);
}
// Increment that will be used in SwitchController value will be between 0 and 3
// because there are 4 samplers
Integer increment = (Integer)vars.getObject("INCREMENT");
if(increment==null) {
	increment = Integer.valueOf(0);
} else {
	increment = Integer.valueOf((increment.intValue()+1)%10);	
}
vars.put("SWITCH_VALUE", new Integer(values[increment.intValue()]).toString());
vars.putObject("INCREMENT", increment);
  • Note the following:
    • BeanShell 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%)


That's it.

Result:

2 VUs with Loop of 50:

Image:JMETER_AVERAGE_RESULT.png

Want to Learn more?:

Author:

Philippe Mouawad

Personal tools