Jmeter | While Loop — Best Use Cases

Priyank Shah
3 min readFeb 5, 2020

Problem Statement:

I need to implement performance test where -* Need to Implement FOR loop to cater some project need
* I need to repeat requests based on some conditions and need to exit loop based on some conditions

Controller | What is “WHILE” & How “IF” is Different?

  • If controller — If condition met, children are executed one time Else zero
  • While controller — Till Condition is not met, children are executed multiples times Else zero in case of condition not met. An Exit condition is mandatory : something inside the while controller must switch the condition to false at some point, otherwise the while controller will run forever.

How to use WHILE Controller?

The While Controller runs its children until the condition is “false”.

While Controller
Possible condition values:* blank - exit loop when last sample in loop fails
* LAST - exit loop when last sample in loop fails. If the last sample just before the loop failed, don't enter loop.
* Otherwise - exit (or don't enter) the loop when the condition is equal to the string "false".

Condition:

  • The condition should be a “function or variable”. The while controller expects the condition to be “true” or “false”.
  • Therefore, variable comparison like “${Variable}”==”Value” does not work as the comparison does not evaluates to either “true” or “false”.
  • To evaluate such variable comparisons to “true” or “false” use __Javascript function . ${__javaScript(“${Variable}”==”Value”,)}, which evaluates to “true” or “false”.
  • We recommend you to use javascript conditional evaluation and avoid beanshell.

Let’s Build Common Use Cases:

Now, we understood how “While” controller works. Let’s see few common use cases.

A) Need to implement “FOR” loop:

  • A while can be seen as a for loop when used in conjunction with a counter. see below screenshot for more information.
  • JSR223 Sampler: Initializes the counter to the value 1:
vars.put("counter","1");
  • While Controller: It checks for the counter value:
${__javaScript(parseInt(vars.get("counter"))<=5)}
  • JSR223 Sampler: it increments the counter:
int counter = Integer.parseInt(vars.get("counter")) +1; vars.put("counter",Integer.toString(counter));

Well, Can’t we have something In-Built which manages the Counter in Jmeter? Yes — we have called Counter” as Config Element. Above objective can be achieved Without writing JSR223 Sampler.

Counter Config Element
Where --* Allows the user to create a counter that can be referenced anywhere in the Thread Group.
* Exported Variable Name: This will be the variable name under which the counter value is available. If you name it counterA, you can then access it using ${counterA} as explained in user-defined values.

Note: Probably, Given problem statement can be addressed by different ways in Jmeter which I have not explored yet. Source code( LearnWhileLoop.jmx) is available on GitHub.

If you enjoyed the article, do me a favor and smack the 👏👏 👏 multiple times — your support means the world to me!

--

--

Priyank Shah
Priyank Shah

Written by Priyank Shah

Agile Product Leader | Delivery Manager | Design Thinker (PRINCE2, CSPO™, CSM™, SFC™, ISTQB)

No responses yet