面临在JMeter中存储变量结果的问题



我必须使用后处理器将所有匹配的结果存储在一个文件中。下面是响应体。我能够提取所有匹配的结果并将其存储在文件中。

events":[
{
"idWorkflow":3,
"idCase":3754,
"idWorkitem":4059,
"displayName":"Inform RPA to Register SOP in CRM",
"type":"activity",
"date":"05/21/2021 13:38",
"status":"closed",
"assignee":"f3044857-7233-449c-9abe-f44e48616c8e",
"containsMoreAssignees":false
},
{
"idWorkflow":21,
"idCase":3454,
"idWorkitem":3790,
"displayName":"Get & evaluate CDWH Data",
"type":"activity",
"date":"05/21/2021 13:38",
"status":"closed",
"assignee":"f3044857-7233-449c-9abe-f44e48616c8e",
"containsMoreAssignees":false
}`

在上面的响应体中,我需要提取所有";displayName""日期";具有正确顺序的字段值。预期结果:;通知RPA在CRM中注册SOP"-2021年5月21日13:38"获取&评估CDWH数据"-2021年5月21日13:38";下面是我的代码

import java.text.SimpleDateFormat;
import java.io.FileOutputStream;
import java.util.Map;
import java.util.Set;
def resultCount = vars.get("c_date_matchNr")
def activity = vars.get("c_displayName_matchNr")
for (int i = 1; i <= resultCount.toInteger(); i++) {
records = vars.get("c_date_" + i)
for (int j = 1; j <= activity.toInteger(); j++) {
list = vars.get("c_displayName_" + j)
FileWriter fstream = new FileWriter("C:/final.csv", true);
BufferedWriter out = new BufferedWriter(fstream);
out.write(records);
out.write("-");
out.write(list);
out.write(System.getProperty("line.separator"));
out.close();
fstream.close();
}
}

上面的代码存储了结果,但同一行打印了多次。有人能解释一下这段代码中的问题吗?或者在JMeter 中有其他类似的方法吗

使用Debug Sampler和View Results Tree监听器组合查看c_displayNamec_date变量变量的值,可能会出现变量具有重复值的情况。

另一种方法是在Groovy中完全解析响应,而不使用任何其他后处理器:

new groovy.json.JsonSlurper().parse(prev.getResponseData()).events.each { event ->
new File('c:/final.csv') << event.get('displayName') << '-' << event.get('date') << System.getProperty('line.separator')
} 

如果你用一个以上的用户/迭代运行你的测试;活动";,此外,由于竞争条件,数据可能已损坏,因此考虑使用灵活的文件写入程序可能是一个更好的主意

最新更新