JMeter-从目录读取文件 [英] JMeter - Read Files from Directory
问题描述
使用Apache JMeter
Using Apache JMeter
第一个问题:我能够从目录中读取一个文件(包含所有数据)并使用其数据.
First question: I was able to read one single file (containing all the data) from a directory and use its data.
但是现在的要求是每个数据点都有一个单独的 文件,这意味着我将不得不将许多数据文件放在目录中,然后读取这些文件离开该目录.我有一组数据文件,但是我不知道如何循环读取每个文件并将其发送给JMeter.
However now the requirement is to have a separate file for each data point, that means I will have to put many data files in a directory and then read the files off that directory. I have a set of data files, but I do not know how to read each of the files in a loop and send it to JMeter.
第二个问题:我们在这里讨论的数据是一个JSON消息,它是缩进的和多行,如何将多行文件读入单输入变量?同样,当JSON消息为一行时,我没有问题.
Second Question: the data we are talking about here is a JSON msg, and it's indented and multi-line, how to read a multi-line file into a single input variable? Again, I had no problem when the JSON msg is a single line.
推荐答案
-
使用 Beanshell采样器将文件列表转换为JMeter变量:
Use Beanshell Sampler to convert files list into a JMeter Variable:
- 在测试计划中添加 Beanshell采样器 li>
-
将以下代码放入Sampler的脚本"区域:
- Add a Beanshell Sampler to your test plan
Put the following code into the Sampler's "Script" area:
File folder = new File("/path/to/your/folder");
File[] files = folder.listFiles();
int counter = 1;
for (File file : files) {
vars.put("file_" + counter, file.getAbsolutePath());
counter++;
}
它将导致变量设置如下:
It will result into variables set like:
file_1 =/路径/到/您的/文件夹/file1.txt file_2 =/path/to/your/folder/file2.txt 等等.
file_1=/path/to/your/folder/file1.txt file_2=/path/to/your/folder/file2.txt etc.
在之后添加 ForEach控制器 Beanshell Sampler并进行如下配置:
Add ForEach Controller after Beanshell Sampler and configure it as follows:
- 输入变量前缀:
file
- 开始循环的索引:
0
- 结束循环索引:留空
- 输出变量名称:任何有意义的变量,例如
current_file
- 检查
Add "_" before number
- Input variable prefix:
file
- Start index for loop:
0
- End index for loop: leave blank
- Output variable name: anything meaningful, i.e.
current_file
- Check
Add "_" before number
这篇关于JMeter-从目录读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!