JMeter测试计划结构咨询:50用户登录后并行执行HTTP请求
Alright, let's walk through building a JMeter test plan that perfectly fits your authentication and workflow requirements. I'll break this down into clear, actionable components so you can replicate it easily:
JMeter Test Plan Design for Authentication & Multi-API Initialization Workflow
1. Core Test Plan Setup
Start with the foundational elements to handle your 50-user dataset:
- Add a Thread Group: Set the number of threads to 50 (one per user), adjust ramp-up time based on how quickly you want users to start (e.g., 10 seconds for gradual load), and set loop count to 1 (so each user runs the workflow once).
- Add a CSV Data Set Config (as a child of the Thread Group) to feed user credentials:
- Filename: Path to your CSV file (e.g.,
./user_credentials.csv) - Variable Names: Define names matching your CSV columns, like
username,password - Configure "Recycle on EOF" to
Falseand "Stop thread on EOF" toTrue—this ensures each user is used exactly once.
- Filename: Path to your CSV file (e.g.,
2. Fetch the Session Data Key
First, we need to grab the sessiondataKey from the login page:
- Add an HTTP Request for the login page (usually a
GETrequest):- Enter the login page URL and ensure the method is set to
GET.
- Enter the login page URL and ensure the method is set to
- Add a Regular Expression Extractor (child of this request) to capture the key:
- Reference Name:
sessionDataKey - Regular Expression: Tailor this to how the key appears in the response (e.g.,
<input type="hidden" name="sessiondataKey" value="(.+?)" />for HTML forms) - Template:
$1$ - Match No.:
1(captures the first occurrence of the key)
Pro tip: Use the View Results Tree listener to inspect the login page response and refine your regex if needed.
- Reference Name:
3. Login with Session Key & Credentials
Next, send the login request with the required headers and user data:
- Add an HTTP Request for the login endpoint (typically a
POSTrequest):- Enter the login API URL and set method to
POST.
- Enter the login API URL and set method to
- Add an HTTP Header Manager (child of this request) to include the session key and any required content headers:
- Add a header like
sessiondataKey: ${sessionDataKey}(uses the extracted variable from step 2) - Include content type headers, e.g.,
Content-Type: application/x-www-form-urlencoded(for form data) orapplication/json(for JSON bodies)
- Add a header like
- Configure the request body/parameters:
- For form data: Use parameters like
username=${username}&password=${password} - For JSON: Use a body like
{"username":"${username}","password":"${password}"}
- For form data: Use parameters like
- Add a JSON Extractor (child of the login request) to capture the authorization token:
- Reference Name:
authToken - JSON Path Expression: Match your response structure (e.g.,
$.authorization_tokenor$.token) - Match No.:
1
- Reference Name:
4. Trigger the Initialization Request
After logging in, run the initialization request:
- Add an HTTP Request for the initialization endpoint:
- Set the correct method and URL.
- Ensure your HTTP Header Manager (can reuse a group-level one or add a request-specific one) includes the authorization token:
- Add
Authorization: Bearer ${authToken}(adjust the prefix if your token uses a different scheme likeBasic).
- Add
5. Simulate Concurrent XHR API Requests
Since the site fires multiple API calls post-initialization, use a Parallel Controller to mimic this behavior:
- First, install the Parallel Controller via the JMeter Plugins Manager (it’s essential for simulating concurrent requests).
- Add the Parallel Controller as a child element right after the initialization request.
- Inside the Parallel Controller, add individual HTTP Request elements for each XHR API endpoint:
- For each request, attach an HTTP Header Manager (or use a shared one) that includes
Authorization: Bearer ${authToken} - Configure each request’s method, URL, and parameters to match the actual API specs.
- For each request, attach an HTTP Header Manager (or use a shared one) that includes
6. Optional: Add Listeners for Result Analysis
Include these listeners to track test performance and debug issues:
- View Results Tree: Great for debugging (disable this in production test runs to save resources)
- Summary Report: Provides aggregated metrics like response times, error rates, and throughput
- Response Times Graph: Visualizes response time trends over the test run
Quick Best Practices
- Scope Matters: Ensure extractors are children of the request they’re extracting from, and Header Managers are scoped to apply to all relevant requests (Thread Group level for global headers).
- Validate Variables: Use the Debug Sampler to check if
sessionDataKeyandauthTokenare being captured correctly before running full load tests. - Handle Dynamic Content: If there are other dynamic values (like CSRF tokens), add additional extractors as needed.
内容的提问来源于stack exchange,提问作者Nirmal Dholakiya
相关产品推荐
相关产品推荐

