Jmeter JSR223 Groovy 逐行读取文件并执行 http POST
Jmeter JSR223 Groovy read file line by line and do http POST
我正在尝试迭代文件夹中的文件。每个文件都有多个由换行符分隔的 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 正文中为动态变量提供变量名称。
A Pre-Processor executes some action prior to a Sampler Request being made.
Please note that Timers, Assertions, Pre- and Post-Processors are only processed if there is a sampler to which they apply. Logic Controllers and Samplers are processed in the order in which they appear in the tree. Other test elements are processed according to the scope in which they are found, and the type of test element. [Within a type, elements are processed in the order in which they appear in the tree]
这就是您的脚本不适用于 forEach 控制器的原因。
尝试使用 JSR223 Sampler 而不是 JSR223 预处理器。您也可以忽略示例结果。
我正在尝试迭代文件夹中的文件。每个文件都有多个由换行符分隔的 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 正文中为动态变量提供变量名称。
A Pre-Processor executes some action prior to a Sampler Request being made.
Please note that Timers, Assertions, Pre- and Post-Processors are only processed if there is a sampler to which they apply. Logic Controllers and Samplers are processed in the order in which they appear in the tree. Other test elements are processed according to the scope in which they are found, and the type of test element. [Within a type, elements are processed in the order in which they appear in the tree]
这就是您的脚本不适用于 forEach 控制器的原因。
尝试使用 JSR223 Sampler 而不是 JSR223 预处理器。您也可以忽略示例结果。