Business Thread Monitoring Plugin
Recommended usage scenarios
- Production
- Development stage
- Performance analysis
Introduction
ThreadMonitorInOut is a business thread monitoring plugin. Main focus areas:
- Action execution count per business thread
- Average execution time
- Current queued task count in each business thread (remaining unexecuted tasks, i.e., actions)
Developers can use these metrics to analyze per-thread workload consumption, scale hardware resources up/down accordingly, and adjust thread-related strategies based on these insights.
Print Preview
From the preview, all thread information is printed. There are 8 threads in total (the number varies by machine CPU). Based on average thread processing time, developers can roughly estimate current game health.
Assume one thread has an average action execution time of 100 ms and still has 50 queued tasks,
then finishing the remaining tasks would take about 100 * 50 = 5000 ms.
With this plugin, we can get monitoring data and set thresholds. When thresholds are exceeded, notifications can be triggered for relevant developers (with SMS, email, etc.).
Business thread[User-8-1] executed 1 task, avg time 1 ms, 91 tasks remaining
Business thread[User-8-2] executed 1 task, avg time 1 ms, 0 tasks remaining
Business thread[User-8-3] executed 1 task, avg time 1 ms, 36 tasks remaining
Business thread[User-8-4] executed 1 task, avg time 1 ms, 0 tasks remaining
Business thread[User-8-5] executed 1 task, avg time 1 ms, 88 tasks remaining
Business thread[User-8-6] executed 1 task, avg time 1 ms, 0 tasks remaining
Business thread[User-8-7] executed 7 tasks, avg time 1 ms, 56 tasks remaining
Business thread[User-8-8] executed 1 task, avg time 1 ms, 0 tasks remaining
How to Use
BarSkeletonBuilder builder = ...;
builder.addInOut(new ThreadMonitorInOut());
Comprehensive Example
Through ThreadMonitorRegion, monitoring information can be accessed.
In production, console printing is usually unnecessary,
but these metrics can be periodically synced to logs, DB, or other systems through scheduled tasks.
The example below checks thread metrics every 2 minutes and triggers alerts when a configured dangerous threshold is exceeded.
Code notes
- code 10: executed task count per thread
- code 11: average execution time
- code 12: current remaining task count
- code 13: thread name
- code 14: total execution time
- code 16: when exceeding threshold, notification can be triggered for relevant developers
var threadMonitorInOut = new ThreadMonitorInOut();
builder.addInOut(threadMonitorInOut);
ThreadMonitorInOut.ThreadMonitorRegion region = threadMonitorInOut.getRegion();
ExecutorKit.newSingleScheduled("Scheduled").scheduleAtFixedRate(() -> {
System.out.println(region);
region.forEach(threadMonitor -> {
LongAdder executeCount = threadMonitor.executeCount();
long avgTime = threadMonitor.getAvgTime();
int countRemaining = threadMonitor.countRemaining();
String name = threadMonitor.name();
LongAdder totalTime = threadMonitor.totalTime();
long dangerous = 8000;
if (avgTime * countRemaining > dangerous) {
// your code
}
});
}, 2, 2, TimeUnit.MINUTES);