Operations 5 min read

Mastering JMeter Variables with Groovy: Private vs Shared Scope

This tutorial shows how to use Groovy scripts inside JMeter to set and retrieve both thread‑local (private) and thread‑shared (global) variables, providing step‑by‑step configuration, code snippets, and sample console output for each case.

FunTester
FunTester
FunTester
Mastering JMeter Variables with Groovy: Private vs Shared Scope

Introduction

The article demonstrates practical ways to manipulate JMeter variables using Groovy, covering both thread‑private (local) and thread‑shared (global) scopes. It includes detailed configuration steps, Groovy code examples, and the resulting console logs.

Thread‑Private Variables

Create a simple Thread Group and a basic request.

Note: The parameter t refers to the local variable MY1.

Add a JSR223 PreProcessor (post‑processor would affect the next request).

Groovy script used:

OUT.println 'FunTester'
log.error '输出JMeter控制台错误'
vars.put("MY1","FunTester")
def my_var = vars.get("MY")
log.warn "输出参数-------- ${vars} console"
log.info("222222 " + my_var)

Console output example:

2020-03-03 21:02:30,499 INFO o.a.j.t.JMeterThread: Thread started: 线程组 1-1
2020-03-03 21:02:30,517 ERROR o.a.j.m.J.JSR223 预处理程序: 输出JMeter控制台错误
2020-03-03 21:02:30,518 WARN o.a.j.m.J.JSR223 预处理程序: 输出参数-------- org.apache.jmeter.threads.JMeterVariables@14a52b54 console
2020-03-03 21:02:30,518 INFO o.a.j.m.J.JSR223 预处理程序: 222222 FunTester

Viewing the results in the Listener confirms that the private variable is set correctly.

Thread‑Shared Variables

The approach is identical to the private case, except that the variable is stored in the props object, which is shared across threads.

Groovy script for shared variables:

OUT.println 'FunTester'
log.error '输出JMeter控制台错误'
vars.put("MY1","FunTester")
def my_var = vars.get("MY1")
log.warn "输出参数-------- ${vars} console"
log.info("222222 " + my_var)

props.put("MY",test())
log.info(props.get("MY"))

def test(){
    "funtest" + fan()
}

def fan(){
    System.currentTimeMillis()
}

The script defines two helper methods to illustrate that Groovy functions can be used within JSR223 elements.

Set the appropriate JSR223 PreProcessor parameters.

Inspecting the Result Tree shows the shared variable values and the method outputs.

With these steps, Groovy can effectively manage both private and shared variables in JMeter test plans.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Performance TestingJMeterLoad TestingScriptingGroovyVariable Handling
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.