在JMeter中使用JSR223/Beanshell前置/后置处理器构建含1000个动态Kit对象的JSON请求体
Converting JavaScript Kit Generation to Beanshell/JSR223 for JMeter
Here's a Beanshell/JSR223 script that replicates your JavaScript logic, generating 1000 kit objects with dynamic kitHandle and kitID values while keeping other fields static as per your example:
import java.util.UUID; import org.json.JSONArray; import org.json.JSONObject; // Initialize the kits array JSONArray kits = new JSONArray(); // Generate 1000 kit objects for (int i = 0; i < 1000; i++) { JSONObject kit = new JSONObject(); // Dynamic unique kitHandle using UUID kit.put("kitHandle", UUID.randomUUID().toString()); // Static fields from your example kit.put("locationHandle", "5e1bbca0-19c0-480a-b8ff-66e676f5f668"); kit.put("containerHandle", "748f217f-6e7f-4382-897b-9c5fee5a58eb"); // Dynamic unique kitID (sequential starting from your example's base ID) kit.put("kitID", "KitID" + (12841790 + i)); kit.put("kitUsageSeq", "585"); kit.put("kitStatus", "QUARANTINE"); kit.put("shipperID", "CONT-585"); // Add the kit to the array kits.put(kit); } // Store the JSON array as a JMeter variable for use in your request vars.put("kitsArray", kits.toString());
How to use this in JMeter:
- Add a JSR223 PreProcessor as a child of your HTTP Request sampler.
- Paste the above script into the "Script" field.
- In your request body, replace the static
kitsarray with${kitsArray}. For example:{ "kits": ${kitsArray} }
Notes:
- kitHandle: Uses
UUID.randomUUID()to generate a unique identifier every time, ensuring no duplicates. - kitID: Uses a sequential number starting from your example's
12841790(so the first kit isKitID12841790, secondKitID12841791, etc.). If you prefer random IDs instead, replace that line with:kit.put("kitID", "KitID" + (int)(Math.random() * 100000000)); - The script uses JMeter's built-in
org.jsonlibrary, which is included by default, so no extra dependencies are needed.
内容的提问来源于stack exchange,提问作者suresh alapati
相关产品推荐
相关产品推荐

