【问题标题】:Jmeter wait until CSV File is readableJmeter 等到 CSV 文件可读
【发布时间】:2021-08-18 16:13:50
【问题描述】:

我有一个包含两个线程组的 jmeter 测试计划。一个线程组依赖于从另一个线程组写入的数据。

这种设计似乎可行,除了我面临的竞争条件。在依赖线程组中,我添加了一个检查以查看公共文件是否存在,然后再尝试读取它。

目前我在 while 控制器中使用以下表达式,执行睡眠动作。

${__groovy(!((new File(org.apache.jmeter.services.FileServer.getFileServer().getBaseDir() + "/someFileName")).exists()))}

我面临的异常来自我的依赖线程组,尝试从文件读取时出现 IllegalArgumentException,错误消息指出文件需要存在或可读。我认为正在发生的事情是我的检查通过了,因为文件已创建但第一行尚不存在。

是否有解决此类问题的方法?

我从 js223 预处理器尝试了以下方法,但运气不佳..

def boolean isFileReadable() {
    try {
        File file = new File(org.apache.jmeter.services.FileServer.getFileServer().getBaseDir() +"/sampleData/temp/vbinformation.csv");
        FileReader fileReader = new FileReader(file.getAbsolutePath());
        fileReader.read();
        fileReader.close();
    } catch (Exception e) {
        return false;
        print(false)
    }
    print(true)
    return true;
}

while (!isFileReadable()) {
    sleep(2000)
}

【问题讨论】:

    标签: groovy jmeter


    【解决方案1】:

    如果您只想共享一个字符串,您可以使用Inter Thread Communication Plugin 在线程之间共享数据(字符串)作为快速解决方案。

    如果您要连续传递多个值并想坚持使用您的解决方案,您也可以检查以下条件。

    file.length()>0
    

    另外,您可以考虑使用带有 JSR223 元素的 Critical Section Controller 来确保文件一次只能由一个线程访问。

    【讨论】:

      【解决方案2】:
      1. CSV Data Set ConfigConfiguration Element
      2. 根据JMeter Test Elements Execution Order 配置元素执行在其他任何事情之前

      因此,如果您在具有 CSV 数据集配置的线程组启动的同时启动线程组,它会尝试读取 CSV 文件并失败,因为它不存在。所以我建议迁移到__CSVRead() function,它会在调用函数时读取 CSV 文件

      另一个选项是使用 JMeter Properties 在线程组之间传递数据,您可以在任何 JSR223 测试元素中使用 props 简写或 __setProperty()__P() functions 组合

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-16
        • 2018-11-28
        • 2018-11-18
        • 2019-09-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多