RandomUtils.nextLong(10L,1000L) 在 JMeter HTTP 请求正文中失败并显示 "unexpected token: 10L"
RandomUtils.nextLong(10L,1000L) fails with "unexpected token: 10L" in JMeter HTTP Request body
我必须在我的一个 HTTP 请求正文数据中传递一个随机长值。
我使用的代码是"${__groovy(import org.apache.commons.lang3.RandomUtils;RandomUtils.nextLong(10L,1000L);)}"
我已经在 JSR223 预处理器中尝试了 groovy 的相关 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),)}
下次考虑使用 The Function Helper Dialog 以获得 JMeter 函数的正确语法。
通常建议使用 JMeter 的内置测试元素 and/or 函数并尽可能避免编写脚本,因此请考虑使用 __Random() function,相关语法为:
${__Random(10,1000,)}
如果您提供第三个参数 - JMeter 会将生成的值存储在变量中。
我必须在我的一个 HTTP 请求正文数据中传递一个随机长值。
我使用的代码是"${__groovy(import org.apache.commons.lang3.RandomUtils;RandomUtils.nextLong(10L,1000L);)}"
我已经在 JSR223 预处理器中尝试了 groovy 的相关 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),)}
下次考虑使用 The Function Helper Dialog 以获得 JMeter 函数的正确语法。
通常建议使用 JMeter 的内置测试元素 and/or 函数并尽可能避免编写脚本,因此请考虑使用 __Random() function,相关语法为:
${__Random(10,1000,)}
如果您提供第三个参数 - JMeter 会将生成的值存储在变量中。