3

I am fairly new to jmeter and have been looking at creating a test plan which includes posting personal information (Dummy Data) to a website. I have a CSV File that contains around 20,000 records that I need to load into jmeter and then randomize the order of the information that jmeter will use for each iteration. Currently I am using a Beanshell Sampler to load the CSV and Randomise the lines and then a Beanshell Post Processor to load the randomised line into a variable called "Line" however after running the script and looking in my debug results it is showing parts of the data in the line/row of personal information. Investigating a little further I have realised that the Varible "Line" is being loaded with information that is being broken by due to having spaces in the string that is between the commas in the CSV file.

Example data in the CSV file as follows:

firstname,lastname,housenumber,streetname,area,postcode
john,smith,21,Albert Street,Knotts County,AB3 4DL

The code that i am using in beanshell is part1 loading the csv file :

Scanner scanner = new Scanner(new File("c:/file.csv")); 
Map file = new HashMap(); 
int counter = 0; 
while (scanner.hasNext()) { 
  String line = scanner.next(); 
  file.put(counter, line); 
  counter++; 
} 
bsh.shared.fileMap = file; 
bsh.shared.linesnumber = counter;

Second Part of beanshell script in a Post Processor gets random row from file and adds to variable "line":

int counter = bsh.shared.linesnumber; 
Map file = bsh.shared.fileMap; 

Random r = new Random(); 
String line = file.get(r.nextInt(counter)); 
vars.put("line", line);

When running this the results in the debug postprocessor show as below:

line=john,smith,21,Albert

It seems that the space between the words "Albert and Street" are breaking the line. So any string value between the comma's that includes a space breaks the line eg. streetname, postcode etc. My QUESTION is, is there a way i can load the full line in to the variable including the spaces.

Naveen Kumar R B
  • 6,248
  • 5
  • 32
  • 65

2 Answers2

3

First of all, I believe that test need to be repeatable so in case the test reveals the error you could re-run the test to confirm it or verify that the underlying issue is fixed. So I would recommend keeping "normal" order of CSV data. Well-behaved tests clean up after themselves so you should not run into situation where data cannot be reused.

If you are still looking for a way to randomise the data, you can try HTTP Simple Table Server plugin which has "RANDOM" mode of returning the line.

And if after all you still want Beanshell, remember that JMeter is built on the top of other libraries and you can call their methods from Beanshell scripts. Particularly in your case I believe FileUtils.readLines() will be very helpful, like:

  1. First Beanshell script:

    import org.apache.commons.io.FileUtils;
    
    List lines = FileUtils.readLines(new File("c:/file.csv"));
    bsh.shared.lines = lines;
    
  2. Second Beanshell script:

    import java.util.concurrent.ThreadLocalRandom;
    
    List lines = bsh.shared.lines;
    String line = lines.get(ThreadLocalRandom.current().nextInt(0, lines.size()));
    vars.put("line", line);
    

More information: How to Use BeanShell: JMeter's Favorite Built-in Component

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • Thank you for this however I have never used HTTP STS, is there an example of how I can use this in my situation. And you are correct the test does need to be repeatable, not only that but I have to then split the information and add to Variables. Thanks – miteshlimbachia Nov 15 '16 at 15:08
1

Change the following line:

String line = scanner.next();

to

String line = scanner.nextLine();

next() breaks as soon as space is found, while nextLine() waits for end of line.

Naveen Kumar R B
  • 6,248
  • 5
  • 32
  • 65