44

I am using a Thread Group with Number Of Threads = 5 with an HTTP request.

In the request I want to include a parameter with the value of the thread number, e.g.,

"pageno": ${threadno}

I want to get the thread number like ${threadno}.

How can I do that?

gtonic
  • 2,295
  • 1
  • 24
  • 32
Raju
  • 737
  • 2
  • 11
  • 21

4 Answers4

80

The thread number is available as:

${__threadNum}

See: functions reference

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
Andrey Pokhilko
  • 2,578
  • 18
  • 19
41

While the above-mentioned ${__threadNum} will work in many places within jMeter, you'll need to use something else where it is not allowed, e.g., script elements within Pre/Post-Processors.

This answer explains how to get thread number or count within such a script in jMeter.

To get the number of the current thread (out of 5 in your case) use ctx.getThreadNum() which will get the number of the thread.

To get the total number of threads being used by jMeter you can use ctx.getThreadGroup().getNumThreads() or ctx.getThreadGroup().getNumberOfThreads() for total active threads.

https://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterContext.html#getThreadNum() https://jmeter.apache.org/api/org/apache/jmeter/threads/AbstractThreadGroup.html

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
user3609244
  • 419
  • 4
  • 3
  • Is there a good reason not to use the documented `${__threadNum}` function? – Dave Newton Jul 21 '14 at 13:38
  • 1
    Yes: there are at least two spaces: a) the input-boxes of the GUI forms within the jMeter elements: There the `${__threadNum}` is sufficient, yes. But b) there is also the scripting area, mainly JSR223/groovy: There the programmatic approach `ctx.getThreadNum()` is needed. Instead of the pre-chewed "variables", programmatically, one get power to do almost whatever. – Franta Nov 03 '17 at 15:01
  • In groovy there is also the option to use a GString to evaluate the function: `log.info("thread(${__threadNum})")` – Paxic Mar 21 '18 at 23:21
4

For those looking for the number of active threads in the entire test plan, not just a particular ThreadGroup.

${__groovy(org.apache.jmeter.threads.JMeterContextService.getNumberOfThreads())}
abbas
  • 6,453
  • 2
  • 40
  • 36
-2

${__threadNum} does not work well alone.

You will need use ${__eval(${__threadNum})}. Try to use this:

int threadNum=ctx.getThreadGroup().getNumThreads(); 

ctx is from JmeterContext

MarekM
  • 1,426
  • 17
  • 14
  • 3
    Why would you need to eval a number? `__threadNum` as documented works just fine. Also, the number of threads is not the same as the current thread number. – Dave Newton Jul 21 '14 at 12:18