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

如何在JMeter中实现异步请求重试及事务控制器内指定请求重复执行?

Hey there! Let’s walk through exactly how to build both of your JMeter scenarios with clear, actionable steps.

Scenario 1: Repeat Async Request Until Correct Response is Received

This is a classic "polling" pattern for async operations—here's how to implement it:

  • Step 1: Set up the base structure
    Start with a Thread Group, then add an HTTP Request element configured to hit your async endpoint.

  • Step 2: Add a response validator
    Under the HTTP Request, add either a Response Assertion (for plain text/HTML) or JSON Assertion (for JSON responses). Configure it to check for your expected "correct" response content (e.g., a status field like "status": "completed").

  • Step 3: Add a loop control variable
    Add a JSR223 PostProcessor right after the assertion. Use this to set a variable that controls whether the loop continues:

    // Check if all assertions passed
    def allAssertionsPassed = prev.getAssertionResults().every { !it.isFailure() }
    
    // Set variable: "true" = keep looping, "false" = stop
    vars.put("keep_polling", allAssertionsPassed ? "false" : "true")
    
  • Step 4: Wrap everything in a While Controller
    Drag your HTTP Request, assertion, and postprocessor into a While Controller. Set the controller's condition to ${keep_polling}—this will repeat the request until the assertion passes (and keep_polling is set to false).

  • Optional: Add a delay between polls
    To avoid overwhelming your service, add a Constant Timer inside the While Controller (before the HTTP Request) with a reasonable delay (e.g., 1000ms for 1 second between requests).

Scenario 2: Conditional Repeat of a Single Request in a Transaction Controller

For this scenario, we need to run 70 requests in a transaction, with one specific request repeating only if its response matches your criteria. Here's how:

  • Step 1: Create the Transaction Controller
    Add a Transaction Controller under your Thread Group. If you want to include timer/pre-processor durations in the transaction metrics, check the box labeled "Include duration of timer and pre-post processors in generated sample".

  • Step 2: Initialize a control variable
    Add a JSR223 PreProcessor directly under the Transaction Controller to set up our loop control variable:

    // Start with "true" to run the target request at least once
    vars.put("repeat_target", "true")
    
  • Step 3: Wrap the target request in a While Controller
    Add a While Controller inside the Transaction Controller, set its condition to ${repeat_target}. Then add your target HTTP Request (the one that needs conditional repetition) inside this While Controller.

  • Step 4: Add validation for the target request
    Under the target HTTP Request, add a Response Assertion or JSON Assertion to check for the text you want to match.

  • Step 5: Update the loop variable based on validation
    Add another JSR223 PostProcessor under the target request to update the repeat_target variable:

    def responseMatches = prev.getAssertionResults().every { !it.isFailure() }
    
    // If response matches, keep repeating; if not, stop and move to next requests
    vars.put("repeat_target", responseMatches ? "true" : "false")
    
  • Step 6: Add the remaining 69 requests
    Place the other 69 HTTP Request elements directly under the Transaction Controller (at the same level as the While Controller). These will run immediately after the While Controller stops (either when the response doesn't match, or if you add a max loop limit).

  • Optional: Prevent infinite loops
    To avoid repeating the target request forever, modify the While Controller condition to include a max loop count. For example, to cap it at 10 repeats:

    ${__jexl3(${repeat_target} == "true" && ${__counter(TRUE, loop_num)} <= 10,)}
    

Quick Tips

  • Always use Groovy for JSR223 elements—it's the most performant option in JMeter.
  • Use View Results Tree during debugging to verify that loops and assertions are behaving as expected.
  • For JSON responses, prefer JSON Assertion over regex—it's more accurate and easier to maintain.

内容的提问来源于stack exchange,提问作者Bal Kishan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 12:32:47