Do Thread‑Safe Classes and synchronized Really Slow Down Performance Tests?

This article examines how adding Java thread‑safe classes and the synchronized keyword influences QPS in performance testing, presenting code modifications, benchmark results for 20 and 40 threads, and concluding that their impact on measured throughput is minimal.

FunTester
FunTester
FunTester
Do Thread‑Safe Classes and synchronized Really Slow Down Performance Tests?

Part 1 – Locking Resources

In complex performance‑testing scenarios we often rely on thread‑safe utilities. When a suitable class exists we use the JDK’s built‑in thread‑safe types; otherwise we implement safety with synchronized and static initialization.

We added a static AtomicInteger field to count total requests and modified the run() method to increment it inside the test loop.

@Override
void run() {
    times.times {
        def start = Time.getTimeStamp()
        sleep(0.1)
        100.times {
            fun.getAndIncrement()
        }
        def end = Time.getTimeStamp()
        excutetimes.getAndIncrement()
        costs.add(end - start)
    }
    countDownLatch.countDown()
}

Running 20 threads with 50 iterations produced the following QPS values:

INFO-> 通过平均时间计算QPS:191.4535150865
INFO-> 通过总时间计算QPS:189.7173211914
INFO-> 误差是:0.91%

Without the thread‑safe class (baseline) the results were:

INFO-> 通过平均时间计算QPS:192.8287006238
INFO-> 通过总时间计算QPS:191.1314984709
INFO-> 误差是:0.88%

The QPS difference is negligible, confirming that the overhead of the safety class is far lower than the service under test.

Increasing the thread count to 40 (still 50 iterations) yielded:

With safety class: QPS avg 385.09, total 377.93, error 1.86%
Baseline: QPS avg 388.42, total 379.65, error 2.26%

Again, the impact remains small.

Part 2 – synchronized Keyword

We introduced a static integer field and a synchronized method to increment it:

/**
 * Thread‑safe method
 */
private static void funplus() {
    synchronized (fun) {
        fun++
    }
}

An equivalent version using a synchronized static method is also shown.

private synchronized static void funplus() {
    fun++
}

The test loop now calls funplus() instead of the atomic increment. Results for 20 threads, 50 requests:

INFO-> QPS avg 191.07, total 185.49, error 2.92%

For 40 threads, 50 requests:

INFO-> QPS avg 379.61, total 372.93, error 1.76%

Increasing requests to 100 (40 threads) gave:

INFO-> QPS avg 382.99, total 379.04, error 1.03%

These numbers show that using synchronized introduces a slightly larger QPS deviation than the atomic class, but the overall error remains under 3 % and is insignificant compared with other sources of measurement variance.

Conclusion: Both Java thread‑safe classes and the synchronized keyword have a minimal effect on performance‑test QPS, allowing testers to use them without worrying about substantial measurement distortion.

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.

JavaPerformance TestingSynchronizationthread safetyBenchmarkQPS
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.