Apache JMeter Functions: An Introduction

DZone 's Guide to

Apache JMeter Functions: An Introduction

Get a basic working knowledge of how to get started working with JMeter functions, starting with creating a test plan and editing with the Information function.

· Performance Zone ·
Free Resource

Apache 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: 

Or 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.

Creating a Basic Test Plan

 First, let’s make sure our test works. In the test plan, add serverurl as www.blazedemo.com

jmeter test plan serverurl

Next, add a Cookie Manager. Right click > Add > Config Element > HTTP Cookie Manager.

Now, add a Request Defaults element. Right click > Add > Config Element > HTTP Request Defaults.

In the Request Defaults Server Name, type: 

  • Serverurl url variable ${serverurl}.
  • Implementation HTTPClient4.
  • Protocol http

jmeter http request defaults serverurl

Add a Thread Group. Right click > Add > Threads > Thread Group. Then, add an HTTP Sampler as a child element to the Thread Group. Right clickon Thread Group > Add > Sampler > HTTP Request.

 Now, add a Listener. Right click > Add > Listener > View Results Tree

jmeter test scenario

Save the script and launch the test to ensure everything is working properly.

jmeter test results view results tree

Now we can add our functions.

JMeter Thread Number Function

The 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 here.

First, change the number of the threads to a different number, say, 3.

jmeter thread group

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 Controller.

In the Condition, write the JMeter function ${__threadNum()} == 1

This function refers to the thread that is currently executed — in this case, the first one.

jmeter if controller

Add an HTTP Sampler as a child element of the If Controller. Right click on Thread Group > Add > Sampler > HTTP Request.

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.

jmeter http request functions

Save the test, clear the results, and run the script.

jmeter test scenario

You 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).

jmeter test results threadnum functions

jmeter test results threadnum function

The Information Function and the Function Helper Dialog

The Function Helper Dialog can help you create function strings and see the different function options you have.

Open it under JMeter Options like this: 

jmeter function help dialog

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 the input string. 

jmeter log function

Fill in the configurations:

  • String to be logged vacation.html.
  • Log level OUT; this means this string will be printed out to the system log.
  • Additional comment Some comment.

jmeter log function configuration

jmeter log function configuration

Click Generate. You will get the results in the bottom string: ${__log(vacation.html,OUT,,Some comment)}.

jmeter log function configuration

Now, you can copy and paste it wherever you need it in your script. We will put it in the path field of the HTTP Request under the If Controller. 

jmeter http request log function

Save the script, clear previous results, and launch the script. 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.

jmeter log function results in log

 Congratulations! You now know have the basic knowledge to start working with JMeter functions.

jmeter, jmeter functions, performance, tutorial

Published at DZone with permission of Noga Cohen , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}