Search code examples
jmeterload-testingstress-testing

Use same random variable in all threads


I've got test plan :

Thread groups ( users 3, loop 2) Random Variable HTTP Request

I want variable to be changed only per loop, so under each iteration all three threads should send same value.

So I want something like this :

request where random var = X

request where random var = X

request where random var = X

request where random var = Y

request where random var = Y

request where random var = Y

I tried a lot of workounds but can't find proper solution.

P.S. I don't want to read variables from file. I need to generate them


Solution

  • No matter whatever you "want" the best option would be pre-generating random values somewhere in setUp Thread Group and writing it to the file and then using CSV Data Set Config in the "main" Thread Group to read the values.

    However if this is still not something you "want" here is yet another "workaround", hopefully it's "proper" enough for you:

    1. Add JSR223 PreProcessor as a child of the request which you "want" to parameterize with the random variable

    2. Put the following code into "Script" area:

      if (props.get('foo_' + vars.getIteration()) != null {
          props.put('foo_' + vars.getIteration(), org.apache.commons.lang3.RandomUtils.nextInt(0, 100))
      }
      
    3. Refer the "generated" random value using the following __groovy() function where required:

      ${__groovy(props.get('foo_' + vars.getIteration()),)}
      

    Demo:

    enter image description here