---
title: "__groovy"
description: "JMeter __groovy function reference: syntax, parameters, and practical examples for parameterizing test plans with dynamic values."
url: https://docs.jmeter.ai/functions/groovy/
lastUpdated: 2026-10-01
source: docs.jmeter.ai
---

# __groovy

*Also documented in context in the [full Functions and Variables reference](/user-manual/functions/#__groovy).*

**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](/components/jsr223-sampler/) element.

The `__groovy` function evaluates [Apache Groovy](http://groovy-lang.org/) 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](https://www.slf4j.org/api/org/slf4j/Logger.html) for the groovy function (*)
- `ctx` - [JMeterContext](https://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterContext.html) object
- `vars` - [JMeterVariables](https://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterVariables.html) object
- `props` - JMeterProperties (class [`java.util.Properties`](https://docs.oracle.com/javase/8/docs/api/java/util/Properties.html)) object
- `threadName` - the threadName (String)
- `sampler` - the current [Sampler](https://jmeter.apache.org/api/org/apache/jmeter/samplers/Sampler.html), if any
- `prev` - the previous [SampleResult](https://jmeter.apache.org/api/org/apache/jmeter/samplers/SampleResult.html), if any
- `OUT` - System.out

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

> **Note**
> When using this function please use the variables defined above rather than using string replacement to access a variable in your script.  Following this pattern will ensure that your tests are performant by ensuring that the Groovy can be cached.

For instance **don’t** do the following:

```bash
\${__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:

```bash
\${__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:

```bash
\${__groovy(123*456)}
```

: returns `56088`

```bash
\${__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 `\,`

> **Note**
> Remember to include any necessary quotes for text strings and JMeter variables that represent text strings.

### Example

```plaintext
${__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](/components/jsr223-preprocessor/) or [PostProcessor](/components/jsr223-postprocessor/) is more maintainable and easier to debug than a one-liner buried inside a field. See the [JSR223 Groovy Scripting Guide](/topics/jsr223-groovy-scripting-guide/).

## Related

- [Functions and Variables Guide](/topics/functions-and-variables/)
- [Regex Extractor Builder](/tools/regex-tester/)
- [JSR223 Groovy Scripting Guide](/topics/jsr223-groovy-scripting-guide/)
- [Full Functions Reference](/user-manual/functions/)
- [Component Reference](/user-manual/component-reference/)
