DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
What's in store for DevOps in 2023? Hear from the experts in our "DZone 2023 Preview: DevOps Edition" on Fri, Jan 27!
Save your seat
  1. DZone
  2. Software Design and Architecture
  3. Performance
  4. Apache JMeter Functions: An Introduction

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.

Noga Cohen user avatar by
Noga Cohen
·
Apr. 26, 17 · Tutorial
Like (2)
Save
Tweet
Share
6.28K Views

Join the DZone community and get the full member experience.

Join For Free

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: 

  • Using JMeter Functions: Part I.

  • Using JMeter Functions: Part II.

  • How to Use JMeter Functions: Part III.

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.

Apache JMeter

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • What Java Version Are You Running? Let’s Take a Look Under the Hood of the JDK!
  • Distributed SQL: An Alternative to Database Sharding
  • Using QuestDB to Collect Infrastructure Metrics
  • PostgreSQL: Bulk Loading Data With Node.js and Sequelize

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: