Jmeter Beanshell:访问全局数据列表
Jmeter Beanshell: Accessing global lists of data
我正在使用 Jmeter 设计一个需要从文本文件中随机读取数据的测试。为了节省内存,我设置了一个 "setUp Thread Group" 和一个 BeanShell 预处理器,其中包含以下内容:
//Imports
import org.apache.commons.io.FileUtils;
//Read data files
List items = FileUtils.readLines(new File(vars.get("DataFolder") + "/items.txt"));
//Store for future use
props.put("items", items);
然后我尝试在我的其他线程组中阅读此内容,并尝试使用如下内容访问我的文本文件中的随机行:
(props.get("items")).get(new Random().nextInt((props.get("items")).size()))
但是,这会引发 "Typed variable declaration" 错误,我认为这是因为 get() 方法 returns 是一个对象,我正在尝试对其调用 size(),因为它确实是一个列表。我不确定在这里做什么。我的最终目标是定义一些数据列表,以便在我的测试中全局使用,这样我的测试就不必自己存储这些数据。
有没有人知道哪里出了问题?
编辑
我也试过在 setUp 线程组中定义变量如下:
bsh.shared.items = items;
然后像这样使用它们:
(bsh.shared.items).get(new Random().nextInt((bsh.shared.items).size()))
但是失败并出现错误 "Method size() not found in class'bsh.Primitive'"。
你已经很接近了,只需将转换添加到 List 中,这样解释器就会知道预期的对象是什么:
log.info(((List)props.get("items")).get(new Random().nextInt((props.get("items")).size())));
请注意 since JMeter 3.1 it is recommended to use Groovy for any form of scripting 为:
- Groovy performance is much better
- Groovy 支持更现代的 Java 功能,而使用 Beanshell 时你会停留在 Java 5 级别
- Groovy 有很多 JDK 增强功能,即 File.readLines() 函数
请在下面找到 Groovy 解决方案:
在第一个线程组中:
props.put('items', new File(vars.get('DataFolder') + '/items.txt').readLines()
在第二个线程组中:
def items = props.get('items')
def randomLine = items.get(new Random().nextInt(items.size))
我正在使用 Jmeter 设计一个需要从文本文件中随机读取数据的测试。为了节省内存,我设置了一个 "setUp Thread Group" 和一个 BeanShell 预处理器,其中包含以下内容:
//Imports
import org.apache.commons.io.FileUtils;
//Read data files
List items = FileUtils.readLines(new File(vars.get("DataFolder") + "/items.txt"));
//Store for future use
props.put("items", items);
然后我尝试在我的其他线程组中阅读此内容,并尝试使用如下内容访问我的文本文件中的随机行:
(props.get("items")).get(new Random().nextInt((props.get("items")).size()))
但是,这会引发 "Typed variable declaration" 错误,我认为这是因为 get() 方法 returns 是一个对象,我正在尝试对其调用 size(),因为它确实是一个列表。我不确定在这里做什么。我的最终目标是定义一些数据列表,以便在我的测试中全局使用,这样我的测试就不必自己存储这些数据。
有没有人知道哪里出了问题?
编辑
我也试过在 setUp 线程组中定义变量如下:
bsh.shared.items = items;
然后像这样使用它们:
(bsh.shared.items).get(new Random().nextInt((bsh.shared.items).size()))
但是失败并出现错误 "Method size() not found in class'bsh.Primitive'"。
你已经很接近了,只需将转换添加到 List 中,这样解释器就会知道预期的对象是什么:
log.info(((List)props.get("items")).get(new Random().nextInt((props.get("items")).size())));
请注意 since JMeter 3.1 it is recommended to use Groovy for any form of scripting 为:
- Groovy performance is much better
- Groovy 支持更现代的 Java 功能,而使用 Beanshell 时你会停留在 Java 5 级别
- Groovy 有很多 JDK 增强功能,即 File.readLines() 函数
请在下面找到 Groovy 解决方案:
在第一个线程组中:
props.put('items', new File(vars.get('DataFolder') + '/items.txt').readLines()
在第二个线程组中:
def items = props.get('items') def randomLine = items.get(new Random().nextInt(items.size))