1
我有一個爲Amazon Kinesis構建Json的問題。 此JSON必須具有以下格式:在Jmeter中使用JSONObject和JSONArray
{
"Records": [
{
"Data": "XzxkYXRhPl8x",
"PartitionKey": "partitionKey1"
},
{
"Data": "f1PxFQo92Afh",
"PartitionKey": "partitionKey2"
},
{
"Data": "Gi4sEdd08HypA",
"PartitionKey": "partitionKey3"
}
],
"StreamName": "exampleStreamName"
}
我使用的BeanShell採樣以創建JSON作爲緩衝:
import org.json.JSONArray;
import org.json.JSONObject;
//Dichiarazione variabili
int timestampValue=(${startTime}+${i}+1);
float current_powerValue=${current_power_1}+${__Random(0,10)};
String idValue=${__threadNum}+"_"+"5";
JSONObject part = new JSONObject();
//Create JSON
part.put("timestamp",timestampValue);
part.put("parent","${__threadNum}");
part.put("id",idValue);
part.put("state","on");
part.put("today_kwh",65);
part.put("current_power",current_powerValue);
part.put("today_on_time",0);
part.put("on_for",0);
part.put("today_standby_time",0);
//ADD json to array
if(${i}%(${bufferSize}*${sample}-1)==0 && ${i}!=0 || ${i}==${totalNumber}-${endOfDb}){
//Add to json variable the last json created
vars.put("json",vars.get("json")+part.toString());
//Make an JSONObject by json variable of jmeter
JSONObject tempArray= new JSONObject(vars.get("json"));
log.info(tempArray.toString());
//Add tempArray into JSONArray so that it adds square brackets
JSONArray records= new JSONArray();
records.put(tempArray);
//Add the second field streamName
JSONObject kinesis = new JSONObject();
kinesis.put("records",records);
kinesis.put("streamName","kinesis");
//save into jsonBuffer
vars.put("jsonBuffer",kinesis.toString());
//restart json variable
vars.put("json","");
}
else{
//add new json into variable so to store it.
vars.put("json", vars.get("json")+part.toString()+",");
}
我使用JSON變量在JMeter的保存爲每個iteraction的JSON和當「i」變量尊重if子句時,我開始創建json結構。 因此,我添加最後一個json到jmeter變量,然後創建一個JSONObject來存儲這個json,但是當我這樣做時它只存儲一個json(因爲它是一個對象)。 不幸的是,如果我存儲在JSONArray中,它會添加「」,因爲將變量json讀作字符串。 最好的解決辦法是隻使用的JSONObject和JSONArray,但我如何使用相同的對象爲所有iteractions(在JMeter的我無法使用JSONArray),其中 這是我的JMX