BreadcrumbHomeResourcesBlog Apache JMeter Functions - An Introduction April 14, 2020 Apache JMeter Functions - An IntroductionOpen Source AutomationApache JMeter™ functions are values in the JMeter script that enable flexible and more sophisticated management of the test scenarios. Functions dictate operations that can be performed on JMeter variables and constants.The structure of JMeter functions is:${__functionName(var1, var2, varN)}Functions can be used for many different operations, including: inputting information from a file, performing math calculations and value generation, processing strings, evaluating JMeter variables, working with JMeter properties and executing scripts like Javascript or BeanShell script.This blog post is a quick intro for using JMeter functions. For more detailed information, check out our guides:Using JMeter Functions - Part IUsing JMeter Functions - Part IIHow to Use JMeter FunctionsOr take a look at the complete function guide on the Apache JMeter website.In this blog post, we will show how to use functions in the JMeter script. We will look at the ThreadNum function and the Information (log) function, as well as at the Function Helper dialog.We will be testing our regular demo site - www.blazedemo.comTable of Contents:Creating a Basic Test PlanJMeter Thread Number FunctionThe Information Function and the Function Helper DialogCreating a Basic Test PlanFirst, let’s make sure our test works.1. In the test plan, add ‘serverurl’ as ‘www.blazedemo.com’2. Add a Cookie ManagerRight Click -> Add -> Config Element -> HTTP Cookie Manager3. Add a Request Defaults elementRight Click -> Add -> Config Element -> HTTP Request Defaults4. In the Request Defaults Server Name type:Serverurl url variable ${serverurl}Implementation - HTTPClient4Protocol - http5. Add a Thread GroupRight Click -> Add -> Threads -> Thread Group6. Add a HTTP Sampler as a child element to the Thread GroupRight Click on Thread Group -> Add -> Sampler -> HTTP Request7. Add a ListenerRight Click -> Add -> Listener -> View Results Tree8. Save the script and launch the test to ensure everything is working properly.Now we can add our functions.JMeter Thread Number FunctionThe Thread Number function is a function that shows the number of the thread that is being executed. To examine it, let’s modify one of the threads on our test to go through www.blazedemo.com/vacation.html1. So first, change the number of the threads to a different number, say 3.2. Add an If Controller as a child element to the Thread Group. The If Controller lets us execute requests based on a specific condition.Right Click on Thread Group -> Add -> Logic Controller -> If ControllerIn the Condition, write the JMeter function:${__threadNum()} == 1This function refers to the thread that is currently executed, in this case, the first one.4. Add a HTTP Sampler as a child element of the If ControllerRight Click on Thread Group -> Add -> Sampler -> HTTP Request5. Type vacation.html in the path field of the HTTP Request. This means we are running the first thread only through the vacation.html page.6. Save the test, clear the results and run the scriptYou can see in the results that the vacation page was opened only once, and only for the first thread (we changed the HTTP Request’s name to Vacation, to ensure it was running properly).The Information Function and the Function Helper DialogThe Function Helper Dialog can help you create function strings and see the different function options you have.1. Open it under JMeter Options, like this:2. In the Helper Dialog, you can choose the function you want and see what is required. Let’s look at the information (log) function, which logs a function and returns it in input string.3. Fill in the configurations:String to be logged - vacation.htmlLog level - OUT, this means this string will be printed out to the system logAdditional comment - Some comment4. Click on ‘Generate.You will get the results in the bottom string.${__log(vacation.html,OUT,,Some comment)}Now, you can copy and paste it wherever you need it in your script.5. We will put it in the path field of the HTTP Request under the If Controller.6. Save the script, clear previous results, launch the script7. Check the results and the system outlog. You can see that all the information was printed out in the system log, with the ‘Some comment’.Congratulations! You now know have the basic knowledge to start working with JMeter functions. BlazeMeter enhances JMeter tests.START TESTING NOW Related ResourcesApache Kafka vs. ActiveMQ: Key Benefits and Use Cases