Functions and Variables in JMeter

JMeter Functions : JMeter functions are special values that can populate fields of any Sampler or other elements in a test tree.

The Syntax for JMeter Function is as below:


JMeter Variables : Container that can store a value that can be referred by any other element within the thread.

The Syntax for JMeter Variables is as below:


Different Types of Thread Group

Executing Different Functions and Variables in JMeter

  • Open the JMeter and add thread Group to the Test Plan.
  • Next, add one HTTP Request Sampler to the Thread Group.
  • Enter the Server Details in the HTTP request and Path Details as below.
  • Next, add View Result Tree and View Result in Table Listener to the Thread Group and then save and run the thread group.
  • Now, we will pass the log function in the JMeter, Where this log function will print the message which is present inside the double-quotes ${__log("Hi World")}‚Äč
  • If you run the thread group, the message will be printed in the view result tree.
  • The log function is assigning the message as a label name of the particular request.
  • Let us execute the Time function, which is going to execute the current date and time in the given pattern.
  • Here I am copying the same HTTP Request to execute the Time Function.
  • Now, save and run the request, after execution the time function will return the current date and time as below.
  • We can also print the thread numbers by using the thread number function.
  • First, go to the thread group and enter the number of threads as 5. and then enter the function as ${__threadNum}
  • Now save and run the request, after the execution the JMeter will return the number of threads in the output.
  • Before running the ThreadNum function I am disabling the Log and Time functions.
  • Even if you see the View Result in Table, you can see the thread numbers.
  • In the same way, the IntSum function will add the integers and return the sum of the two integers.
  • We have to pass the intSum function as ${__intSum(2,6)}
  • Now, save and run the request, the integer sum will be displayed as a label name of the request.
  • We can store this value in some variable as Sum, we can pass the function as ${__(2,6,sum)}, Where the sum is the variable name.
  • So, the value 8 will be stored in the variable Sum, and we can access this variable in any other thread group.
  • Let us pass another inter with a variable by using the intSum function as ${__intSum(100,${sum})}. So the JMeter will add 100 and the value of the variable 8 and should return the sum as 108
  • Save and run the request, after execution, you will see the sum of the integer and the variable as below.
  • I am trying to execute the second intSum function(${__intSum(100,${sum})})by disabling the First intSum function(${__intSum(2,6,sum)}), when I run the request I will get the output as 100, because if we disable the First request the JMeter is unable to access the Variable.
Do not disable the first intSum function of adding two integer value which is ${__intSum(2,6,sum)} while executing the function ${__intSum(100,${sum})} because the second intSum function is will be unable to access the variable sum.
  • Let us print some random number between 0 to 10 by using the Random function as ${__Random(0,10)}
  • To print random numbers, I had given Number of Threads as 7.
  • Save and run the request, after execution the 7 random numbers will be printed between 0 and 10.
  • In the same way, we can create some random string with 5 characters by using the function called ${__RadomString(5)}
  • Once you execute the request, it may take any kind of value.
  • If you want to print the string by using the given values then you need to pass the parameters like ${__RandomString(5,abcdefghijkl)}, the JMeter will print the random strings by selecting the 5 characters from the given values.
    random-string-function-with-given string-value-jmeter
  • And if you navigate to the JMeter official website, you will find the details description and different types of functions and variables.
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions