Apache JMeter Pre-defined Functions

In the software world, functions are a very simple concept that carries out a certain, pre-defined activity by accepting some input and producing the desired result.

Moreover, Apache JMeter contains certain in-built functions that speed up and simplify various elementary arithmetic and coding operations.

Apache JMeter Pre-defined Functions

How to call or use JMeter functions?

The format of the JMeter function is given below:

${__functionName(var1,var2,var3)}

Firstly, you must know the appropriate name of the function. To call a function, enter a dollar sign $, a curly brace open {, two underscores __, and the correct function name. After that, add the parameters separated by a comma , enclosed in parentheses () and at last close curly brace }.  

Example: ${__intSum(2,4)}
Output: 6

Where functionName is intSum. 2 and 4 are input parameters and parentheses surround the parameters sent to the function. 

The actual parameters vary from function to function. Functions that require no parameters can leave off the parentheses

Example: ${__threadNum}

If a function parameter contains a comma, then be sure to escape this with “\“, otherwise, JMeter will treat it as a parameter delimiter. 

Example: ${__time(EEEE\, yyyy-mm-dd,)}
Output: Thursday, 2023-13-16

Apache JMeter Function List

1. Information Type Functions:

Such functions are used to get the value or information from the system or JMeter.

  • threadNum: To get the thread number
  • threadGroupName: To get the thread group name
  • samplerName: To get the sampler name (label)
  • machineIP: To get the local machine IP address
  • machineName: To get the local machine name
  • time: To get the current time in various formats.
  • timeShift: To get a date in various formats with the specified amount of seconds/minutes/hours/days added
  • log: To get the log (or display) a message (and return the value)
  • logn: To get the log (or display) a message (empty return value)

2. Input Type Functions

Such functions are used for read and write operations.

  • StringFromFile: To read a line from a file
  • FileToString: To read an entire file
  • CSVRead: To read from CSV delimited file
  • XPath: To use an XPath expression to read from a file
  • StringToFile: To write a string to a file

3. Calculation Type Functions

Such functions are used for some basic mathematical operations.

  • counter: To generate an incrementing number
  • digest: To Generate a digest (SHA-1, SHA-256, MD5…)
  • intSum: To add int numbers
  • longSum: To add long numbers
  • Random: To generate a random number
  • RandomDate: To generate random date within a specific date range
  • RandomFromMultipleVars: To extracts an element from the values of a set of variables separated by |
  • RandomString UUID: To generate a random string generate a random type 4 UUID

4. Formatting Type Function:

Such a function is used for formatting.

  • dateTimeConvert: To convert a date or time from source to target format

5. Properties Type Function:

Such functions are used to perform the operation on JMeter or user-defined properties

  • isPropDefined: To test if a property exists
  • property: To read a property
  • P: To read a property (shorthand method)
  • setProperty: To set a JMeter property
  • isVarDefined: To test if a variable exists

6. Scripting Type Function:

Such functions are used for coding-level scripting and validation.

  • groovy: To run an Apache Groovy script
  • BeanShell: To run a BeanShell script
  • javaScript: To process JavaScript (Nashorn)
  • jexl2: To evaluate a Commons Jexl2 expression
  • jexl3: To evaluate a Commons Jexl3 expression

7. String Type Functions:

Such functions are used to perform string operations.

  • char: To generate Unicode char values from a list of numbers
  • changeCase: To change the case following different modes
  • escapeHtml: To encode strings using HTML
  • escapeOroRegexpChars: To encode quote meta chars used by ORO regular expression
  • escapeXml: To encode strings using XML.
  • regexFunction: To encode and parse previous response using a regular expression
  • unescape: To process strings containing Java escapes (e.g. \n & \t)
  • unescapeHtml: To decode HTML-encoded strings
  • urldecode: To decode an application/x-www-form-urlencoded string
  • urlencode: To encode a string to an application/x-www-form-urlencoded string
  • TestPlanName: To return the name of the current test plan

8. Variables Type Functions:

Such functions are used to perform the operation on variables.

  • split: To split a string into variables
  • eval: To evaluate a variable expression
  • evalVar: To evaluate an expression stored in a variable
  • V: To evaluate a variable name

Use – Function Helper Dialog

To get more understanding and practical, JMeter provides Function Helper Dialog under the ‘Tools’ tab of JMeter UI.

  1. Using the Function Helper, you can select a function from the drop-down and assign values for its arguments.
  2. The left column in the table provides a brief description of the argument and the right column is where you write in the value for that argument.
  3. Please note different functions take different arguments.
  4. Once you have provided the argument(s) then click the ‘Generate’ button and the appropriate string is generated for you to copy
  5. Now, you can paste the copied functions into the test plan wherever you like.

So, use the above Apache JMeter pre-defined function in your JMeter script and optimize the script.


You may be interested: