Shivakumar ss
Shivakumar ss

Reputation: 663

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

Here is what i have done till now.

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 script present in pre-processor

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

enter image description here enter image description here enter image description here

Upvotes: 4

Views: 5930

Answers (1)

Masud Jahan
Masud Jahan

Reputation: 2978

According to documentation

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

Check the 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]

This is the reason your script isn't working for the forEach controller.

Try using JSR223 Sampler instead of JSR223 pre-processor. You can ignore the sample result also.

Upvotes: 1

Related Questions