Mumfi
Mumfi

Reputation: 425

Running preprocessor once per thread loop in JMeter

If I use a JSR223 Preprocessor with the following code:

log.info("" + ${rand});

where ${rand} is a random variable, how can I make this variable change every time I loop this thread?

Changing the number of threads will indeed have the variable change each run, with loop it just takes one value and keeps it for all the other loops.

Putting it in a JSR223 Sampler yields the same result. I basically want the code to behave as a User Parameter.

1

Upvotes: 1

Views: 771

Answers (2)

Ori Marko
Ori Marko

Reputation: 58872

You need to use vars to avoid getting cached/same value

 vars.get("rand")

See JSR223 Best practices

 script does not use any variable using ${varName} as caching would take only first value of ${varName.. Instead use vars.get("varName")

Upvotes: 1

Dmitri T
Dmitri T

Reputation: 168157

Don't inline JMeter Functions or Variables in JSR223 Test Elements because:

  1. They may resolve into something which will cause compilation failure

  2. The syntax conflicts with Groovy's GStrings feature

  3. If you tick Cache compiled script if available box the first occurrence will be cached and used on subsequent iterations, if you don't - you will loose performance benefits of Groovy

    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.

So:

Upvotes: 1

Related Questions