无法在 Jmeter 中运行 Groovy 脚本

Posted

技术标签:

【中文标题】无法在 Jmeter 中运行 Groovy 脚本【英文标题】:Can't able to run Groovy script in Jmeter 【发布时间】:2022-01-22 03:47:43 【问题描述】:

当我在 JSR223 后处理器中执行 groovy 脚本时,我收到以下错误。该脚本在早期版本中运行良好,但升级到 Jmeter5.4.2 时会抛出此错误。

021-12-20 19:47:02,491 WARN o.a.j.f.Groovy: Error running groovy script
javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: AESUtil for class: Script4
    at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:162) ~[groovy-all-2.4.16.jar:2.4.16]
    at javax.script.AbstractScriptEngine.eval(Unknown Source) ~[?:1.8.0_201]
    at org.apache.jmeter.functions.Groovy.execute(Groovy.java:120) [ApacheJMeter_functions.jar:5.4.2]
    at org.apache.jmeter.engine.util.CompoundVariable.execute(CompoundVariable.java:138) [ApacheJMeter_core.jar:5.4.2]
    at org.apache.jmeter.engine.util.CompoundVariable.execute(CompoundVariable.java:113) [ApacheJMeter_core.jar:5.4.2]
    at org.apache.jmeter.testelement.property.FunctionProperty.getStringValue(FunctionProperty.java:100) [ApacheJMeter_core.jar:5.4.2]
    at org.apache.jmeter.testelement.AbstractTestElement.getPropertyAsString(AbstractTestElement.java:280) [ApacheJMeter_core.jar:5.4.2]
    at org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase.getPath(HTTPSamplerBase.java:508) [ApacheJMeter_http.jar:5.4.2]
    at org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase.getUrl(HTTPSamplerBase.java:1079) [ApacheJMeter_http.jar:5.4.2]
    at org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase.sample(HTTPSamplerBase.java:1296) [ApacheJMeter_http.jar:5.4.2]
    at org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase.sample(HTTPSamplerBase.java:1285) [ApacheJMeter_http.jar:5.4.2]
    at org.apache.jmeter.threads.JMeterThread.doSampling(JMeterThread.java:638) [ApacheJMeter_core.jar:5.4.2]
    at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:558) [ApacheJMeter_core.jar:5.4.2]
    at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:489) [ApacheJMeter_core.jar:5.4.2]
    at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:256) [ApacheJMeter_core.jar:5.4.2]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_201]
Caused by: javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: AESUtil for class: Script4
    at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:324) ~[groovy-all-2.4.16.jar:2.4.16]
    at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:159) ~[groovy-all-2.4.16.jar:2.4.16]
    ... 15 more
Caused by: groovy.lang.MissingPropertyException: No such property: AESUtil for class: Script4
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53) ~[groovy-2.4.6.jar:2.4.6]
    at org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:52) ~[groovy-2.4.6.jar:2.4.6]
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:307) ~[groovy-2.4.6.jar:2.4.6]
    at Script4.run(Script4.groovy:1) ~[?:?]
    at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:321) ~[groovy-all-2.4.16.jar:2.4.16]
    at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:159) ~[groovy-all-2.4.16.jar:2.4.16]
    ... 15 more

【问题讨论】:

【参考方案1】:

您“无法在 Jmeter 中运行 Groovy 脚本”,因为您尝试使用 AESUtil 类(无论它是什么),而 JMeter/Groovy 不知道该类

为了能够使用AESUtil 类,您需要将它放在JMeter Classpath 下(将提供该类的.jar 复制到JMeter 安装的“lib”文件夹中)

还研究了 JMeter 5.4.2 的依赖项,似乎 JMeter 使用了 groovy version 3.0.7 并且从您的日志文件中看来您拥有 groovy-all-2.4.16.jar 所以我建议将其从 JMeter Classpath 中删除以避免潜在的 @ 987654323@

更多信息:How to Reuse Your JMeter Code with JAR Files and Save Time

【讨论】:

以上是关于无法在 Jmeter 中运行 Groovy 脚本的主要内容,如果未能解决你的问题,请参考以下文章

Jmeter通过groovy脚本,连接mongoDB

Elasticsearch 无法使用 lang groovy 运行内联脚本 [doc ....]

用Groovy构建java脚本

使用非GUI模式运行jmeter测试脚本

jmeter+Jenkins配合使用

在linux上运行jmeter