Thursday, July 19, 2012

How to make JMeter to execute certain sampler in certain thread

One day I needed to develop JMeter scenario so that it could execute certain samplers in certain threads and choose the order of sequential sampler execution according to certain algorithm. Random Controller was not an option as it actually does not guarantee that the next thread will run another sampler.

Searching the solution led me to JMeter component reference page where they say that  such the cases should be handled with so called Interleave Controller. However due to some reasone that silution didn't work for me. Probaly I couldn apply that emoponent properly, anyway I used another approach.

1. Add a counter to your thread group so that it will store the numbr of sequential running thread in dedicated property (ex. cntr)
2. Group the set of samplers you'd like to choose from with Switch Controller
3. Using javaScript expression specify the condition for "Switch Value" field like it is shown on the picture below
Example of Switch controller and javascript mod (%) operator
This approach actually means that each time the new thread gets started the sequential number is generated by the conter. That value is stored in cntr property. Then the execution flow gets into Switch Controller. It evaluates the number of the item to be executed this time. Evealuation is performed with the help of javascript mod operator. Thus in above example we have two alternatives to choose from so we must get the reminder after devision current thread number by two. If we have N alternatives to choose from we must specify the following   condition:

${__javaScript(${cntr}%N)}

That's it