JMeter实现WebSocket压测:循环生成消息并添加至数组的方法
Got it, let's walk through how to set this up step by step—no prior Java/Beanshell experience required! We'll use a CSV Data Set Config to pull your message content, and a Beanshell PreProcessor to dynamically add new entries to the body array each time we send a request.
Step 1: Prepare Your CSV File
First, make sure your CSV is formatted correctly:
- Create a file (e.g.,
messages.csv) with a single column headerMESSAGE - Each row below the header should contain one message content (the value you want for the
bodyfield in your new entry)
Example CSV content:
MESSAGE new message again another test message third message to send
Step 2: Add CSV Data Set Config to Your Thread Group
- Right-click your Thread Group > Add > Config Element > CSV Data Set Config
- Configure it like this:
- Filename: Path to your
messages.csvfile - Variable Names:
MESSAGE(matches the header in your CSV) - Recycle on EOF: Check this if you want to loop through the CSV entries when you reach the end (good for long load tests)
- Stop thread on EOF: Uncheck this if you're recycling
- Delimiter:
,(default, since it's a CSV)
- Filename: Path to your
Step 3: Add Beanshell PreProcessor to Your WebSocket Request
- Right-click your WebSocket Request > Add > Pre Processors > Beanshell PreProcessor
- Paste the following script into the "Script" field. I've added comments to explain each part:
// 1. Start with your base message template (matches the format you provided) String baseMessage = "42[\"tickets\",\"ticket_message\", {\"ticket\":{ \"body\":[{\"received\":\"2018-05-17T11:28:51.000Z\",\"fromName\":\"Jesper Pedersen\",\"body\":\"test\\n\"}, {\"received\":\"2018-05-17T13:30:53+02:00\",\"body\":\"Yo\",\"fromName\":\"Futurum Digital\"} ] }}]"; // 2. Remove the Socket.IO prefix "42" to get the raw JSON array String jsonContent = baseMessage.substring(2); // 3. Parse the JSON array using JMeter's built-in JSON library org.json.JSONArray mainArray = new org.json.JSONArray(jsonContent); // 4. Navigate to the ticket's body array org.json.JSONObject ticketWrapper = mainArray.getJSONObject(2); org.json.JSONObject ticket = ticketWrapper.getJSONObject("ticket"); org.json.JSONArray bodyArray = ticket.getJSONArray("body"); // 5. Create your new message entry org.json.JSONObject newEntry = new org.json.JSONObject(); // Generate current UTC timestamp in the format from your example newEntry.put("received", new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").format(new java.util.Date())); // Pull the message content from your CSV variable newEntry.put("body", vars.get("MESSAGE")); newEntry.put("fromName", "Futurum Digital"); // 6. Add the new entry to the body array bodyArray.put(newEntry); // 7. Reassemble the full Socket.IO message String finalMessage = "42" + mainArray.toString(); // 8. Store the final message in a JMeter variable to use in your request vars.put("DYNAMIC_WEBSOCKET_MESSAGE", finalMessage);
Step 4: Configure Your WebSocket Request
In your WebSocket Request's Message Data field, replace any static content with the variable we created:
${DYNAMIC_WEBSOCKET_MESSAGE}
Optional Adjustment: Start with an Empty Body Array
If you don't want to keep the initial two entries in the body array and want to start fresh each time, add this line right after getting the bodyArray:
// Clear existing entries to start with an empty array bodyArray = new org.json.JSONArray();
That's it! Now each time your thread runs, it will pull a new message from the CSV, add it to the body array, and send the updated WebSocket message.
内容的提问来源于stack exchange,提问作者Jesper

