DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • JMeter Plugin HTTP Simple Table Server (STS) In-Depth
  • Building an LLM-Powered Product to Learn the AI Stack: Part 1
  • Data Analysis and Automation Using Python
  • Getting Started With OCR docTR on Ubuntu

Trending

  • Immutable Secrets Management: A Zero-Trust Approach to Sensitive Data in Containers
  • A Modern Stack for Building Scalable Systems
  • Microsoft Azure Synapse Analytics: Scaling Hurdles and Limitations
  • How to Configure and Customize the Go SDK for Azure Cosmos DB
  1. DZone
  2. Data Engineering
  3. Data
  4. How to Use the While Controller in JMeter

How to Use the While Controller in JMeter

Dmitri Tikhanski shows how to use the While Controller in Apache JMeter, a tool that implements the 'while loop' for repeated if-then statements.

By 
Dmitri Tikhanski user avatar
Dmitri Tikhanski
·
Jul. 13, 16 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
38.1K Views

Join the DZone community and get the full member experience.

Join For Free

When it comes to building an advanced JMeter load test scenario that assumes complex logic, depending on certain conditions it is sometimes required to change the Samplers execution order. Usually JMeter runs all the Samplers upside down, but there could be some scenarios when the default behaviour is not suitable.

This post covers the While Controller—JMeter’s implementation of while loop. For those not familiar with the while loop concept, to put it simply, it’s just a repeated “if->then” statement.

If something is true, then repeat the last action

In JMeter, the controller basically runs children Samplers, unless some predetermined “Condition” is “true” where “Condition” could be:

  1. blank - in this case, the While Controller will exit when the last sampler in the loop fails.
  2. LAST - the same as blank, but with an additional check for the last Sampler before the loop result. If the preceding Sampler is not successful, the While Controller and its children won’t be executed at all.
  3. JMeter Function or Variable - the While Controller’s children will be run until a variable or function evaluation result is “true”.
  4. JMeter Property  - the same as Function or Variable, but assumes the Property instead.

Both the Function/Variable and Property approaches assume that the “Condition” will be set to “false” somewhere in or outside the loop. In the case of Property, it can be done from another Thread Group or even outside JMeter, for example via the Beanshell Server.

The first two cases with “blank” and “LAST” are pretty straight-forward. If you leave the “Condition” blank or set it to LAST, the loop will break if the last sampler under the While Controller is not be successful. The same applies to the Variables and Properties. The While Controller will loop its children until the Variable or Property specified in “Condition” will result in “true”.

Using Functions in the While Controller

As per the line in parentheses in the While Controller GUI, the condition should be “function or variable”, as seen below.

While Controller in JMeter

This means that, unlike the If Controller, which automatically interprets the “Condition” as JavaScript, the While Controller expects it to be either “true” or “false”. Therefore:

  • "${myVar}" != "some value" - doesn’t work, as it is neither a function nor a single variable.
  • ${__javaScript("${myVar}" != "some value",)} - works OK as the expression resolves it to either be “true” or “false”.

Additionally, remember that in the absolute majority of cases, JMeter Variables are stored as Strings, so if you’re using __javaScript() or a similar function in order to compare the variable value to something, make sure that both counterparties are of the same type. For example, in JavaScript just surround both the variable and the expected value with quotation marks and they will be interpreted as strings.

Now that we’ve explained how the While Controller is used, let’s take a look at some use cases.

Potential Use Cases

Looping Until a Response Contains a Specified Line

If you’re testing a system built on, let’s say, the eventual consistency principle or a multi-component system where the response is not immediate, you might have to wait until something becomes available so you can proceed. In these scenarios, the While Controller is the most obvious choice to turn to to. If you need to decide whether to continue or not based on the presence of some text in the response—it can be done as follows:

  1. Add the While Controller to your Test Plan
  2. Put the HTTP Request sampler (or any other sampler if you’re using a different protocol) under the While Controller.
  3. Save the partial or the whole response into a JMeter Variable. It can be done using the Regular Expression Extractor.
  4. Use the following Condition in the While Controller:

${__javaScript("${response}".indexOf("string") == -1,)}

Where:

  • ${response} - the JMeter Variable which holds partial or whole response
  • string - the character sequence you’re looking for

Once the string will be found in the ${response} the loop will exit and the test will continue (or end).

If there won’t be any occurrences of the string in the ${response} the request will be repeated forever.

The above approach assumes the Regular Expression Extractor and __javaScript() function combination, and in the String.indexOf() the JavaScript method is used to determine whether the search criteria are present in the response or not.

While Controller in JMeter

Looping Until the Response Contains a Specified Line, But Not More Than X Times

This use case is similar to the previous one, with some extra “protection," in order to avoid an endless loop if the text you’re looking for never appears.

Let’s retry for five times and if the string will not be found in the response, then exit the loop. To implement, you just need to:

  1. Add a Counter test element under the While Controller.
  2. Configure the Counter to:
    • Start from 1
    • Increment by 1
    • Have a counter reference name
  3. Change the While Controller Condition to consider the counter value as well, like:

${__javaScript(("${response}".indexOf("string") == -1 && ${counter} < 5),)}

While Controller in JMeter

As you can see, the while loop stopped after 5 attempts instead of running forever. 

Reading All Values From the CSV and Continue

This scenario assumes using the CSV Data Set Config for parametrization. In most cases, it is utilized for recycling or stopping the JMeter Thread when the end of the file is reached. Now we will see how to read all the values from the CSV file and continue the test afterwards.

Assuming that CSV file contains only five lines:

line 1
line 2
line 3
line 4
line 5

And the following CSV Data Set Configuration:

While Controller in JMeter

Let’s see what happens with the blank Condition in the While Controller.

While Controller in JMeter

As you can see, the test continues forever and returns the <EOF> value for each subsequent call. EOF stands for End Of File. If you don’t like it, you can change it for whatever you want to see instead via the csvdataset.eofstring property.

So given we have this “<EOF>” thing, now we can use it in the While Controller Condition as:

    ${__javaScript("${myVar}" != "<EOF>",)}

Let’s see what happens now:

JMeter While Controller

Almost done. I assume you don’t want this <EOF> bit at all? It is there as the CSV data is being read in the loop before the While Controller condition is evaluated, so that the While Controller is always one step behind. You can get rid of it using the If Controller by the following steps:

  1. Put the If Controller under the CSV Data Set Config.
  2. Put all the samplers which use the ${myVar} variable under the If Controller.
  3. Use "${myVar}" != "<EOF>" as the If Controller condition.

Let’s see how it looks now:

JMeter While Controller

Seems to be exactly what we’re looking for.

The funny thing is that assuming you use the If Controller, you can even leave the While Controller’s Condition blank.

This is it on the While Controller, if anything remains unclear or your scenario is not covered, feel free to use comments form below.

Want to Learn More?

For experienced JMeter users, you'll want to view the on-demand webcast, How to Create Advanced Load Testing Scenarios with JMeter.

If you are new to JMeter, and you’d like to learn more, please sign up for our free online JMeter training course. 

Data set

Published at DZone with permission of Dmitri Tikhanski, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • JMeter Plugin HTTP Simple Table Server (STS) In-Depth
  • Building an LLM-Powered Product to Learn the AI Stack: Part 1
  • Data Analysis and Automation Using Python
  • Getting Started With OCR docTR on Ubuntu

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!