Customers Contact TR

Using Functions in Apache JMeter

 
 

In JMeter, useful functions are available to create flexible and sustainable test plans. The functions can be used in the fields of Sampler and other elements in the test tree. In this article, I explain some useful JMeter functions.

 

The primary call for functions is as follows:

 
${__functionName(parameters,..)}

Some JMeter functions:

 

* ${__log(param1, param2, param3)}

 

log function is a tool to do reporting on JMeter.

 

param1: Message content to be logged (required)

param2: Specifies log level (default is INFO)

param3: Throwable text

 

Sample use;

 
${__log(“http request made”)}, $__log(${message}, DEBUG)
 

* ${__time(param1, param2)}

 

time function returns the current time value in various formats. It can also be used as ${time} without any parameters, but it returns values in milliseconds if no format is specified.

 

param1: The parameter we specify the format as SimpleDateFormat.

param2: Specifies the name of the variable to which the returned value from the function is assigned.

 

Sample use;

 
${__time(yyyy.MMMM.dd GGG hh:mm aaa)} -> 2013.August.19 AD 02:37 PM
${__time(Y/M/dd)} -> 2013/8/19
 

* ${__RandomString(param1, param2, param3)}

 

RandomString function serves to create a random character string of the specified length.

 

param1: Specifies the length of the string to be generated. This parameter is mandatory.

param2: Specifies characters and generates random character strings using these characters.

param3: Specifies the name of the variable to store the results.

 

Sample use;


${__RandomString(10)} -> ıeqkk5gjjo
${__RandomString(10, kartaca)} ->tarkaaraka

 

* ${__property(param1, param2, param3)}

 

property function returns the properties of JMeter. If it cannot find the property, it returns the name of the property. Generally, you can use this function for test cases containing logical operations.

 

param1: Name of the property of the value

param2: Name of the variable to store the value of the property

param3: This value is returned if the property is not defined or not found.

 

Sample use;

 
${__property(user.dir,userstring)}
-> ${userstring} -> home/ahmetcan/apache-jmeter-2.9/bin
${__property(hede,property,kartaca)}
-> ${property} -> kartaca
(As there is no property as "hede," the variable is assigned the default value that we designated.)
 

If the user directory is home/ahmetcan/apache-jMeter-2.9/bin, we add an if condition to run the query:

 
   

* ${__split(param1, param2, param3)}

 

split function splits the given character string according to a specific pattern.

 

param1: Character string to be split

param2: Name of the variable that stores the values

param3: Specifies the pattern to be split

 

Sample use;

 
${__split(www.kartaca.com,url,.)}: 

Here, we split http://www.kartaca.com according to “.” sign and each string is stored in the following variables: url_1 -> www, url_2 -> kartaca, url_3 -> com

   

* ${__machineIP(param1)}

 

machineIP function returns the IP address of the local machine.

param1: Name of the variable to which the IP address is assigned

Sample use;

 
${__machineIP(my_ip_address)}
 

If we want to get the name of the machine, we can use the machineName function.

If we want to make a request to our local machine and we want to log the request URL, we can do as below:

 

Author: Kartaca