Skip to content

__groovy

JMeter __groovy function reference: syntax, parameters, and practical examples for parameterizing test plans with dynamic values.

Difficulty
intermediate
Guide type
reference
Estimated read time
1 min read
Last verified version
Verified JMeter 5.6

Also documented in context in the full Functions and Variables reference.

TL;DR: __groovy evaluates an inline Groovy expression and returns its result — useful for a one-line computation directly in a field, without a full JSR223 Sampler/PreProcessor element.

The __groovy function evaluates Apache Groovy scripts passed to it, and returns the result.

If the property “groovy.utilities” is defined, it will be loaded by the ScriptEngine. This can be used to define common methods and variables. There is a sample init file in the bin directory: utility.groovy.

The following variables are set before the script is executed:

(*) means that this is set before the init file, if any, is processed. Other variables vary from invocation to invocation.

For instance don’t do the following:

Terminal window
\${__groovy("\${myVar}".substring(0\,2))}

Imagine that the variable myVar changes with each transaction, the Groovy above cannot be cached as the script changes each time.

Instead do the following, which can be cached:

Terminal window
\${__groovy(vars.get("myVar").substring(0\,2))}
NameRequiredDescription
Expression to evaluateYesAn Apache Groovy script (not a file name) :::note Argument values that themselves contain commas should be escaped as necessary. If you need to include a comma in your parameter value, escape it like this: ‘\,’ :::
Name of variableNoA reference name for reusing the value computed by this function.

Example:

Terminal window
\${__groovy(123*456)}

: returns 56088

Terminal window
\${__groovy(vars.get("myVar").substring(0\,2))}

: If var’s value is JMeter, it will return JM as it runs String.substring(0,2). Note that , has been escaped to \,

${__groovy(1+1)} → 2
${__groovy(vars.get("count") as Integer + 1)} → increment a JMeter variable

For anything beyond a short expression — multi-line logic, imports, error handling — a JSR223 PreProcessor or PostProcessor is more maintainable and easier to debug than a one-liner buried inside a field. See the JSR223 Groovy Scripting Guide.

On this page