Search code examples
jmeterjmx

Jmeter loop through JSR223 PreProcessor


I have a post request that is taking input from a JSR223 PreProcessor. This is so i can include 50 rows from csv per request. I would now like to iterate over the csv so i can request all rows in the csv, 50 at a time per request. The PreProcessor code below works great to take the first 50 records;

def rows = 50 // how many rows you need to collect from the CSV

def content = [:]
def body = []

1.upto(rows, { index ->
    def values = new File('locations.csv').readLines().get(index).split(',')
    def entry = [:]
    entry.put("sAddress", values[0])
    entry.put("sPostcode", values[1])
    entry.put("sID", values[2])
    body.add(entry)
})
content.put('data', body)
sampler.addNonEncodedArgument('', new groovy.json.JsonBuilder(content).toPrettyString(),'')
sampler.setPostBodyRaw(true)

This works amazingly well. However, i would like to iterate through this taking the next 50 records in the next thread group loop and for it to complete say 100 loops and therefore 5000 rows of csv will have been sent. Is there a way of doing this?


Solution

    1. You can get current iteration number as vars.getIteration()
    2. Assuming above you can use some calculated offset instead of 1 hard-coded value

    Something like:

    def rows = 50
    
    def start = vars.getIteration() - 1
    
    def offset = rows * vars.getIteration() - 1
    
    (start * rows).upto(offset, {index->
       //do what you need here
    })
    

    In the above example vars stands for JMeterVariables class instance, see Top 8 JMeter Java Classes You Should Be Using with Groovy article for more information on this and other JMeter API shorthands which are available for the JSR223 Test Elements