如何从JMeter的CSV数据集配置中读取下一行? [英] How Can I Read The Next Row From A CSV Data Set Config In JMeter?
问题描述
我正在JMeter中创建一个测试场所,该场所访问随机数量的页面(从2到10),这些页面的URL从CSV数据集中获取.我创建了CSV数据集和采样器,它们工作正常,只是每个线程从数据集中仅读取一行,这不是我所需要的-我希望在采样器完成后读取新行(或更早之前,我并不着急).
I am in the process of creating a test place in JMeter which visits a random amount of pages (from 2 - 10), whose URLs are to be fetched from a CSV Data Set. I have created the CSV Data Set and the samplers which are working fine, except that only one row is read from the Data Set per thread, which is not as a I need - I want a new row to be read after the sampler has completed (or before, I'm not fussed).
我看到这个问题非常相似,解决方案是使用原始数据Source Pre-Processor,它可以工作,但需要对相关文件进行艰巨的更改(在每行之前添加块大小),当文件长约500行时,这会有些麻烦.
I saw that this question is very similar and the solution was to use the Raw Data Source Pre-Processor, which does work but requires arduous alterations to the file in question (adding chunk sizes before each line), which is a bit of a pain when the file is about 500 lines long.
有没有一种方法可以将CSV数据集设置为在阅读时前进到下一行,或者使用某些后处理或预处理器(例如beanshell)来执行此操作?我见过有人指出 CSVRead 可以做到这一点,但是该声明指出是每个线程的,这对我没有好处.
Is there a way I can set the CSV Data Set to advance to the next row on reading, or use some post or pre processor, such as beanshell, in order to do this? I have seen people state that CSVRead can do this, but that states that access is per-thread, which would be no good for me.
作为旁注-最终,我要做的就是访问文件中的随机行,该行将传递给HTTP采样器,如果有更简便的方法,我可以征询您的意见.
As a side note - ultimately all I want to do is access a random line in the file which gets passed to a HTTP sampler, if there is an easier or better way to do this I'm open to suggestions.
推荐答案
对于从 BeanShell PostProcessor / BeanShell预处理器.
以下代码将从文件中读取所有行,然后选择单个随机数:
The following code will read all the lines from your file and then select single random:
import java.text.*;
import java.io.*;
import java.util.*;
String [] params = Parameters.split(",");
String csvTest = params[0];
String csvDir = params[0];
ArrayList strList = new ArrayList();
try {
File file = new File(System.getProperty("user.dir") + File.separator + csvDir + File.separator + csvTest);
if (!file.exists()) {
throw new Exception ("ERROR: file " + csvTest + " not found in " + csvDir + " directory.");
}
BufferedReader bufRdr = new BufferedReader(new FileReader(file));
String line = null;
while((line = bufRdr.readLine()) != null) {
strList.add(line);
}
bufRdr.close();
Random rnd = new java.util.Random();
vars.put("csvUrl",strList.get(rnd.nextInt(strList.size())));
}
catch (Exception ex) {
IsSuccess = false;
log.error(ex.getMessage());
System.err.println(ex.getMessage());
}
catch (Throwable thex) {
System.err.println(thex.getMessage());
}
然后,您可以通过变量(在此示例中为$ {csvUrl})访问提取的URL.
Then you can access extracted URL via variable (${csvUrl} in this example).
从性能的角度来看,我只怀疑在每次迭代中读取完整文件(如果必须在循环中执行此操作)是否是一个好的解决方案.
I doubt only that reading full file on each iteration (if you have to execute this in loop) is good solution from performance point of view.
这篇关于如何从JMeter的CSV数据集配置中读取下一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!