JMeter负载测试需求:先登录所有用户再执行后续步骤
How to Implement "All Users Log In First, Then Execute Tasks Uniformly" in JMeter
Got it, I totally get why you want to adjust your script—this scenario is way closer to real user behavior where people stay logged in and perform actions together instead of the one-and-done login-task-logout flow. Let's walk through exactly how to tweak your JMeter setup to make this happen:
Step 1: Configure Test Plan to Run Thread Groups Sequentially
First, we need to make sure JMeter runs our thread groups one after another instead of in parallel. Here's how:
- Open your Test Plan settings (click the root Test Plan element)
- Scroll to the bottom and check the box for Run Thread Groups Consecutively
- This ensures all users finish logging in before any tasks start, and all tasks finish before logout begins.
Step 2: Create a "Login Only" Thread Group
This thread group will handle all user logins and store their session data for later use:
- Duplicate your existing thread group, rename it to something like
Login - All Users - Remove the "Execute Task" and "Logout CAS" steps—we only want login logic here
- Keep your CSV Data Set Config (make sure its Sharing Mode is set to
All threadsso each thread gets a unique user from the CSV) - After the "Login CAS" step, add a JSR223 PreProcessor (better performance than BeanShell) to save each user's session to a global JMeter property. For example, if your session is stored in a cookie named
JSESSIONID, use this code:
Note: Replace// Save current thread's session to a global property named session_<threadNumber> props.put("session_" + ctx.getThreadNum(), vars.get("COOKIE_JSESSIONID"));COOKIE_JSESSIONIDwith whatever variable holds your CAS session token if it's not a cookie. - Set this thread group's Number of Threads to your total number of test users, and Loop Count to 1 (each user logs in once)
Step 3: Create a "Execute Tasks" Thread Group
This thread group will use the saved sessions to run tasks uniformly:
- Create a new thread group named
Execute Tasks - All Users - Set its Number of Threads to match the login thread group (same number of users)
- Add a JSR223 PreProcessor at the start of this thread group to retrieve the saved session for the current thread:
// Fetch the global session property for this thread and store it in a local variable String sessionId = props.get("session_" + ctx.getThreadNum()); vars.put("ACTIVE_SESSION_ID", sessionId); - Update your "Execute Task" steps to use this session:
- If using a Cookie Manager, add a cookie entry with Name
JSESSIONID, Value${ACTIVE_SESSION_ID}, and the appropriate Domain/Path - If your task requests use a session token in headers/parameters, replace the token value with
${ACTIVE_SESSION_ID}
- If using a Cookie Manager, add a cookie entry with Name
- Add your original "Execute Task" logic here (exactly as it was before)
Step 4: Create a "Logout All Users" Thread Group
Finally, add a thread group to handle logout for all users:
- Create a new thread group named
Logout - All Users - Match its Number of Threads to the previous groups
- Add the same JSR223 PreProcessor from the "Execute Tasks" group to retrieve the active session
- Add your original "Logout CAS" step, updated to use
${ACTIVE_SESSION_ID}for the session - Set Loop Count to 1
Key Notes to Avoid Issues
- Session Expiry: Make sure your CAS session timeout is long enough to cover the time it takes for all users to log in + execute tasks. If sessions expire mid-test, you'll get failed requests.
- Variable Names: Double-check that you're using the correct variable names for your session tokens (cookies, CAS tickets, etc.)—mismatches here will break the flow.
- CSV Sharing: Ensure your CSV Data Set Config is shared across all thread groups if you need to reference user-specific data in tasks/logout (set Sharing Mode to
All threads).
内容的提问来源于stack exchange,提问作者Stirling Crow
相关产品推荐
相关产品推荐

