JMeter __groovy function reference: syntax, parameters, and practical examples for parameterizing test plans with dynamic values.
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:
log- the Logger for the groovy function (*)ctx- JMeterContext objectvars- JMeterVariables objectprops- JMeterProperties (classjava.util.Properties) objectthreadName- the threadName (String)sampler- the current Sampler, if anyprev- the previous SampleResult, if anyOUT- System.out
(*) 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:
\${__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:
\${__groovy(vars.get("myVar").substring(0\,2))}| Name | Required | Description |
|---|---|---|
| Expression to evaluate | Yes | An 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 variable | No | A reference name for reusing the value computed by this function. |
Example:
\${__groovy(123*456)}: returns 56088
\${__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 \,
Example
Section titled โExampleโ${__groovy(1+1)} โ 2${__groovy(vars.get("count") as Integer + 1)} โ increment a JMeter variableFor 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.