为什么我的 JSON 值在 Jmeter 中重复?
Why my JSON values are getting duplicated in Jmeter?
这是我在 JMeter 中编写的 BeanShell 脚本
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
JSONObject outerObject = new JSONObject();
JSONArray jsonArray = new JSONArray();
for(int i = 0;i<=3;i++)
{
JSONObject innerObject = new JSONObject();
innerObject.put("middleName", "${__RandomString(4,KLMNOPRSTUVWXYZ,)}");
innerObject.put("maidenName", "${__RandomString(4,HUDNSERTFG,)}");
innerObject.put("gender", "${__RandomString(1,MFU,)}");
innerObject.put("TestingType", "${__RandomString(1,IE,)}");
jsonArray.put(innerObject);
outerObject.put("nameList", jsonArray);
}
outerObject.put("Alert", "Testing Alert");
log.info(outerObject.toString());
vars.putObject("jsonData",jsonArray);
这是我收到的 JSON 回复
{
"nameList": [
{
"gender": "M",
"maidenName": "DUDT",
"middleName": "ZPMZ",
"TestingType": "E"
},
{
"gender": "M",
"maidenName": "DUDT",
"middleName": "ZPMZ",
"TestingType": "E"
},
{
"gender": "M",
"maidenName": "DUDT",
"middleName": "ZPMZ",
"TestingType": "E"
},
{
"gender": "M",
"maidenName": "DUDT",
"middleName": "ZPMZ",
"TestingType": "E"
}
],
"Alert": "Testing Alert"
}
如上图 JSON 所示,所有值都是重复的。我想为所有变量设置不同的值。我错过了什么吗?如果我的代码有任何问题,请指导我。谢谢
我建议将 JSR223 + Groovy 用于 performances with below code using RandomStringGenerator:
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.apache.commons.text.RandomStringGenerator;
JSONObject outerObject = new JSONObject();
JSONArray jsonArray = new JSONArray();
RandomStringGenerator generator = new RandomStringGenerator.Builder().selectFrom("KLMNOPRSTUVWXYZ".toCharArray()).build();
RandomStringGenerator generatorGender = new RandomStringGenerator.Builder().selectFrom("MFU".toCharArray()).build();
RandomStringGenerator generatorType = new RandomStringGenerator.Builder().selectFrom("IE".toCharArray()).build();
for(int i = 0;i<=3;i++)
{
JSONObject innerObject = new JSONObject();
innerObject.put("middleName", generator.generate(4));
innerObject.put("maidenName", generator.generate(4));
innerObject.put("gender", generatorGender.generate(1));
innerObject.put("TestingType", generatorType.generate(1));
jsonArray.put(innerObject);
outerObject.put("nameList", jsonArray);
}
outerObject.put("Alert", "Testing Alert");
log.info(outerObject.toString());
vars.putObject("jsonData",jsonArray);
这是我在 JMeter 中编写的 BeanShell 脚本
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
JSONObject outerObject = new JSONObject();
JSONArray jsonArray = new JSONArray();
for(int i = 0;i<=3;i++)
{
JSONObject innerObject = new JSONObject();
innerObject.put("middleName", "${__RandomString(4,KLMNOPRSTUVWXYZ,)}");
innerObject.put("maidenName", "${__RandomString(4,HUDNSERTFG,)}");
innerObject.put("gender", "${__RandomString(1,MFU,)}");
innerObject.put("TestingType", "${__RandomString(1,IE,)}");
jsonArray.put(innerObject);
outerObject.put("nameList", jsonArray);
}
outerObject.put("Alert", "Testing Alert");
log.info(outerObject.toString());
vars.putObject("jsonData",jsonArray);
这是我收到的 JSON 回复
{
"nameList": [
{
"gender": "M",
"maidenName": "DUDT",
"middleName": "ZPMZ",
"TestingType": "E"
},
{
"gender": "M",
"maidenName": "DUDT",
"middleName": "ZPMZ",
"TestingType": "E"
},
{
"gender": "M",
"maidenName": "DUDT",
"middleName": "ZPMZ",
"TestingType": "E"
},
{
"gender": "M",
"maidenName": "DUDT",
"middleName": "ZPMZ",
"TestingType": "E"
}
],
"Alert": "Testing Alert"
}
如上图 JSON 所示,所有值都是重复的。我想为所有变量设置不同的值。我错过了什么吗?如果我的代码有任何问题,请指导我。谢谢
我建议将 JSR223 + Groovy 用于 performances with below code using RandomStringGenerator:
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.apache.commons.text.RandomStringGenerator;
JSONObject outerObject = new JSONObject();
JSONArray jsonArray = new JSONArray();
RandomStringGenerator generator = new RandomStringGenerator.Builder().selectFrom("KLMNOPRSTUVWXYZ".toCharArray()).build();
RandomStringGenerator generatorGender = new RandomStringGenerator.Builder().selectFrom("MFU".toCharArray()).build();
RandomStringGenerator generatorType = new RandomStringGenerator.Builder().selectFrom("IE".toCharArray()).build();
for(int i = 0;i<=3;i++)
{
JSONObject innerObject = new JSONObject();
innerObject.put("middleName", generator.generate(4));
innerObject.put("maidenName", generator.generate(4));
innerObject.put("gender", generatorGender.generate(1));
innerObject.put("TestingType", generatorType.generate(1));
jsonArray.put(innerObject);
outerObject.put("nameList", jsonArray);
}
outerObject.put("Alert", "Testing Alert");
log.info(outerObject.toString());
vars.putObject("jsonData",jsonArray);