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.
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, 平均耗时: 101Conclusion
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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
