Jmeter JSR223 Groovy 逐行读取文件并执行 http POST

Jmeter JSR223 Groovy read file line by line and do http POST

这是我到目前为止所做的。

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 ----------");

根据documentation

A Pre-Processor executes some action prior to a Sampler Request being made.

勾选Execution order

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 预处理器。您也可以忽略示例结果。