Operations 8 min read

How to Build a Thread‑Safe Global Counter in JMeter with Groovy

This guide explains how to create a globally unique, auto‑incrementing variable in JMeter by using a synchronized Groovy script that leverages the built‑in props object, demonstrates setting an initial value, and compares locked versus unlocked execution results.

FunTester
FunTester
FunTester
How to Build a Thread‑Safe Global Counter in JMeter with Groovy

Goal

Create a globally unique, incrementing variable in JMeter that can be accessed by all threads during a test run.

Approach

Use a Java‑level lock on JMeter's built‑in props object to guarantee that only one thread updates the counter at a time.

Test Plan

Create a simple Thread Group with a single HTTP Request (or any sampler).

Add a JSR223 PreProcessor to the request. The script will run before each sampler execution.

JMeter thread group setup
JMeter thread group setup
Add JSR223 PreProcessor
Add JSR223 PreProcessor

Groovy Script (Thread‑safe version)

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

def test() {
    synchronized (props) {
        // Optional delay for demonstration: Thread.sleep(1000)
        int i = props.get("MY") as Integer
        log.info(i + "")
        return i + 1
    }
}

The props object lives in the JVM for the lifetime of the JMeter process, so any value stored in it persists across all threads until the test ends.

Initialize Counter

// Set an initial value before the test starts
props.put("MY", "999")
Set initial value
Set initial value

Execution Logs

Without locking (race condition)

2020-07-19 19:29:50,187 INFO o.a.j.t.JMeterThread: Thread started: Thread Group 1-3
2020-07-19 19:29:50,191 INFO o.a.j.t.JMeterThread: Thread started: Thread Group 1-4
... (additional log lines) ...
2020-07-19 19:29:50,241 INFO o.a.j.t.JMeterThread: Thread finished: Thread Group 1-3

With locking (strictly increasing sequence)

2020-07-19 19:28:40,988 INFO o.a.j.t.JMeterThread: Thread started: Thread Group 1-5
2020-07-19 19:28:41,015 INFO o.a.j.m.J.JSR223 变量: 999
2020-07-19 19:28:41,017 INFO o.a.j.t.JMeterThread: Thread is done: Thread Group 1-1
2020-07-19 19:28:41,031 INFO o.a.j.m.J.JSR223 变量: 1000
2020-07-19 19:28:41,041 INFO o.a.j.t.JMeterThread: Thread is done: Thread Group 1-2
2020-07-19 19:28:41,044 INFO o.a.j.m.J.JSR223 变量: 1001
2020-07-19 19:28:41,046 INFO o.a.j.m.J.JSR223 变量: 1002
2020-07-19 19:28:41,049 INFO o.a.j.m.J.JSR223 变量: 1003
... (additional log lines) ...

Conclusion

Synchronizing on the props object provides a simple, reliable way to maintain a global counter that is safe for concurrent access in JMeter. By setting an initial value before the test, the counter starts from a known state, and no external Java code or custom classes are required.

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 TestingJMeterthread safetyGroovyGlobal Variable
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.