eBook > The JMeter Playbook: Build, Scale, and Optimize Performance Tests
Variables & Functions
Apart from loading data from CSV files, JMeter offers several other ways for creating and manipulating variables in the test design or run time.
Back to topUser Defined Variables
The simplest way to set a variable is through the User Defined Variables config element (or the Test Plan’s built-in variable table).
Right-click Test Plan → Add → Config Element → User Defined Variables.
| Name | Value |
|---|---|
baseUrl | https://demoblaze.com |
timeout | 5000 |
Reference them as ${baseUrl} and ${timeout} where required.
Back to topNote: User Defined Variables are evaluated once at test start. They do not change between iterations. For per-iteration values, use CSV Data Set Config or functions.
JMeter Functions
JMeter includes a rich library of built-in functions that could be embedded in any field using the syntax ${__functionName(args)}. You can explore and test all available functions via Tools → Function Helper Dialog in the GUI.
Commonly Used Functions
| Function | Purpose | Example | Output |
|---|---|---|---|
__Random | Random integer in given range | ${__Random(1,100,)} | 42 |
__RandomString | Random string of given length from the characters provided | ${__RandomString(8,abcdef0123456789,)} | a3f0c1b9 |
__time | Current timestamp in different formats | ${__time(yyyy-MM-dd,)} | 2026-04-04 |
__UUID | Random GUID-like structure | ${__UUID()} | 550e8400-e29b-... |
__threadNum | Current thread (virtual user) number | ${__threadNum} | 5 |
__iterationNum | Current Thread Group iteration (loop) number | ${__iterationNum} | 3 |
__P / __property | Read a JMeter property | ${__P(host,localhost)} | localhost (default) |
__setProperty | Set a global property | ${__setProperty(token,abc123,)} | |
__V | Evaluate a nested variable | ${__V(user_${index})} | value of user_3 |
__groovy | Inline Groovy expression | ${__groovy(vars.get("count").toInteger()+1,)} | 6 |
Example: Unique Email per Thread
${__threadNum}+user_${__RandomString(5,abcdefghijklmnop,)}@test.com
This produces something like 5+user_kfmab@test.com ; random enough for registration tests if the load is not too high.
Setting Variables at Runtime
JSR223 PreProcessor / PostProcessor
For complex logic, use a JSR223 Test Elements with Groovy (recommended for performance):
import java.time.Instant
// Set a variable scoped to the current thread
vars.put("requestId", UUID.randomUUID().toString())
vars.put("timestamp", Instant.now().toString())
Then reference ${requestId} and ${timestamp} in your sampler.
BeanShell vs. Groovy
| Language | Performance | Recommendation |
|---|---|---|
| Groovy | Compiled, fast | ✅ Always prefer Groovy |
| BeanShell | Interpreted, slow | ❌ Avoid in load tests |
| JavaScript (Nashorn) | Moderate | Removed in Java 15+ |
Back to topBest practice: Always select groovy in the Language dropdown of JSR223 test elements and check the “Cache compiled script if available” box.
Variables vs. Properties: When to Use Which
| Need | Use |
|---|---|
| Data unique to each thread (usernames, tokens local to thread/virtual user) | Variable - vars.put() / ${varName} |
| Data shared across thread groups (i.e. global token from setUp Thread Group) | Property - props.put() / ${__P(name)} |
| Value passed from the command line | Property - -Jname=value |
Passing Properties from the Command Line
jmeter -n -t test.jmx -Jhost=api.staging.example.com -Jthreads=100
In the test plan:
Server Name : ${__P(host,localhost)}
Number of Threads : ${__P(threads,1)}
Ramp-up period : ${__P(ramp-up,1)}
Loop Count : ${__P(loops,1)}
Duration : ${__P(duration,60)}
The second argument is a default value used if the property is not supplied. This way, you can parameterize your test from the command line without having to mofify the test plan file itself.
Summary
- User Defined Variables are evaluated once at test start - good for constants and to avoid duplicate data.
- Functions (
__Random,__time,__UUID, etc.) generate dynamic values inline. - JSR223 + Groovy gives you full combined programming power of JDK, JMeter API and Groody SDK for complex variable logic.
- Use Properties for global or command-line-driven values; use Variables for thread-local data.
Not let’s see how JMeter Post-Processors (Extractors) can extract data from responses and save it into variables for later re-use.