JMeter技术问询:如何实现多用户指定时间请求第二个端点?
I’ve run into this exact scenario dozens of times—simulating staggered logins followed by a coordinated burst of requests is tricky with JMeter’s default scheduler, but there are three reliable ways to make it work:
Option 1: Two Thread Groups with Session Sharing + Timed Trigger
This is the most straightforward approach for separating the login phase and synchronized request phase:
First Thread Group (Login Phase)
- Set
Number of Threads (users)to your total user count (e.g., 5000) - Set
Ramp-Up Periodto 300 seconds (5 minutes) — this spreads out login requests randomly over the 5-minute window - Add your login HTTP Request, plus a Cookie Manager to preserve session tokens
- Enable
Run Thread Groups Consecutivelyin the Test Plan settings (so this group finishes before the next starts)
- Set
Second Thread Group (Synchronized Request Phase)
- Set
Number of Threadsto match the first group (same user count) - Use JMeter’s Scheduler to set a specific start time (the exact moment you want all users to hit the second endpoint)
- Add the HTTP Request for your target resource — the Cookie Manager automatically reuses session tokens from the first group (JMeter shares cookie managers across thread groups by default)
- Set
Option 2: Single Thread Group with JSR223 Wait Logic
If you prefer keeping everything in one thread group (to avoid session sharing edge cases), use a Groovy script to pause each thread until your target time:
Configure your thread group with:
Number of Threads: Your total user countRamp-Up Period: 300 seconds (to stagger logins)
Add your login HTTP Request first.
Insert a JSR223 Sampler right after the login request, with this Groovy code (replace the target time with your desired timestamp):
// Define your target trigger time (format: yyyy-MM-dd HH:mm:ss) def targetTriggerTime = Date.parse("yyyy-MM-dd HH:mm:ss", "2024-05-20 15:00:00") def currentTime = new Date() // Wait until we hit the target time while (currentTime.before(targetTriggerTime)) { sleep(1000) // Check every second to avoid excessive CPU usage currentTime = new Date() }Add your second endpoint HTTP Request after the JSR223 Sampler.
Each thread will log in at a random time over 5 minutes, wait until the target time, then fire the resource request simultaneously.
Option 3: Synchronizing Timer for Burst Control (Alternative)
If you don’t need an exact wall-clock time, just want all logged-in users to trigger the request at the same relative point, use a Synchronizing Timer:
- Add the timer right after your login request
- Set
Number of Simultaneous Users to Group Byto your total user count - This pauses every thread after login until all threads have completed the login step, then they’ll all fire the next request at once
Note: This works best if your login phase completes quickly relative to the ramp-up — if logins take time, some threads might finish early and wait longer than others.
内容的提问来源于stack exchange,提问作者stuart

