问题
I'm looking for way how to get random row from file in JMeter. I would be appreciate for any suggestions.
回答1:
Not sure regarding groovy, maybe there is an easier way, but for instance you can do it with Beanshell Sampler using the following code:
import org.apache.commons.io.FileUtils; //necessary import
List lines = FileUtils.readLines(new File("/path/to/your/file")); // read file into lines array
int random = new Random().nextInt(lines.size()); // get random line number
String randomLine = lines.get(random); // get random line
vars.put("randomLine", randomLine); // store the random line into ${randomLine} variable
- substitute
/path/to/your/file
with relative or absolute path to the file you want the random line from - you will be able to access the random line as
${randomLine}
where required
See How to Use BeanShell: JMeter's Favorite Built-in Component guide for more information on using JMeter and Java APIs from Beanshell test elements in your JMeter test
N.B. The above code is valid groovy code as well
回答2:
This should be possible using a bean shell controller. Here you will need to do bit of programming to achieve the desired behaviour.
There is an easy way to achieve the desired outcome. Introduce a ransom timer into your thread group. The timer will ensure the randomness of the threads. Configure a CSV confit element to read data from a file. Ensure the file is shared with all the threads.
回答3:
you can get a random var
random__Random(1,9)
//syntax: __Random(start,end)
Then you can pull out
File fileInstance = new File("path/yourfile.txt");
readerInstance = new IndexedFileReader(fileInstance);
lines = readerInstance.readLines(randomIdx, randomIdx+1);
readLines takes a start and an end line number..
However if you are using anything other then very small files, with low iterations I really suggest you read the entire file into a buffer, and use the random var to pull lines from that
回答4:
You can also use the Random CSV Data Set Config Plugin Jmeter plugin by Blazemeter. It has all the randomising a regualr user will want on top of the conventional JMeters CSV Data Set Config.
来源:https://stackoverflow.com/questions/35309234/how-to-get-random-row-from-file-in-jmeter