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.
答案 0 :(得分:3)
首先,我认为测试需要是可重复的,因此如果测试显示错误,您可以重新运行测试以确认它或验证基础问题是否已修复。所以我建议保持CSV数据的“正常”顺序。性能良好的测试会自行清理,因此您不应该遇到数据无法重用的情况。
如果您仍在寻找一种随机化数据的方法,您可以尝试使用“RANDOM”模式返回该行的HTTP Simple Table Server插件。
如果毕竟你还想要Beanshell,请记住JMeter是建立在其他库的顶层,你可以从Beanshell脚本调用它们的方法。特别是在您的情况下,我相信FileUtils.readLines()会非常有用,例如:
First Beanshell脚本:
import org.apache.commons.io.FileUtils;
List lines = FileUtils.readLines(new File("c:/file.csv"));
bsh.shared.lines = lines;
Second Beanshell脚本:
import java.util.concurrent.ThreadLocalRandom;
List lines = bsh.shared.lines;
String line = lines.get(ThreadLocalRandom.current().nextInt(0, lines.size()));
vars.put("line", line);
更多信息:How to Use BeanShell: JMeter's Favorite Built-in Component
答案 1 :(得分:1)
更改以下行:
String line = scanner.next();
到
String line = scanner.nextLine();
一旦找到空格,next()会中断,而nextLine()会等待行尾。