如何使用JMeter的While循环重复执行采样器直至返回Completed值
Got it, let's walk through how to set this up in JMeter using a While Controller—it's the perfect tool for this kind of polling scenario where you need to repeat a request until a certain condition is met.
First off, let's confirm you've already got the regex extractor working correctly—make sure it's pulling the status value into a variable (let's say you named it ${status} for this example). If you haven't already, toss in a Debug Sampler to verify that the variable updates with each HTTP request's response.
Step 1: Add a While Controller
- Right-click your Thread Group > Add > Logic Controller > While Controller.
- In the "Condition (function or variable)" field, use one of these reliable expressions to keep looping until the status hits
Completed:- Using JEXL3 (simple and widely supported):
The${__jexl3("${status}" != "Completed" || "${status}" == "",)}|| "${status}" == ""part handles the first run, where thestatusvariable might not exist yet. - Using Groovy (more flexible for complex logic):
${__groovy(vars.get("status") == null || !vars.get("status").equals("Completed"),)}
- Using JEXL3 (simple and widely supported):
Step 2: Move Your HTTP Request & Regex Extractor Into the While Controller
- Drag your target HTTP Request (and the regex extractor that pulls the
statusvalue) inside the While Controller. This ensures they run every loop iteration. - Double-check the regex extractor's scope: it should be a child of the HTTP Request so it updates the
${status}variable after each response.
Step 3: Add a Delay to Avoid Overloading the Server
Since you mentioned this could take hundreds of attempts over minutes, adding a small delay between requests is kind to the server:
- Right-click the HTTP Request > Add > Timer > Constant Timer.
- Set the "Thread Delay (in milliseconds)" to a reasonable value—like
3000(3 seconds) or whatever aligns with your system's rate limits.
Step 4: Add a Safety Limit to Prevent Infinite Loops
To avoid getting stuck in an endless loop if something goes wrong (e.g., the status never becomes Completed), add a counter to cap the number of iterations:
- Right-click the While Controller > Add > Config Element > Counter.
- Name it something like "Loop Counter"
- Set "Start" to
1, "Increment" to1, "Reference Name" toloopCount
- Update your While Controller condition to include the counter limit. For example, if you want a max of 500 attempts:
- JEXL3 version:
${__jexl3((${loopCount} < 500) && ("${status}" != "Completed" || "${status}" == ""),)} - Groovy version:
${__groovy((vars.get("loopCount") as int) < 500 && (vars.get("status") == null || !vars.get("status").equals("Completed")),)}
- JEXL3 version:
Step 5: Test the Setup
- Add a Debug Sampler inside the While Controller (right-click > Add > Sampler > Debug Sampler) and run a quick test.
- Check the View Results Tree to confirm:
- The
${status}variable updates each loop - The loop stops as soon as
${status}becomesCompleted - The loop terminates if it hits your max iteration limit
- The
That's it! This setup will keep polling the endpoint until the status is Completed, with built-in safeguards to avoid overloading the server or infinite loops.
内容的提问来源于stack exchange,提问作者Bala

