You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用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-by-Step Setup

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):
      ${__jexl3("${status}" != "Completed" || "${status}" == "",)}
      
      The || "${status}" == "" part handles the first run, where the status variable might not exist yet.
    • Using Groovy (more flexible for complex logic):
      ${__groovy(vars.get("status") == null || !vars.get("status").equals("Completed"),)}
      

Step 2: Move Your HTTP Request & Regex Extractor Into the While Controller

  • Drag your target HTTP Request (and the regex extractor that pulls the status value) 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:

  1. Right-click the While Controller > Add > Config Element > Counter.
    • Name it something like "Loop Counter"
    • Set "Start" to 1, "Increment" to 1, "Reference Name" to loopCount
  2. 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")),)}
      

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} becomes Completed
    • The loop terminates if it hits your max iteration limit

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:16:29