JMeter场景构建:100个用户同时上传唯一文件需求
Alright, let's walk through how to implement this test scenario effectively. It involves three key parts: batch user creation, concurrent uploads, and ensuring unique file assignment per user. Here's a detailed breakdown:
The success of this test hinges on three non-negotiable requirements: 100 unique test users, simultaneous file upload triggers, and one-to-one mapping between users and files from your Excel list. Let's tackle each part step by step.
1. Batch Create 100 Test Users
Whether you're using an automation tool or custom code, the goal is to generate 100 valid, authenticated users quickly:
- Automation Tools (e.g., JMeter):
- Set up a user registration HTTP request, then wrap it in a loop controller set to run 100 times. Use a dynamic variable like
user_${__counter(1,)}to generate unique usernames (e.g.,test_user_001totest_user_100). - Store all user credentials (username, password, auth token) in a variable group or temporary CSV for later use.
- Set up a user registration HTTP request, then wrap it in a loop controller set to run 100 times. Use a dynamic variable like
- Custom Code (e.g., Python):
- Write a loop to call your user registration API 100 times, generating sequential user IDs. Example snippet:
import requests for i in range(1, 101): user_data = { "username": f"test_user_{i:03d}", "password": "secure_test_pass123", "email": f"test_{i}@example.com" } response = requests.post("https://your-api.com/register", json=user_data) # Save auth token or credentials to a list
- Write a loop to call your user registration API 100 times, generating sequential user IDs. Example snippet:
2. Read & Validate the 100 Unique Filenames from Excel
First, you need to pull the filenames into a usable format and confirm they're unique:
- Automation Tools:
- For JMeter, convert your Excel to a CSV first, then use the
CSV Data Set Configcomponent to load the filenames. Enable "Sequential" sharing mode to ensure each thread gets a unique entry. - For LoadRunner, use built-in Excel reading functions (like
lr_paramarr_idx) to extract filenames into a parameter array.
- For JMeter, convert your Excel to a CSV first, then use the
- Custom Code:
- Use libraries like
openpyxlorpandasto read the Excel column, then validate the count and uniqueness:import openpyxl wb = openpyxl.load_workbook("file_list.xlsx") sheet = wb.active file_names = [cell.value for cell in sheet["A"] if cell.value is not None] # Critical validation steps assert len(file_names) == 100, "Excel must contain exactly 100 filenames" assert len(set(file_names)) == 100, "Excel has duplicate filenames—fix this first!"
- Use libraries like
3. Execute Concurrent Uploads with Unique File Mapping
The trick here is to tie each user to exactly one unique file, then trigger all uploads at the same time:
Option 1: Using JMeter
- Create a thread group with 100 threads and a Ramp-Up Period of
0(this ensures all users start uploading simultaneously). - Add a login request to each thread, using the pre-stored user credentials.
- Link the CSV Data Set Config (with your filenames) to the upload request, so each thread picks the next sequential file.
- Ensure the upload request references the correct local file path (e.g.,
${__P(file_path)}${filename}).
Option 2: Using Python Threading
- Pair each user credential with a unique filename, then use a thread pool to fire off all uploads at once:
import threading import requests def upload_for_user(user_cred, file_name): # Step 1: Log in to get auth token login_response = requests.post( "https://your-api.com/login", json={"username": user_cred["username"], "password": user_cred["password"]} ) auth_token = login_response.json()["token"] # Step 2: Upload the file file_path = f"./test_files/{file_name}" with open(file_path, "rb") as file: upload_response = requests.post( "https://your-api.com/upload", headers={"Authorization": f"Bearer {auth_token}"}, files={"file": file} ) print(f"User {user_cred['username']} uploaded {file_name}: Status {upload_response.status_code}") # Assume user_creds is your list of 100 user credentials # Assume file_names is your validated list from Excel threads = [] for cred, fname in zip(user_creds, file_names): thread = threading.Thread(target=upload_for_user, args=(cred, fname)) threads.append(thread) thread.start() # Wait for all uploads to complete for thread in threads: thread.join()
4. Post-Test Validation & Monitoring
- Verify that all 100 files were uploaded successfully and are linked to the correct user (check your backend logs or database).
- Monitor system performance metrics: track response times, server CPU/memory usage, and throughput to ensure your system handles the concurrent load.
内容的提问来源于stack exchange,提问作者Limbo

