I was given some already done test plan. I added (simple) groovy script that changes number of threads (users) for thread groups by using standard setNumThreads method. https://jmeter.apache.org/api/org/apache/jmeter/engine/StandardJMeterEngine.html

I noted that if in test plan number of threads (users) for thread group is set to be constant number, the script works fine, but if number is set as ${somevar} (in GUI for for thread group), the script seams to have no effect (JMeter version 5 tested).

As variables are not shared between thread groups, what is simplest to implement way of changing number of threads (users) at runtime in such case?

  1. Variables are not shared between thread groups but properties are, in fact they are global for the whole JVM
  2. You should not be referencing variables as ${somevar}, you should use vars.get('somevar') function instead where vars is a shorthand for JMeterVariables class. As per JSR223 Sampler documentation:

    JMeter processes function and variable references before passing the script field to the interpreter, so the references will only be resolved once. Variable and function references in script files will be passed verbatim to the interpreter, which is likely to cause a syntax error. In order to use runtime variables, please use the appropriate props methods, e.g.

  3. Depending on your use case you might want to reconsider your approach and instead of trying to add users you can increase the throughput by using Constant Throughput Timer

  • I edited question to clarify. ${varname} is seen in GUI for thread group object, not script. Object has to be changed by script. Script need to change the threads number during run time. – Alexei Martianov Jan 28 at 12:39

I think you have two problems.

  1. The expression "${somevar}" gets inserted by JMeter at compile time of the first call of the sampler and the script gets cached than by default.
  2. If the script didn't get cached (see "Cache compiled script if available) you would have another two problems.

    1. The groovy runtime would cache your scripts, which could lead to OOM
    2. The variable might be interpreted as a String and the API expects an int

If you want to use JMeter variables inside of groovy scripts, you should use vars.get("somevar") or vars.getObject("somevar").

If you want to test the behaviour yourself, then add the following content to your script:

 log.info("Count: ${somevar}")
 log.info("Count: " + vars.get("somevar"))

The logs will be printed to jmeter.log or can be found in the log panel.

  • no, I don't want to change variable. I want to change threads count, which in defined by variable as seen in GUI. – Alexei Martianov Jan 28 at 12:40
  • I understood that you don't want to change the value of the variable by hand. I wanted to give you an example to see, that changing the variable would probably not help. – Felix Schumacher Jan 28 at 17:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.