Jaro
Jaro

Reputation: 125

How to parametrize number of threads in JMeter 5.2.1

I have a JMeter 5.2.1 project where in the SetUp thread I generate properties with names like ThreadGroupName1-NumberOfThreads, ThreadGroupName2-NumberOfThreads, etc. with values representing integers.

Now, I wish to access these properties in thread groups named ThreadGroupName1, ThreadGroupName2, etc. to parametrize the number of threads. I tried something like ${__jexl3(props.get(threadName + "-NumberOfThreads"))} but it fails as threadName evaluates to standardjmeterengine.

Also, I tried to use ctx but ctx.getThread() and ctx.getThreadGroup() but they evaluate to null.

So far what 'works' for me is ${__jexl3(props.get("ThreadGroupName1-NumberOfThreads"))} but I want it be parametrized by the name of the thread group.

Is it possible to do this?

Is this threadName returning standardjmeterengine a bug?

Update: In fact, the easiest 'solution' that provides the parametrized number of threads there is ${__P(ThreadGroupName1-NumberOfThreads)} and what I want is to generate this key ThreadGroupName1-NumberOfThreads to be something like ${MyCurrentGroupName}-NumberOfThreads, effectively providing a way to have an abstract method like

int GetNumberOfThreads(string threadGroupName)
{
  return properties.get(threadGroupName + "-NumberOfThreads";
}

Similarly, I wish to use this patter in Constant Throughput Timer as well with another prefix like -Rpm.

Upvotes: 0

Views: 768

Answers (2)

Dmitri T
Dmitri T

Reputation: 168072

I don't think you can use any JMeter Function in the "Number of Threads" field of the Thread Group so this is not something you can do via UI. If you believe this is something everyone needs you can consider raising an enhancement request

As a workaround you can

  1. Set the number of threads to 1
  2. Add If Controller to the Thread Group and use the following __groovy() function as the condition:

    ${__groovy(ctx.getThreadNum() == 0 && vars.getIteration() == 1,)}
    
  3. Add JSR223 Sampler as a child of the If Controller and put the following code into "Script" area:

    SampleResult.setIgnore()
    2.upto(props.get(ctx.getThreadGroup().getName() + '-NumberOfThreads') as int, { ctx.getThreadGroup().addNewThread(0, ctx.getEngine()) })
    

This way each thread Group will normally start with 1 thread, however this thread will read the property you defined earlier and add as many threads as needed.

Upvotes: 1

Ori Marko
Ori Marko

Reputation: 58772

You can use JMeterContext's getThreadNum to get thread number (increment, because it starts with 0)

${__jexl3(props.get("ThreadGroupName"))}${__jexl3((ctx.getThreadNum()+1) + "-NumberOfThreads" )} 

Upvotes: 0

Related Questions