Jmeter Beanshell:访问全局数据列表



我正在使用jmeter来设计一个需要从文本文件中随机读取数据的测试。为了节省内存,我已经设置了一个使用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()))

但是,这引发了"键入变量声明"错误,我认为这是因为get()方法返回对象,并且我正在尝试在其中调用size(),因为它确实是列表。我不确定在这里做什么。我的最终目标是定义一些数据列表,一次在我的测试中全球使用,以便我的测试不必自己存储这些数据。

有人对可能出错的想法有什么想法吗?

编辑

我还尝试过以下定义设置线程组中的变量:

bsh.shared.items = items;

然后使用它们:

(bsh.shared.items).get(new Random().nextInt((bsh.shared.items).size()))

,但这会失败,因为在class'bsh.bsh.primitive'。

您非常接近,只需将铸件添加到列表中,以便解释器知道预期的对象是什么:

log.info(((List)props.get("items")).get(new Random().nextInt((props.get("items")).size())));

请注意,由于jmeter 3.1,建议将groovy用于任何形式的脚本,例如:

  • Groovy的性能要好得多
  • Groovy支持更现代的Java功能,而Beanshell您陷入了Java 5级
  • Groovy具有大量JDK增强功能,即File.ReadLines()函数

如此友善地在下面找到Groovy解决方案:

  1. 在第一个线程组中:

    props.put('items', new File(vars.get('DataFolder') + '/items.txt').readLines()
    
  2. 在第二个线程组中:

    def items = props.get('items')
    def randomLine = items.get(new Random().nextInt(items.size))
    

最新更新