简体   繁体   English

RandomUtils.nextLong(10L,1000L) 在 JMeter HTTP 请求正文中失败并显示“意外令牌:10L”

[英]RandomUtils.nextLong(10L,1000L) fails with "unexpected token: 10L" in JMeter HTTP Request body

I have to pass a random long value in one of my HTTP request body data.我必须在我的 HTTP 请求正文数据之一中传递一个随机长值。

The code I used is "${__groovy(import org.apache.commons.lang3.RandomUtils;RandomUtils.nextLong(10L,1000L);)}"我使用的代码是"${__groovy(import org.apache.commons.lang3.RandomUtils;RandomUtils.nextLong(10L,1000L);)}"

I have tried the relevant groovy code in JSR223 preprocessor for groovy and it prints out the number correctly;我已经在 groovy 的 JSR223 预处理器中尝试了相关的 groovy 代码,它正确地打印出数字;

long rand = org.apache.commons.lang3.RandomUtils.nextLong(2000, 10000);
log.info("random   [" + rand + "]");

But when I user the same code in __groovy() inside the HTTP Request body, it fails with ;但是当我在 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]

I can see I can get other groovy scripts like "${__groovy(new Date().format("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");)}" working fine inside the HTTP Request Body Data.我可以看到我可以得到其他 groovy 脚本,例如“${__groovy(new Date().format("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");)}”在里面工作正常HTTP 请求正文数据。

What am I missing here?我在这里缺少什么?

UPDATE : I also tried the code without "L" even RandomUtils.nextLong(10,1000);更新:我也尝试了没有“L”的代码甚至RandomUtils.nextLong(10,1000); same exception.同样的例外。

https://jmeter.apache.org/usermanual/functions.html#__groovyhttps://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: \\,如果您需要在参数值中包含逗号,请像这样转义它: \\,

so, it should be like this:所以,它应该是这样的:

${__groovy( org.apache.commons.lang3.RandomUtils.nextLong(10\\,1000) )}

You need to escape the comma like:您需要像这样转义逗号:

${__groovy(return org.apache.commons.lang3.RandomUtils.nextLong(10\, 1000),)}

Next time consider using The Function Helper Dialog in order to get the correct syntax for JMeter Functions.下次考虑使用函数助手对话框以获得 JMeter 函数的正确语法。

In general it's recommended to use JMeter's built-in test elements and/or functions and avoid scripting where possible so consider using __Random() function instead, the relevant syntax would be:一般来说,建议使用 JMeter 的内置测试元素和/或函数,并尽可能避免编写脚本,因此请考虑使用__Random() 函数,相关语法为:

${__Random(10,1000,)}

if you provide the 3rd parameter - JMeter will store the generated value in the variable.如果您提供第三个参数 - JMeter 会将生成的值存储在变量中。

More information: Apache JMeter Functions - An Introduction更多信息: Apache JMeter 函数 - 简介

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM