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

JMeter中如何让线程组2、3在指定步骤等待线程组1完成外部任务?

解决方案:JMeter多线程组依赖Jenkins任务完成的动态等待实现

Absolutely, this scenario is totally achievable in JMeter—no rigid fixed delays needed! Here's a practical, step-by-step approach tailored exactly to your requirements:

Core Idea

We'll use JMeter's global shared properties (the props object) to signal when the Jenkins task is done, paired with a While Controller to make Thread Groups 2 and 3 wait dynamically until that signal is set.


1. Set Up Thread Group 1 (Jenkins Task Trigger & Status Polling)

This thread group's job is to kick off the Jenkins task, then keep checking its status until it finishes, and finally set a global completion flag.

  • Add an HTTP Request Sampler to trigger your Jenkins job:
    • Request URL: http://your-jenkins-instance/job/Your-Job-Name/build (use buildWithParameters instead if you need to pass job arguments)
    • Method: POST
    • Pro Tip: If your Jenkins requires authentication, add an HTTP Header Manager with an Authorization header (format: Basic [base64-encoded username:password])
  • Add a Regular Expression Extractor to pull the Build Number from Jenkins' response header (look for the Location field, which ends with the build ID)
  • Add a While Controller with the condition: ${__jexl3(${BUILDING} == "true",)} (we'll extract the BUILDING value from Jenkins' API next)
    Inside the While Controller:
    • Add another HTTP Request Sampler to fetch the job status: http://your-jenkins-instance/job/Your-Job-Name/${BUILD_NUMBER}/api/json
    • Add a JSON Extractor to pull the building field value from the API response, save it to a variable named BUILDING
    • Add a Fixed Timer set to 10000ms (10 seconds) to avoid spamming Jenkins with too frequent requests
  • Once the While Controller exits (meaning Jenkins task is done), add a JSR223 Sampler to set the global flag:
    props.put("JENKINS_TASK_COMPLETED", "true")
    
    This property is accessible across all thread groups in your test plan.

2. Configure Thread Groups 2 & 3 (Run Steps 1-2 First, Then Wait Dynamically)

These two thread groups follow the same pattern: finish steps 1 and 2 first, then wait until the global flag is set before proceeding to steps 3-100.

  • Add all your Step 1 and Step 2 logic (Samplers, controllers, etc.) first
  • Add a While Controller with the condition: ${__jexl3(props.get("JENKINS_TASK_COMPLETED") != "true",)}
    Inside this While Controller, add a Fixed Timer set to 5000ms (5 seconds) to reduce unnecessary resource usage during the wait
  • After the While Controller, add all your Step 3 to Step 100 test logic

Critical Notes to Avoid Issues

  • Make sure the "Run thread groups consecutively" option in your Test Plan is unchecked—this ensures all three thread groups start at the same time.
  • Add a timeout safeguard to Thread Group 1's polling: Insert a Counter inside the While Controller, and adjust the loop condition to exit if the counter exceeds a threshold (e.g., 30 loops = 5 minutes) to prevent infinite waiting.
  • Always use JSR223 elements instead of BeanShell—Groovy is faster, more efficient, and has better syntax support.
  • If your Jenkins job could fail, extend Thread Group 1's logic to check the result field (e.g., SUCCESS) from the Jenkins API before setting the global flag. This prevents Thread Groups 2 & 3 from running steps 3-100 if the Jenkins task failed.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 07:20:42