在JMeter中,我有一个CSV用户列表,每个用户都应该上传一定数量的文件。这些文件列在第二个CSV中。每个用户必须上传所有文件。由于服务器不能一次处理所有线程,因此我将Thread组设置为使用X个用户并循环Y次,以便最终所有用户都上传所有文件。
Test plan
- CSV Data Set Config (contains users) --> Recycle = false, Stop thread on EOF = false
- Thread Group (X users, Y loops)
- - While Controller ( ${__javaScript("${uploadFile}"!="<EOF>")} )
- - - CSV Data Set Config (contains files) --> Recycle = false, Stop thread on EOF = false
- - - HTTP Request (upload file)
对于第一个循环,一切正常:启动了X个线程。在每个线程中,都会打开files-CSV,每个用户都会上传所有文件。然后第二个线程组循环开始,下一个用户从users-CSV中取出,但是当这些线程到达包含文件-CSV的while-循环时,它们不会上传任何内容。似乎他们重用了第一个循环中的文件- CSV,并认为CSV是EOF或类似的东西。
例如,3个线程/用户和2个循环的结果如下所示:
**first loop**
user 1
upload file 1
upload file 2
upload file 3
user 2
upload file 1
upload file 2
upload file 3
user 3
upload file 1
upload file 2
upload file 3
**second loop**
user 4
user 5
user 6
第二个循环中的用户还应该检查files-CSV并上传文件。你知道我做错了什么吗?提前感谢!!
发布于 2014-03-24 06:12:40
我认为您需要在EOF上启用回收。我也会查看jmeter的日志,寻找一些关于为什么jmeter不再发送任何请求的提示。
When the end of file (EOF) is reached, and the recycle option is true,
reading starts again with the first line of the file.
If the recycle option is false, and stopThread is false, then all the
variables are set to <EOF> when the end of file is reached. This value
can be changed by setting the JMeter property csvdataset.eofstring .
发布于 2015-10-30 19:50:45
所以这就是我如何设置的:
我将Users
CSV设置为Stop on End of File
(而不是Recycle on EOF
)。
我将Files
CSV设置为Recycle on End of File
(而不是Stop on EOF
)。
请注意,我每个线程只有一个用户。是的,您将运行更多的线程循环,但每个循环将更短。
发布于 2020-09-27 07:35:24
我找不到一个简单的解决方案。我最终使用了beanshell脚本,它可以让你使用与java非常相似的代码来做一些自定义的事情。我做了一个示例JMeter项目来演示如何做到这一点(是的,它非常复杂,考虑到我只想重复CSV读取):
我的文件结构:
JMeterExample
|
⊢--JMeterTests.jmx // the JMeter file
⊢--example.csv // the CSV file
我的CSV的内容:
guest-id-1,"123 fake street",
guest-id-2,"456 fake street",
guest-id-3,"789 fake street",
所以在这个线程组中,我将只有一个用户,并且我将循环2次。我打算在每个CSV线路上发送1个请求。所以总共应该发送6个请求。
这是可选的,但是文件路径可能会改变,我不喜欢仅仅为了配置的改变而改变我的脚本。因此,我将CSV文件名存储在“用户定义变量”节点中。
如果将CSV文件存储在与JMeter测试相同的目录中,则只需指定文件名即可。
如果要将CSV保存在包含JMeter文件的目录以外的文件夹中,则需要提供绝对路径,然后稍微修改下面的beanshell脚本:您将需要注释掉相对加载文件的行,并注释从绝对路径加载的行。
用于解析和存储CSV线的
添加一个Beanshell采样器,它基本上接受一个路径,并将每一行解析并存储为一个变量。第一行将存储为名为csv_line_0
的变量,第二行将存储为csv_line_1
,依此类推。我知道这不是一个干净的解决方案但是..。我找不到任何干净利落的方法来完成这个干净利落的任务。我复制并粘贴了下面的代码。
import org.apache.jmeter.services.FileServer;
import java.text.*;
import java.io.*;
import java.util.*;
String temp = null;
ArrayList lines = new ArrayList();
BufferedReader bufRdr;
ArrayList strList = new ArrayList();
// get the file
try {
// you can use this line below if your csvFilePath is an absolute path
// File file = new File(${csvFilePath});
// you can use this line below if your csvFilepath is a relative path, relative to where you saved this JMeter file
File file = new File(org.apache.jmeter.services.FileServer.getFileServer().getBaseDir() + "/" + ${csvFilePath});
if (!file.exists()) {
throw new Exception ("ERROR: file " + filename + " not found");
}
bufRdr = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF8"));
} catch(Exception e){
log.error("failed to load file");
log.error(e.getMessage());
return;
}
// For each CSV line, save it to a variable
int counter = 0;
while(true){
try{
temp = bufRdr.readLine();
if(temp == null || temp.equals("<EOF>")){
break;
}
lines.add(temp);
vars.put("csv_line_" + String.valueOf(counter), temp);
counter++;
} catch(Exception e){
log.error("failed to get next line");
log.error(e.getMessage());
break;
}
}
// store the number of CSV lines there are for the loop counter
vars.put("linesCount", String.valueOf(lines.size()));
添加为每条CSV线路循环一次的Loop Controller。${linesCount}
是CSV行数的计数,由上面的beanShell脚本计算得出。
用于从当前CSV线提取数据的
此脚本将在每个CSV行上运行一次。它将获取当前行,并解析出其中的任何数据。您必须修改此脚本才能获得所需的数据。在我的示例中,我只有2列,其中列1是"guestId",列2是"address“。
__jm__loopController__idx
是JMeter为您定义的变量,也是循环控制器的索引。变量名为__jm__{loop controller name}__idx
。
String index = vars.get("__jm__loopController__idx");
String line = vars.get("csv_line_" + index);
String [] tokens = line.split(",");
vars.put("guestId", tokens[0]);
vars.put("address", tokens[1]);
下面是使用提取的数据的HTTP请求。
当运行这段代码时,根据需要,我最终向我定义的端点发送了6个http请求。
https://stackoverflow.com/questions/22594097
复制