Skip to content
Tutorials Center
My learning

Topics

Lesson 13 of 37 ยท Test Plans and Samplers

JMeter Logic Controllers: Loop, If, While, Transaction and More

Control the flow of a JMeter test with logic controllers: Simple, Loop, If, While, ForEach, Random, Interleave, Throughput, Once Only, Switch and the Transaction Controller.

A plan that runs the same list of requests top to bottom is only the beginning. Real users make choices: some search, some browse; some are logged in; some repeat an action several times. Logic controllers let a plan make those choices. They are tree nodes that contain samplers (and other controllers) and decide whether, how often and in what order their children run.

Honesty note. JMeter cannot run here. The controllers and field names are the real ones. The runnable blocks are stand-ins: a small flow engine that imitates a few controllers so you can see the counts and ordering they produce.

Add them with Add > Logic Controller under a Thread Group or another controller. Remember that samplers and controllers run in tree order, and that other elements (timers, assertions) inside a controller apply to the samplers beneath it.

Controller overview

Controller What it does
Simple Controller Only groups children; no logic. Good for organization and scoping.
Loop Controller Runs children a fixed number of times (can use a variable).
Once Only Controller Runs children once per thread, on the first iteration only (for login).
Interleave Controller On each iteration runs one child, rotating through them.
Random Controller Runs one randomly chosen child per iteration.
Random Order Controller Runs all children, but in random order.
Throughput Controller Runs children for a percentage of iterations, or a fixed number of executions.
Runtime Controller Runs children until a number of seconds has elapsed.
If Controller Runs children only if a condition is true.
While Controller Repeats children while a condition is true.
Switch Controller Runs the child chosen by a value (index or name).
ForEach Controller Loops over var_1, var_2, ... variables.
Transaction Controller Measures a group of requests as one transaction.
Critical Section Controller Lets only one thread at a time run its children.
Module / Include Controller Reuse a Test Fragment or an external .jmx file.
Recording Controller A holder for recorded samplers.

Loop and Once Only

A Loop Controller with Loop Count 3 runs its children three times in every thread iteration. Use it for "add three items to the cart". The count may be a variable such as ${itemCount}.

A Once Only Controller is the standard place for login: the request runs on the first iteration of each thread only, while later iterations reuse the session. Beware: the Thread Group must keep the session, so tick Same user on each iteration and use a Cookie Manager.

Interleave, Random, Throughput: traffic mixes

Real traffic is a mix. Suppose 70% of visitors only browse and 30% buy. Put a Throughput Controller (Percent Executions, 30) around the checkout steps; roughly three in ten iterations will run them. Per thread vs. total: Total Executions counts over the whole test; with Percent Executions and Per User ticked each thread decides independently.

An Interleave Controller with children Search, Browse, Product page runs Search in iteration 1, Browse in iteration 2, Product page in iteration 3, then Search again. A Random Controller picks one at random each time. Both create variety without writing code.

If and While

The If Controller's condition field takes an expression. Since JMeter 5.x the recommended way is to tick Interpret Condition as Variable Expression? and write something like ${__groovy(vars.get("loggedIn") == "true")} or, for a simple comparison, ${loggedIn} when the variable itself holds true or false. Without that tick JMeter evaluates the text as JavaScript (slower, and removed in some newer setups), so prefer the variable-expression approach.

A While Controller repeats while its condition is true, for example polling an order status until it is COMPLETE:

TEXT
While Controller   condition: ${__groovy(vars.get("status") != "COMPLETE")}
  โ”œโ”€โ”€ GET /orders/${orderId}        (JSON Extractor -> status)
  โ””โ”€โ”€ Constant Timer 1000 ms

An empty condition field also has a meaning (loop until the last sampler fails), so write the condition explicitly and add a safety limit, for example a counter, so a polling loop cannot run forever.

ForEach

After an extractor with Match No. -1 creates productId_1, productId_2, ... and productId_matchNr, a ForEach Controller with Input variable prefix productId and Output variable name currentId runs its children once per extracted value, with ${currentId} set each time. That is how you open every product in a list.

Transaction Controller

Users do not care about individual requests: they care that "Checkout" is fast. A Transaction Controller wraps several samplers and reports one sample whose time is the sum of its children (plus timers, depending on the settings). Two options matter:

  • Generate parent sample: ticked makes only one combined sample (child samples hidden); unticked records the parent and the children, which can double-count in summaries unless you filter by label.
  • Include duration of timer and pre-post processors in generated sample: tick to include think time inside the transaction, usually not what you want; leave off for pure response time.

Name transactions with clear business names (T01_Login, T02_Search, T03_Checkout) and a naming scheme for filtering in reports.

flowchart TD
  TG["Thread Group"] --> TC1["Transaction: T01_Login"]
  TG --> TH["Throughput Controller 30%"]
  TH --> TC2["Transaction: T02_Checkout"]
  TC1 --> A["POST /login"]
  TC2 --> B["POST /cart"]
  TC2 --> C["POST /pay"]

Transactions measure business actions; the Throughput Controller sets how often a path is taken.

Stand-in: simulating controllers

The block below imitates Loop, Interleave, Throughput (with a seeded random) and a ForEach-like step, and counts how many samples each produces.

JS Stand-in: controller flow and counts

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Notice that Checkout appears in only a few iterations (about 30% on average), while Add item always appears twice and the interleaved step rotates.

Reuse with Test Fragments

Put common steps (login, logout, header setup) in a Test Fragment, and call them with a Module Controller (pointing at the fragment in the same plan) or an Include Controller (an external .jmx). Fix a bug once and every scenario benefits. Test Fragments never run by themselves; they only run when something calls them.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which controller runs its children only on the first iteration of each thread?

    Once Only runs its children once per thread, which suits login steps.
  2. 2. You want 30% of iterations to run the checkout steps. Which controller fits?

    The Throughput Controller can run its children for a percentage of iterations.
  3. 3. What does a Transaction Controller provide?

    It groups requests and reports their combined time under one label.
  4. 4. How does a ForEach Controller get its values?

    It iterates over prefix_1, prefix_2, ... as created by an extractor with Match No. -1.
  5. 5. What is the main difference between Random Controller and Random Order Controller?

    Random picks one child per iteration, Random Order shuffles the sequence of all children.

Summary

  • Logic controllers decide whether, how often and in which order children run.
  • Loop and Once Only handle repetition; Interleave, Random and Throughput model traffic mixes.
  • If, While and ForEach add conditions and iteration; always bound polling loops.
  • Transaction Controllers measure business actions; name them clearly.
  • Test Fragments with Module/Include Controllers give reuse.

Next: Timers and Think Time