JMeter JSR223 脚本 - 我们如何加载多个 groovy 文件?

JMeter JSR223 script - how do we load multiple groovy files?

我想很好地构建我的代码以便在 JMeter 中使用,所以我计划将 groovy 脚本放在单独的 groovy 文件中。 JSR223脚本可以加载脚本文件并执行。到目前为止一切顺利。

groovy脚本会比较复杂,所以想把文件拆分成多个文件使用。例如有一个文件 utils.groovy,内容

public class SharedStore {
    // Initializes the store to store all information about all the data being worked upon
    def initializeStore() {
        HashMap allStore = new HashMap();
        // def props = org.apache.jmeter.util.JMeterUtils.getJMeterProperties()
        props.put("${storeHashSet}", allStore);
    }

此 class 将随着更多方法的增加而增长,并将由从 JMeter 调用的所有脚本使用。还会有其他类似的classes.

一个名为 InitializeEnvironment.groovy 的 JMeter 采样器因此想要使用此方法:

evaluate(new File("<<path>>/Utils.groovy"));  // This line is an attempt to include utils.groovy
var shr = new SharedStore().initializeStore();

上述包含 groovy 文件的尝试是基于此讨论 - (我尝试了该讨论中的其他选项但无济于事)

使用上述选项会引发错误:

Script70.groovy: 2: unable to resolve class SharedStore
 @ line 2, column 11.
   var shr = new SharedStore().initializeStore();

我更愿意将实用方法包装成一个 class,但我可以忍受在全局 space 中保留方法。 如果我确实设法像这个 - - 建议中那样加载附加文件,而不使用“class”并让 groovy 为我将其包装在 class 中,道具在 utils.groovy 中不可用,除非我包含 def props = org.apache.jmeter.util.JMeterUtils.getJMeterProperties() 行,即使那样 ${storeHashSet} 也不能使用

No such property: storeHashSet for class: SharedStore

我希望能够将脚本分解成更易于管理的文件,并能够访问这些文件中的 JMeter 变量和结构。

关于如何在 JMeter 中执行此操作的任何建议?

最直接的解决方案是:

  1. Compiling这些单独的文件变成classes
  2. Packaging them into .jar(这个可以跳过)
  3. 将 .jar 或(class 文件,如果您跳过第 2 步)放入 JMeter Classpath
  4. Using the .jar file in your JSR223 Test Elements