【问题标题】:Jmeter Beanshell: Accessing global lists of dataJmeter Beanshell:访问全局数据列表
【发布时间】:2018-05-27 22:57:50
【问题描述】:

我正在使用 Jmeter 设计一个需要从文本文件中随机读取数据的测试。为了节省内存,我使用 BeanShell 预处理器设置了一个“setUp 线程组”,其内容如下:

//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(),因为它实际上是一个 List。我不确定在这里做什么。我的最终目标是定义一些数据列表,以便在我的测试中全局使用,这样我的测试就不必自己存储这些数据。

有人对可能出现的问题有任何想法吗?

编辑

我也试过在setUp线程组中定义变量如下:

bsh.shared.items = items;

然后像这样使用它们:

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

但是失败并出现错误“在类'bsh.Primitive'中找不到方法大小()”。

【问题讨论】:

    标签: scripting automation jmeter beanshell


    【解决方案1】:

    注意since JMeter 3.1 it is recommended to use Groovy for any form of scripting 为:

    所以请在下面找到 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))
      

    【讨论】:

    • 感谢您的回答。我的下一步是将现有的 Beanshell 脚本转换为 Groovy。我确实接受第一个答案是正确的,因为它回答了这个特定问题,尽管这对我即将迁移到 Groovy 很有帮助。
    【解决方案2】:

    您非常接近,只需将转换添加到 List 以便解释器知道预期的对象是什么:

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

    【讨论】:

    • 谢谢,第二个“项目”的额外演员以及将这个元素从 Beanshell 预处理器更改为 Beanshell 采样器就成功了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-12
    • 1970-01-01
    • 2017-02-11
    • 2016-09-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多