我正在尝试迭代文件夹中的文件。每个文件有多个以换行符分隔的JSON字符串。一旦检索到JSON,就必须获取JSON的特定节点并将其POST到http服务器。
最初我想使用csv数据集配置,但我能够从文件中获取嵌套的json。在阅读了一些JMeter教程之后,我最终决定使用JSR223来编写自定义脚本,该脚本读取文件并将其放入ctx中,采样器将使用该数据发送数据。
这是我目前所做的。
Test plan
-> Thread group
-> JSR223 PreProcessor : This is where i am reading file and adding it to vars, its like "json_{number}" and "GETfileLength"
-> ForEach Controller : This is sibling of Thread group
-> HTTP Request : Inside for Each controller has a configuration of host, port and the path and in the body i have mentioned ${json_out}
-> View Results Tree
-> Summary Report
在前置处理器中存在的Groovy脚本
log.info("------ start ----------");
File file = new File("/data/sample1.json")
def line;
def noOfLines=0
file.withReader { reader ->
while ((line = reader.readLine()) != null) {
noOfLines++
vars.put("json_"+noOfLines, line)
}
}
vars.put("GETfileLength",noOfLines.toString()) ;
log.info("------ end ----------");
- 在上述测试计划的层次结构中,我没有看到脚本被调用(已检查日志)。如果我删除forEach控制器,脚本会被调用,但我不知道如何在动态变量的http POST主体中给出变量名称。