Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store array values in Jmeter variables?

Tags:

java

jmeter

I have a CSV file containing data which I read using a Bean Shell script and populate an ArrayList based upon it.Below is the code for it.

//Populate Beanshell script
import java.text.*;
import java.io.*;
import java.util.*;

ArrayList strList = new ArrayList();    

try {
File file = new File("path/to/csv");

if (!file.exists()) {
    throw new Exception ("ERROR: file not found");
}

BufferedReader bufRdr = new BufferedReader(new FileReader(file));
String line = null;

while((line = bufRdr.readLine()) != null) {
    strList.add(line);
}

bufRdr.close();   
}
catch (Exception ex) {
IsSuccess = false; 
log.error(ex.getMessage());
System.err.println(ex.getMessage());
}
catch (Throwable thex) {
System.err.println(thex.getMessage());
}

Now I want to utilize this data in a random manner so I am trying to use something like this

//Consumer bean shell script
//Not able to access strList since vars.put cannot store an object
Random rnd = new java.util.Random(); 
vars.put("TheValue",strList.get(rnd.nextInt(strList.size())));

But I am unable to do this because in vars.put I cannot store an array or a list,I can only store only primitive types.So there is no way in which I can access the populate function's ArrayList from an another BeanShell script.

How do I achieve randomization in this scenario since calling populate function each and every time is not good from a performance point of view.

like image 880
Madusudanan Avatar asked Nov 27 '25 21:11

Madusudanan


1 Answers

vars.put only supports String values. There is vars.putObject:

Scripts can also access JMeter variables using the get() and put() methods of the "vars" variable, for example: vars.get("HOST"); vars.put("MSG","Successful"); . The get() and put() methods only support variables with String values, but there are also getObject() and putObject() methods which can be used for arbitrary objects. JMeter variables are local to a thread, but can be used by all test elements (not just Beanshell).

like image 97
PeterMmm Avatar answered Nov 29 '25 12:11

PeterMmm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!