RandomUtils.nextLong(10L,1000L) 失败,JMeter HTTP 请求正文中出现“意外令牌:10L”
我必须在我的某个 HTTP 请求主体数据中传递一个随机长值。
我使用的代码是
"${__groovy(i​​mport org.apache.commons.lang3.RandomUtils;RandomUtils.nextLong(10L,1000L);)}"
我已经在 groovy 的 JSR223 预处理器中尝试了相关的 groovy 代码,它正确地打印出了数字;
long rand = org.apache.commons.lang3.RandomUtils.nextLong(2000, 10000);
log.info("random [" + rand + "]");
但是当我在 HTTP 请求主体中的 __groovy() 中使用相同的代码时,它会失败;
2020-03-06 09:50:57,821 INFO o.a.j.m.J.JSR223 PreProcessor: random [4023]
2020-03-06 09:50:57,827 WARN o.a.j.f.Groovy: Error running groovy script
javax.script.ScriptException: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script358.groovy: 1: unexpected token: 10L @ line 1, column 66.
domUtils;RandomUtils.nextLong(10L
^
1 error
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_221]
at org.apache.jmeter.functions.Groovy.execute(Groovy.java:121) [ApacheJMeter_functions.jar:5.1.1 r1855137]
at org.apache.jmeter.engine.util.CompoundVariable.execute(CompoundVariable.java:136) [ApacheJMeter_core.jar:5.1.1 r1855137]
at org.apache.jmeter.engine.util.CompoundVariable.execute(CompoundVariable.java:111) [ApacheJMeter_core.jar:5.1.1 r1855137]
at org.apache.jmeter.testelement.property.FunctionProperty.getStringValue(FunctionProperty.java:101) [ApacheJMeter_core.jar:5.1.1 r1855137]
at org.apache.jmeter.testelement.AbstractTestElement.getPropertyAsString(AbstractTestElement.java:281) [ApacheJMeter_core.jar:5.1.1 r1855137]
at org.apache.jmeter.config.Argument.getValue(Argument.java:146) [ApacheJMeter_core.jar:5.1.1 r1855137]
at org.apache.jmeter.protocol.http.util.HTTPArgument.getEncodedValue(HTTPArgument.java:248) [ApacheJMeter_http.jar:5.1.1 r1855137]
at org.apache.jmeter.protocol.http.util.HTTPArgument.getEncodedValue(HTTPArgument.java:229) [ApacheJMeter_http.jar:5.1.1 r1855137]
at org.apache.jmeter.protocol.http.sampler.HTTPHC4Impl.setupHttpEntityEnclosingRequestData(HTTPHC4Impl.java:1529) [ApacheJMeter_http.jar:5.1.1 r1855137]
at org.apache.jmeter.protocol.http.sampler.HTTPHC4Impl.handleMethod(HTTPHC4Impl.java:794) [ApacheJMeter_http.jar:5.1.1 r1855137]
at org.apache.jmeter.protocol.http.sampler.HTTPHC4Impl.sample(HTTPHC4Impl.java:569) [ApacheJMeter_http.jar:5.1.1 r1855137]
at org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy.sample(HTTPSamplerProxy.java:67) [ApacheJMeter_http.jar:5.1.1 r1855137]
at org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase.sample(HTTPSamplerBase.java:1231) [ApacheJMeter_http.jar:5.1.1 r1855137]
at org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase.sample(HTTPSamplerBase.java:1220) [ApacheJMeter_http.jar:5.1.1 r1855137]
at org.apache.jmeter.threads.JMeterThread.doSampling(JMeterThread.java:622) [ApacheJMeter_core.jar:5.1.1 r1855137]
at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:546) [ApacheJMeter_core.jar:5.1.1 r1855137]
at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:486) [ApacheJMeter_core.jar:5.1.1 r1855137]
at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:253) [ApacheJMeter_core.jar:5.1.1 r1855137]
at java.lang.Thread.run(Unknown Source) [?:1.8.0_221]
我看到我可以获得其他 groovy 脚本,例如 "${__groovy(new Date().format("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");)}" 在 HTTP 请求正文数据中工作正常。
我在这里遗漏了什么?
更新:
我也尝试了没有“L”的代码,甚至
RandomUtils.nextLong(10,1000);
同样的异常。
https://jmeter.apache.org/usermanual/functions.html#__groovy
Argument values that themselves contain commas should be escaped as necessary. If you need to include a comma in your parameter value, escape it like this:
\,
所以,应该是这样的:
${__groovy( org.apache.commons.lang3.RandomUtils.nextLong(10\,1000) )>
您需要转义逗号,例如:
${__groovy(return org.apache.commons.lang3.RandomUtils.nextLong(10\, 1000),)}
下次请考虑使用 函数助手对话框 ,以获取 JMeter 函数的正确语法。
一般来说,建议使用 JMeter 的内置测试元素和/或函数,并尽可能避免编写脚本,因此请考虑使用 __Random() 函数 ,相关语法为:
${__Random(10,1000,)}
如果您提供第 3 个参数 - JMeter 会将生成的值存储在变量中。
更多信息: Apache JMeter 函数 - 简介