Apache JMeter – threadNum Function

threadNum is an in-build function of Apache JMeter and its type is ‘Information’, which provides specific details about the thread. By the way, JMeter in-build functions are divided into eight categories which are:

  1. Information
  2. Input
  3. Calculation
  4. Formatting
  5. Scripting
  6. Properties
  7. Variables
  8. Strings

Syntax: ${__threadNum}

Type: Information

Purpose: To get the Thread Number under a Thread Group. If there are multiple thread groups under the same test plan then threadNum() will return the separate thread number concerning each thread group. Example: If there are 2 thread groups under the same test plan then ${__threadNum} will return the values for each thread like below:

Thread GroupNumber of ThreadsReturn Value
Thread Group 012${__threadGroup} = 1 // called by thread 1
${__threadGroup} = 2 // called by thread 2
Thread Group 024${__threadGroup} = 1 // called by thread 1
${__threadGroup} = 2 // called by thread 2
${__threadGroup} = 3 // called by thread 3
${__threadGroup} = 4 // called by thread 4

Input Argument: No input value can pass in the threadNum() function.

Return Value: A natural number i.e. 1 to the maximum thread run under a thread group. The number belongs to the individual thread.

Use: The threadNum function has a specific use. If you want to get the response time of a transaction/request for each thread then you can pass the thread number in the name of the transaction/request.

Limitation:

  1. The threadNum() does not work in any Configuration elements (e.g. User Defined Variables) as these are run from a separate thread.
  2. It works only under the Thread Group level; not at the Test Plan level.

Related Functions:

  • getThreadNum(): It returns a number between zero and (max number of running threads minus one). You can use this function as below under the JSR223 post-processor.
  • Note: ctx is a variable to call JMeterContextClass.
log.info("Thread Number is: "+ctx.getThreadNum());


You may be interested:


Leave a Comment