Creating Weighted Flows in JMeter
Tags: jmeter, performance, load, testing
Published: 2019-04-15 01:42:07

---
title: Creating Weighted Flows in JMeter
published: true
description: How to support multiple user flows in a single JMeter script
tags: jmeter, performance, load, testing
---

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 <strong>Random Variable</strong> and a few <strong>If Controllers</strong>, you can enable any number of different flow paths in your load testing script.
<h2>Flip a Coin</h2>
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.

<figure><img class="alignnone  wp-image-255" src="https://thepracticaldev.s3.amazonaws.com/i/4dvnqt7vce2qwgpllbu5.png" alt="JMeter Coin Flip" width="298" height="249" />
<figcaption>JMeter Coin Flip</figcaption></figure>

Next add two If Controllers to hold your two paths.
<h5>Condition A:</h5>
<pre>${COIN_FLIP} == 1</pre>
<h5>Condition B:</h5>
<pre>${COIN_FLIP} == 2</pre>
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.

<img class="alignnone  wp-image-267" src="https://thepracticaldev.s3.amazonaws.com/i/qgwbnbbibbxthlcypl62.png" alt="JMeter Straws" width="503" height="202" />
<h2>Percentage A/B</h2>
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.

<img class="alignnone  wp-image-287" src="https://thepracticaldev.s3.amazonaws.com/i/5ydfzf25ke06y2p0kqt8.png" alt="PCT_A" width="283" height="159" />

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

<img class="alignnone  wp-image-322" src="https://thepracticaldev.s3.amazonaws.com/i/0gvfrzm879b0lxn42z95.png" alt="Random Is A" width="288" height="279" />

Now we go to our If Controllers and in the one for the <em>A</em> path, put the following condition:
<pre>${IS_A} <= ${PCT_A}</pre>
And in the <em>Not A</em> path, put the opposite:
<pre>${IS_A} > ${PCT_A}</pre>
And <em>voilà</em>, 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.
<h2>Example File</h2>
A script illustrating all these examples is available here: <a href="https://github.com/ambertests/JMeterExamples/blob/master/WeightedFlows.jmx" target="_blank" rel="noopener">WeightedFlows.jmx</a>. If there is another example you would like to see, please let me know!
<p style="padding-left: 30px;"></p>
<i>Originally posted <a href="https://ambertests.com/2017/03/21/creating-weighted-flows-in-jmeter/" target="_blank" rel="noopener">here</a></i>