You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在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:

  1. Add a JSR223 PreProcessor as a child of your HTTP Request sampler.
  2. Paste the above script into the "Script" field.
  3. In your request body, replace the static kits array 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 is KitID12841790, second KitID12841791, 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.json library, which is included by default, so no extra dependencies are needed.

内容的提问来源于stack exchange,提问作者suresh alapati

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 21:49:07