DEV Community

Amber Race
Amber Race

Posted on

Creating Weighted Flows in JMeter

It is pretty common for applications to have multiple flow paths. For example, it is common in mobile games to have one call flow for players that have authenticated with Facebook and a very different one for anonymous players. When trying to replicate a realistic server load in JMeter, it is important to be able to handle these different paths that users take through the client application.

JMeter actually has this capability right out of the box, no Groovy scripting or secondary plugins needed. With a Random Variable and a few If Controllers, you can enable any number of different flow paths in your load testing script.

Flip a Coin

Let's start with the simplest scenario: two paths which you want evenly split in your test. To do this, first add Random Variable element that will generate a number between 1 and 2 Be sure to set the Per Thread option to True so the coin gets flipped on each of your user threads.

JMeter Coin Flip

JMeter Coin Flip

Next add two If Controllers to hold your two paths.

Condition A:
${COIN_FLIP} == 1
Condition B:
${COIN_FLIP} == 2

And that is all you need to do - simple! You can use this same strategy to split your test up evenly among any number of paths; just increase the Maximum Value of your Random Variable.

JMeter Straws

Percentage A/B

What about when you have two different paths, but one is more common than the other? And what if you want to test different ratios of the A and B paths? In that situation, I've used the following technique:

First, add a User Defined Variable element to your script to track what percentage of iterations should go through the A flow.

PCT_A

Then set your Random Variable to have a max value of 100.

Random Is A

Now we go to our If Controllers and in the one for the A path, put the following condition:

${IS_A} <= ${PCT_A}

And in the Not A path, put the opposite:

${IS_A} > ${PCT_A}

And voilà, a near perfect split between the two paths based on the given percentage. If you are feeling really saucy, you can set the PCT_A value as a property that is passed in on the command-line. But that is a topic for another post.

Example File

A script illustrating all these examples is available here: WeightedFlows.jmx. If there is another example you would like to see, please let me know!

Originally posted here

Top comments (0)