JMeter中如何让线程组2、3在指定步骤等待线程组1完成外部任务?
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(usebuildWithParametersinstead if you need to pass job arguments) - Method: POST
- Pro Tip: If your Jenkins requires authentication, add an HTTP Header Manager with an
Authorizationheader (format:Basic [base64-encoded username:password])
- Request URL:
- Add a Regular Expression Extractor to pull the Build Number from Jenkins' response header (look for the
Locationfield, which ends with the build ID) - Add a While Controller with the condition:
${__jexl3(${BUILDING} == "true",)}(we'll extract theBUILDINGvalue 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
buildingfield value from the API response, save it to a variable namedBUILDING - Add a Fixed Timer set to 10000ms (10 seconds) to avoid spamming Jenkins with too frequent requests
- Add another HTTP Request Sampler to fetch the job status:
- Once the While Controller exits (meaning Jenkins task is done), add a JSR223 Sampler to set the global flag:
This property is accessible across all thread groups in your test plan.props.put("JENKINS_TASK_COMPLETED", "true")
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
resultfield (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

