3

In JMeter I have a thread group and I want to control how many threads are run using a jmeter variable. In the thread group I'm setting Number of Threads equal to ${numThreads}. I have a setup thread group that has a bean shell sampler with the following (this always runs before the main test thread group):

vars.put("numThreads","5");

If I set numThreads in a user defined variables config element in the setup thread group it will use the correct number of threads. However I want to control it using a variable I defined in a bean shell sampler and it is not working. I can see the variable is being created and I can print the value in the log but when I use the bean shell sampler the thread group does not correctly create 5 threads (it creates 0 threads). The only thing I can think of is they both create variables but maybe the user defined config element creates it as an integer type? When I debug the type of the variable it shows as a string regardless of if it is set in a user defined parms config or bean shell sampler.

log.debug(vars.get("numThreads").getClass().getName()); // this prints java.lang.String for both

Why does the thread group not create the correct number of threads based on the bean shell variable?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
George
  • 1,021
  • 15
  • 32
  • possible duplicate of [Change the thread count of test plan in JMeter, at run time](http://stackoverflow.com/questions/10312281/change-the-thread-count-of-test-plan-in-jmeter-at-run-time) – vins Jan 07 '15 at 22:06
  • I believe the accepted answer is wrong in that post as I found a way to do what I need. I posted on the other thread with some options for changing the number of running users during the runtime. Its a workaround for a limitation in jmeter but I think it is possible. – George Jan 09 '15 at 21:21

1 Answers1

5

Ok I figured it out. Looks like variables are thread specific and properties are global to the entire test. So setting a variable in the setup threadgroup was out of scope when my main thread group starts. Now Im setting a property in the setupgroup beanshell and using the following in the main threadgroup:

setup threadgroup beanshell: props.put("threadCount","3");

In the main threadgroup I can use the following to start the correct number of threads: ${__P(threadCount)}

Still no idea why the user defined variables config element was working - it must generate properties rather than variables or something.

George
  • 1,021
  • 15
  • 32
  • It's because variable scope is under thread. So value of the variable appears after threads have been initialized. But the property scope is global. So the value of the property exist before the test thread has been initialized and that value has been shared among threads. – Buaban May 17 '17 at 03:12