JMeter - 从目录中读取文件 [英] JMeter - Read Files from Directory

查看:68
本文介绍了JMeter - 从目录中读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 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 msg,而且是缩进的,多行,如何将多行文件读入单输入变量?同样,当 JSON msg 是一行时,我没有问题.

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.

推荐答案

  1. 使用 Beanshell Sampler 将文件列表转换为 JMeter 变量:

  1. Use Beanshell Sampler to convert files list into a JMeter Variable:

  • Beanshell 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++;
}

这将导致变量设置为:

file_1=/path/to/your/folder/file1.txtfile_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 Controller 之后Beanshell Sampler 并配置如下:

Add ForEach Controller after Beanshell Sampler and configure it as follows:

  • 输入变量前缀:file
  • 循环的起始索引:0
  • 循环结束索引:留空
  • 输出变量名:任何有意义的,即current_file
  • 勾选数字前加_"

这篇关于JMeter - 从目录中读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆