JMeter : SwitchControler or How to run a sampler with a certain percentage

0saves

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)

How-To:

Create a Test Plan like this one:

JCP_SWITCH_CONTROLER

  • 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;

JCP_JSR223

 

  • 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:

JCP_RESULTS

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

See also :

Ubik Load Pack

Author:

Philippe Mouawad

0saves
If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

One thought on “JMeter : SwitchControler or How to run a sampler with a certain percentage

  1. Vincent DABURON

    I 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.