Don't inline JMeter Functions or Variables <https://jmeter.apache.org/usermanual/functions.html> into Groovy scripts, either use "Parameters" section or go for code-based equivalents.

An one-liner which does the same:


String msg = org.apache.commons.lang3.RandomStringUtils.randomAlphabetic(org.apache.commons.lang3.RandomUtils.nextInt(500, 1000))


More information:

 * The Groovy Templates Cheat Sheet for JMeter
   <https://www.blazemeter.com/blog/the-groovy-templates-cheat-sheet-for-jmeter>
 * JSR223 Sampler Documentation
   
<https://jmeter.apache.org/usermanual/component_reference.html#JSR223_Sampler>,
   pay special attention to the chapter:

   /When using this feature, ensure your script code does not use
   JMeter variables or JMeter function calls directly in script code as
   caching would only cache first replacement. Instead use script
   parameters./


On 5/5/2022 2:07 PM, Raja Miah wrote:

hi


I think I am probably overlooking something simple here and would appreciate any help...


- Using Jmeter 5.4

- Script should:

    > pick a random number between two given numbers (lower and higher
    limit)

    > Use this random number to produce a random string


The script is placed in a JSR223 sampler:


    //Set a random nummber between 500-1000 for message length

    MESSAGE_LENGTH = ${__Random(500,1000)}

    log.info("################## MESSAGE_LENGTH : " + MESSAGE_LENGTH)


    //Generate a random string which uses the message length from above

    String msg =
    ${__RandomString('MESSAGE_LENGTH',abcdefghijklmnopqrstuvwxyz)}

    log.info("################## myLength : " + msg)



I get the following output in the logs:


    2022-05-05 12:59:03,377 INFO o.a.j.e.StandardJMeterEngine: Running
    the test!

    2022-05-05 12:59:03,378 INFO o.a.j.s.SampleEvent: List of
    sample_variables: []

    2022-05-05 12:59:03,378 ERROR o.a.j.JMeter: Uncaught exception in
    thread Thread[StandardJMeterEngine,6,main]

    java.lang.NumberFormatException: For input string: "'MESSAGE_LENGTH'"

    at java.lang.NumberFormatException.forInputString(Unknown Source)
    ~[?:1.8.0_291]

    at java.lang.Integer.parseInt(Unknown Source) ~[?:1.8.0_291]

    at java.lang.Integer.parseInt(Unknown Source) ~[?:1.8.0_291]

    at
    org.apache.jmeter.functions.RandomString.execute(RandomString.java:74)
    ~[ApacheJMeter_functions.jar:5.4]

    at
    
org.apache.jmeter.engine.util.CompoundVariable.execute(CompoundVariable.java:138)
    ~[ApacheJMeter_core.jar:5.4]

    at
    
org.apache.jmeter.engine.util.CompoundVariable.execute(CompoundVariable.java:113)
    ~[ApacheJMeter_core.jar:5.4]

    at
    
org.apache.jmeter.testelement.property.FunctionProperty.getStringValue(FunctionProperty.java:91)
    ~[ApacheJMeter_core.jar:5.4]

    at
    
org.apache.jmeter.testbeans.TestBeanHelper.unwrapProperty(TestBeanHelper.java:129)
    ~[ApacheJMeter_core.jar:5.4]

    at
    org.apache.jmeter.testbeans.TestBeanHelper.prepare(TestBeanHelper.java:84)
    ~[ApacheJMeter_core.jar:5.4]

    at
    
org.apache.jmeter.engine.StandardJMeterEngine.notifyTestListenersOfStart(StandardJMeterEngine.java:202)
    ~[ApacheJMeter_core.jar:5.4]

    at
    
org.apache.jmeter.engine.StandardJMeterEngine.run(StandardJMeterEngine.java:382)
    ~[ApacheJMeter_core.jar:5.4]

    at java.lang.Thread.run(Unknown Source) [?:1.8.0_291]



Thank you in advance.


Raja


---------------------------------------------------------------------
To unsubscribe, e-mail:user-unsubscr...@jmeter.apache.org
For additional commands, e-mail:user-h...@jmeter.apache.org

Reply via email to