Javatpoint Logo
Javatpoint Logo

JMeter Functions

JMeter functions can be referred as special values that can populate fields of any Sampler or other element in a test tree.

Syntax of a function in JMeter:

Here "__functionName" matches the name of a function and Parentheses surround the parameters sent to the function.

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

For example:

List of Functions

Type of function Name Comment
Information threadNum Get thread number.
Information samplerName Get the sampler name (label).
Information log Log (or display) a message (and return the value).
Information machineName Get the local machine name.
Input StringFromFile Read a line from a file.
Input FileToString Read an entire file.
Input CSVRead Read from CSV delimited file.
Input XPath Use an XPath expression to read from a file.
Calculation Counter Generate an incrementing number.
Calculation intSum Add int numbers.
Calculation longSum Add long numbers.
Calculation Random Generate a random number.
Calculation RandomString Generate a random string.
Scripting BeanShell Run a BeanShell script.
Scripting javaScript Process JavaScript (Mozilla Rhino).
Scripting jexl, jexl2 Evaluate a Commons Jexl expression.
Properties Property Read a property.
Properties P Read a property (shorthand method).
Variables Split Split a string into variables.
Variables V Evaluate a variable name.
Variables eval Evaluate a variable expression.
String regexFunction Parse previous response using a regular expression.
String escapeOroRegexpChars Quote meta chars used by ORO regular expression.
String Char Generate Unicode char values from a list of numbers.
String Unescape Process strings containing Java escapes (e.g. \n & \t).
String unescapeHtml Decode HTML-encoded strings.
String escapeHtml Encode strings using HTML encoding.
String TestPlanName Return name of current test plan.

Note: JMeter Functions and Variables are always case sensitive.

Let us consider an example of Log Function to understand the use of functions in JMeter.

Syntax of JMeter Log Function:

First, we will create a simple test plan with HTTP request sampler.

Create JMeter Test Plan

  • Go to your JMeterbin folder and double click on the Apache JMeter ".jar" file to launch JMeter interface.
  • Select the test plan node and right click on the selected item.
  • Mouse hover on "Add" option then elements list will be displayed.
  • Select Threads (Users) > Thread Group.
  • Rename this Thread Group as Thread Group 1.

Add Sampler

We have to add a HTTP request sampler in our Thread Group.

  • Select the Thread Group and right click on the selected item.
  • Mouse hover on "Add" option then elements list will be displayed.
  • Select Sampler > HTTP request option.
  • It will add an empty HTTP Request Sampler.

JMeter Functions

Configure the fields of HTTP Request Sampler as

  • Name - HTTP Request Sampler 1.
  • Server Name or IP - In our case, web server name will be www.javatpoint.com.
  • Protocol - This field is left unaltered.
  • Path - We will write it as "/" (slash). It indicates that we want the root page of the server.

Now, copy the entire Thread Group 1 and paste it three times in our test plan.

  • Rename the other thread groups as Thread Group 2 and Thread Group 3 respectively.
  • Rename the other samplers as HTTP Request 2 and HTTP Request 3 respectively.

The following image shows the directory structure of our test plan.


JMeter Functions

Add Listener

  • Select the Test Plan node and right click on the selected item.
  • Mouse hover on "Add" option then elements list will be displayed.
  • Select Listener > Summary Report.

JMeter Functions

Save and Execute Test Plan

  • Click on File > Save Test Plan as.
  • Save the entire test plan as Function_test.jmx

JMeter Functions
  • Click on Run > Start to execute the test plan.

Verify the Output

JMeter Functions

Hence, we have successfully executed our test plan. Now, we will use the log function as name for the first HTTP Request Sampler.

JMeter Functions

Click on Run > Start to execute the test plan.

JMeter Functions

You can see that the Log Function has printed "Hello World" instead of complete sampler name. Let us consider one more example of Time Function which we will use again as name for second HTTP Sampler.

Syntax for time function:


JMeter Functions

Click on Run > Start to execute the test plan.


JMeter Functions

You can see that the Time Function has printed the exact time and date instead of complete sampler name.

To get more knowledge of JMeter Functions and their usage, refer the document release by Apache JMeter provided under the link: http://jmeter.apache.org/usermanual/functions.html







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA