Sep. 4th, 2017

How to Turn Off Load Test Parts With the JMeter If Controller

When running a load test, we might not always want to run all of it at the same time. Sometimes we might need to enable / disable some parts of our script. This can happen, for example, when we want to check only certain parts of our system. Or, when load testing for Black Friday, when we want to turn off 3rd parties.

 

But opening up the entire script and selecting the part we want to modify, is a bit tedious. This blog post will show you a quick way to turn off test parts, with the Apache JMeter™ If Controller. The If Controller allows the user to control whether the test elements below it (its children) are run or not, according to user defined variables. The If Controller gave us a lot of flexibility for making our test scripts more usable in cases where we have samplers we don’t always want to be processed.

 

To learn the basics of the If Controller, click here. To learn more advanced If Controller best practices, check out this blog post: “6 Tips for JMeter If Controller Usage”. Now let’s get started.

 

Suppose a user goes to an e-commerce website for phones, like this demo one:

 

How to Turn Off Load Test Parts With the JMeter If Controller

 

They search for a phone, select the one s/he likes and add it to the cart. The following script corresponds to the automation of these user actions. You can see the user waits for the page to load, searches for the relevant item, selects the chosen item and adds it to the cart:

 

turning off parts in your test with the if controller

 

But now, let’s suppose the user sees the item s/he wants on the front page and selects it. In this scenario, the “Search” step in the script is useless and needs to be disabled, as that request will never be sent.

 

One way to achieve the same result and avoid manually disabling the search step is by using an If Controller.

 

We can do this in two ways:

  • Define a property and run the value through the console
  • Define a variable and modify its value in the script

 

The choice depends on how you run your script, either by the console or by JMeter’s interface. Let’s go into each option in more depth.

 

1. Define a Property

 

Define a property and then pass the value through the console. The advantage of this way is that we don’t even have to open JMeter when running the test. This is more practical and it also consumes less resources, thus increasing the accuracy of our tests.

 

To configure this option, add the If Controller to the script with the “Search” step nested as a child element under it.

 

jmeter if contoller to turn off parts in the test

 

Add the property ${__P(do_the_search,0)} == 1  to the If Controller:

 

jmeter if contoller to turn off parts

 

What this will do is define the property “do_the_search”, assign it a value of zero and compare if it's equal to one. If the condition is evaluated as true, the search step will follow, otherwise the search will be turned off.

 

Once we have done that, and in order to run the script with the search part of the script turned on, we simply pass this command to the console:

jmeter -n -t <test-name> -Jdo_the_search=1

 

  • -n: Specifies that JMeter is to run in non-gui mode
  • -t: The name of JMX file that contains the Test Plan
  • -J: Pass the property value.

 

Now, the “Search” step will run when executing the script. To turn off this part, simply run the command without passing the property value. As explained before, the default value is zero, so the condition will be false. Or you can pass the property evaluated to 0, if you want to be more explicit.

 

2. Define a Variable

 

Define a variable and then modify its value in the script. The advantage of this way is that it is useful when we are debugging our script, and don’t want to consume data.

 

Add the If Controller to the script with the “Search” step nested as a child element under it.
Then, go to user defined variables and define the variable “do_the_search” and assign it a value of 1. In the If Controller, add this function: ${do_the_search} == 1

 

jmeter if controller

 

Now when running the script, the condition in the If Controller will be evaluated true and the search step will be turned on. If we need it turned off, we have to change the value of do_the_search to 0.

 

That’s it! You now know how to use the If Controller. Now you can build advanced scenarios on it when creating our Black Friday scripts that include 3rd parties.

 

Get ready for Black Friday 2017. Start load testing now and ensure your site or app can handle high traffic this Black Friday.

 

Click here to subscribe to our newsletter.

 

To try out BlazeMeter, put your URL or JMX file in the box below and your test will start in minutes.

 

     
arrow Please enter a valid URL

Interested in writing for our Blog? Send us a pitch!