How to Add Real‑Time TPS and Latency Display to a Java Performance Test Engine

Implementing real‑time statistics in a Java performance testing engine involves adding TPS and average latency counters, creating a controllable monitoring thread, using LongAdder for high‑concurrency metrics, and gracefully shutting down the thread, enabling testers to instantly observe system load and detect bottlenecks.

FunTester
FunTester
FunTester
How to Add Real‑Time TPS and Latency Display to a Java Performance Test Engine

Background

In performance testing, seeing metrics such as TPS and average response time as the test runs is essential for quickly spotting bottlenecks and preventing damage to the target system.

Core Requirements

Real‑time TPS and average latency displayed continuously.

Asynchronous monitoring thread that does not interfere with the main test workload.

Graceful shutdown of the monitoring thread after the test finishes.

Implementation Steps

Add two LongAdder fields to accumulate total response time and total request count.

Introduce a boolean flag realTimeKey to control the monitoring thread.

In TaskExecutor.start(), create a dedicated thread that sleeps one second, reads and resets the LongAdder values, computes average latency, and prints “实时统计TPS: …, 平均耗时: …”.

In each ThreadTask.run(), after each request record the elapsed time with TaskExecutor.realTimeCostTime.add(delay) and increment the request counter with TaskExecutor.realTimeCostTimes.add(1).

When the test ends, set realTimeKey = false and join the monitoring thread.

Key Code Snippets

public class TaskExecutor {
    public static LongAdder realTimeCostTime = new LongAdder();
    public static LongAdder realTimeCostTimes = new LongAdder();
    public boolean realTimeKey = true;

    public void start() throws InterruptedException {
        Thread realTimeThread = new Thread(() -> {
            while (realTimeKey) {
                ThreadTool.sleep(1000);
                long sumCost = realTimeCostTime.sumThenReset();
                long sumTimes = realTimeCostTimes.sumThenReset();
                System.out.println(String.format(
                    "实时统计TPS: %d, 平均耗时: %d",
                    sumTimes,
                    sumTimes == 0 ? 0 : sumCost / sumTimes));
            }
        });
        realTimeThread.start();
        // … launch test tasks …
        realTimeKey = false;
        realTimeThread.join();
    }
}

Console Output Example

实时统计TPS: 9, 平均耗时: 101
实时统计TPS: 10, 平均耗时: 102
实时统计TPS: 20, 平均耗时: 102
实时统计TPS: 19, 平均耗时: 102
Rump-Up结束, 开始执行测试任务!
实时统计TPS: 19, 平均耗时: 102
实时统计TPS: 20, 平均耗时: 103
实时统计TPS: 20, 平均耗时: 101

Conclusion

By adding the real‑time display feature, the performance test engine provides immediate visibility into system behavior, allowing testers to react promptly to performance issues and complete testing more efficiently.

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 Testingreal-time monitoringmultithreadinglongadder
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.