How to run groovy script in Blazemeter, which try to get access to csv file data?

90 views Asked by At

I have some Jmeter script on groovy

1.upto(${amount_of_items}, { index ->
def data = new File('data/ids.csv').readLines().get(index).split(',') 
def attr = [:]
attr.put('id', data[0].toInteger()) 
def attributes = [:]
def param = [:]
param.put('quantity', ${quantity})
param.put('status_id', 4)
attributes.put('statuses', [param])
attributes.put('identity', attr)
adjustItems.add(attributes)
})

Is there any solution to get access to my csv which I try to parse from Blazemeter?

def data = new File('data/ids.csv').readLines().get(index).split(',') 

Could I upload my csv to Shared Folder in Blazemeter and use it in script?

1

There are 1 answers

1
Dmitri T On

There shouldn't be a problem to access your file in BlazeMeter given you upload it to the shared folder. See Shared Folders article for more details.

Also according to JSR223 Sampler documentation:

The JSR223 test elements have a feature (compilation) that can significantly increase performance. To benefit from this feature:

Use Script files instead of inlining them. This will make JMeter compile them if this feature is available on ScriptEngine and cache them. Or Use Script Text and check Cache compiled script if available property.

When using this feature, ensure your script code does not use JMeter variables or JMeter function calls directly in script code as caching would only cache first replacement. Instead use script parameters.

So I would recommend amending your code to use vars shorthand for JMeterVariables class instance like:

1.upto(vars.get('amount_of_items') as int, { index ->
    def data = new File('data/ids.csv').readLines().get(index).split(',')
    def attr = [:]
    attr.put('id', data[0].toInteger())
    def attributes = [:]
    def param = [:]
    param.put('quantity', vars.get('quantity') as int)
    param.put('status_id', 4)
    attributes.put('statuses', [param])
    attributes.put('identity', attr)
    adjustItems.add(attributes)
})