Skip to content
Tutorials Center
My learning

Topics

Lesson 21 of 37 ยท Parameterization and Correlation

Variables, Properties and Functions in JMeter

Learn JMeter ${variables}, ${__P()} properties, User Defined Variables and built-in functions such as __Random, __UUID and __time, with a runnable resolver stand-in.

Almost every JMeter field accepts the syntax ${name}. Before a sampler runs, JMeter replaces each ${...} with a value. That one mechanism powers data files, extracted tokens, environment switches and generated values. This lesson explains the three kinds of named values and the built-in functions, and shows a tiny resolver that behaves the same way. (JMeter itself cannot run on this site, so the code here is a labelled Python stand-in.)

Variables, properties and functions

Thing Syntax Scope Typical use
Variable ${userId} One thread (each virtual user has its own copy) Data from a CSV file, extracted tokens
Property ${__P(host,localhost)} The whole JMeter JVM Values passed on the command line with -Jhost=example.org
Function ${__UUID()} Evaluated each time it is used Random numbers, timestamps, UUIDs

Variables are per thread. If thread 1 sets token to abc and thread 2 sets it to xyz, they never see each other's values. Properties are global and are read from jmeter.properties, user.properties or the command line. Properties are the right way to make a test configurable without editing the .jmx file.

User Defined Variables

Add > Config Element > User Defined Variables holds a table of Name / Value pairs. It is evaluated once, at the start of the test, and the values are shared by all threads. A common set is:

Name Value
protocol ${__P(protocol,https)}
host ${__P(host,shop.test)}
port ${__P(port,443)}
think ${__P(think,1000)}

Now -Jhost=staging.shop.test on the command line points the whole test at staging, and no .jmx edit is needed. Put one User Defined Variables element at the top of the Test Plan and use ${host} in every HTTP Request Defaults and sampler.

Use a Test Plan > User Defined Variables table for constants and __P for anything that changes between runs. It keeps environment settings in one place.

The function helper

Open Options > Function Helper Dialog to try functions. Pick a function, fill in its parameters and copy the generated text. Frequently used functions:

  • ${__Random(1,100,)}: random integer in a range. The third parameter is an optional variable name to store the result in.
  • ${__RandomString(8,abcdef0123456789)}: random text from a set of characters.
  • ${__UUID()}: a random UUID, handy for unique request ids.
  • ${__time(yyyy-MM-dd,)}: formatted current time. ${__time(,)} gives milliseconds since 1970.
  • ${__counter(FALSE,)}: increments by one on every call. TRUE makes the counter per thread.
  • ${__threadNum}: number of the current thread (starting at 1).
  • ${__P(name,default)}: read a property with a default. __property(name) reads without a default.
  • ${__V(name)}: evaluate a name built from other variables, such as ${__V(user_${__threadNum})}.
  • ${__CSVRead(users.csv,0)}: read a column from a file directly.
  • ${__BeanShell(...)} and ${__groovy(...)}: run a script. Prefer __groovy and use it sparingly, because scripts cost CPU in the load generator.

Functions can nest. ${__Random(1,${maxId},)} picks a random id up to a limit stored in another variable.

A resolver stand-in

To see exactly how nested substitution works, here is a small Python resolver. It resolves inner ${...} first, then evaluates the result. It is a teaching model, not JMeter's implementation.

Python Stand-in: a mini ${variable} and function resolver

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice the last two lines. A variable that does not exist is left as the literal text ${missingVar}; a property that does not exist falls back to the default you gave. In a real JMeter run, a literal ${missingVar} showing up in a request is the classic sign that a variable was never set.

Setting variables from scripts and elements

  • User Parameters (pre-processor): gives each thread a value from a table, one column per thread.
  • JSR223 PreProcessor: runs Groovy before a sampler. Use vars.put("name", "value") to set a variable and vars.get("name") to read it.
  • Regular Expression / JSON Extractor: saves values from a response into a variable (see extractors for correlation).
  • Counter (config element): an incrementing number with a format such as 000.
GROOVY
// JSR223 PreProcessor, language groovy
def orderNo = "ORD-" + vars.get("userId") + "-" + System.currentTimeMillis()
vars.put("orderNo", orderNo)
log.info("Using order number " + orderNo)

Properties are set with props.put("key", "value") and are visible to every thread. Be careful: a variable is per thread, a property is shared, and mixing them up is a common source of surprising results.

Common mistakes

  1. Using a variable before it is defined, for example in an element placed before the extractor in the tree.
  2. Forgetting that User Defined Variables run once at start-up, so they cannot hold a per-iteration value.
  3. Using __counter(TRUE) expecting a global counter. TRUE means each thread has its own.
  4. Overusing __groovy in hot paths. Cache the compiled script and keep it short.
  5. Typing a property name differently on the command line (-Jhost) and in ${__P(Host)}. Names are case sensitive.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which syntax reads a property named host with a default of localhost?

    __P returns the property value, or the second argument when the property is not set.
  2. 2. How do you set a property from the command line?

    -J defines a JMeter property for this run, readable with ${__P(host)}.
  3. 3. A request shows the literal text ${token}. What most likely happened?

    An undefined variable is left as written. Check the extractor, its position and the variable name.
  4. 4. What is the scope of a normal JMeter variable?

    Each thread has its own variables. Properties are the shared, JVM-wide values.

Summary

  • ${name} substitutes variables; ${__P(name,default)} reads properties; ${__function()} calls functions.
  • User Defined Variables are evaluated once; use __P inside them to make tests configurable.
  • Variables are per thread, properties are global.
  • The Function Helper Dialog builds function calls for you.
  • An unresolved ${...} in a request nearly always means a missing or late variable.

Next: Extractors for Correlation