Réalisation et Expertise technique IT

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

on 30 décembre 2012 GROOVY, JMETER, LOAD_TESTING, PERFORMANCE and Étiquettes : , , , with 1 comment

About author:

Philippe Mouawad 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 HLS, GWT, Flex.
Philippe is a committer on the Apache JMeter project and member of the PMC since October 2011.
Finally he is a co-author of a french book on JMeter called Maîriser JMeter: Du test de charge à Devops.


  • 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


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



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


2 VUs with Loop of 50:



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

Ubik Load Pack is a JMeter based solution for complex protocols.

Follow us on Twitter and StackOverflow.

Follow Apache JMeter on Twitter

1 Comment

  • Vincent DABURON
    on 3 janvier 2013

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



    The main advantage is that BeanShell is installed by default with JMeter

Comments are closed.