Did you know that it is possible to use the Java’s Switch-Case structure on JMeter as well? Even if you didn’t, now you know, and we will discover how to use switch controller & simple controller on JMeter in this blog post. Let’s learn how!

We use Switch Controller in cases where we dynamically need to change the requests we want to send, depending on a value.

Simple Controllers are mostly used for organizing and documentation, but if you use them inside a Switch Controller, each one will become a case of Switch-Case structure.

To better understand, let’s create a JMX that reads a value from a CSV file and passes that to a Switch Controller to run the corresponding case.

You can see an example of creating Simple Controllers inside a Switch Controller below.

Now we need to create a CSV file holding some values:

Switch controller 2

We need to add a CSV Dataset Config to read the values from CSV file:

Switch controller 3

Variable name for the values we get from CSV are now set as “switchValue”. Here is how we pass that value to the Switch Controller:

Switch controller 4

How JMeter Controllers Work?

The first Simple Controller in Switch Controller is always the Default one, and the other ones below that are cases.

In this example, we have 3 Simple Controllers inside the Switch Controller. So, the first one is our Default Case, second one is Case 1 and third one is Case 2.

That means if we pass the value “1” to Switch Controller, Samplers inside our Case 1 are going to trigger and if we pass “2” same thing is going to happen for Case 2.

In this case, if we pass any variable except “1” and “2” it will trigger our Default case.

Let’s experience that on our example. We got the values “0,0,0,1,1,2,2,5,5,5” in our CSV. We expect Option 1 and Option 2 to work when we get “1” and “2”, but for any other values Default one should work.

Let’s set the iteration as 10, add JSR223 Sampler in each Simple Controller and take a console log to see how it is going to work.

Iteration:

Switch controller 6 iteration

Default:

Switch controller 7 default

Option 1:

Switch controller 8

Option 2:

Switch controller 9

Here is the result:

Switch controller 10

Option 1 logged for “1”, Option 2 logged for “2“ and Default logged for “0” and “5”.

That’s it! Now you know how to use switch controller & simple controller on JMeter. To keep learning and staying up to date, keep in touch with Loadium blog posts. Happy load testing with Loadium!