Skip to main content

Action Invocation Statistics Plugin

tip

Recommended usage scenarios

  • Production
  • Development stage
  • Performance analysis

Introduction

StatActionInOut is an action invocation statistics plugin. Main focus areas:

  1. Invocation count of each action
  2. Whether the action has time-consuming operations, and how many times they occur
  3. Number of action exceptions

StatActionInOut can be used to collect action invocation metrics, such as execution count, total time, average time, max time, exception count, and more.

Developers can use this data to identify hot methods and slow methods in the project, enabling precise optimization.

Below is a preview of action call information and time-range statistics.

StatAction{cmd[1 - 1], execution[50], exception[0], avgTime[1833], maxTime[2945], totalTime[91691]
requests in 500 ~ 1000 ms: [7]
requests in 1000 ~ 1500 ms: [11]
requests in 1500 ~ 2000 ms: [9]
requests > 2000 ms: [23]

StatAction{cmd[1 - 2], execution[50], exception[0], avgTime[1782], maxTime[2976], totalTime[89133]
requests in 500 ~ 1000 ms: [10]
requests in 1000 ~ 1500 ms: [7]
requests in 1500 ~ 2000 ms: [12]
requests > 2000 ms: [21]

How to Use

BarSkeletonBuilder builder = ...;
var statActionInOut = new StatActionInOut();
builder.addInOut(statActionInOut);

statActionInOut.setListener((statAction, time, flowContext) -> {
System.out.println(statAction);
});

The plugin provides StatActionRegion, StatAction, and StatActionChangeListener for recording, processing, and collecting statistics. With these classes, developers can get all statistics-related information.

  • StatAction: one action statistics record, 1:1 with action. Records action metrics such as execution count, total time, average time, max time, etc.
  • StatActionRegion: statistics region, manages all StatAction records.
  • StatActionChangeListener: listener invoked each time a StatAction record is updated.

StatActionRegion

Developers can obtain a StatActionRegion from the statistics plugin. It provides iteration and other related methods.

  • code 2: statistics region (statistics manager)
  • code 4: iterate all statistics data
StatActionInOut statActionInOut = ...;
var region = statActionInOut.getRegion();

region.forEach((cmdInfo, statAction) -> {
System.out.println(statAction);
// your code, Save data to the DB
});

StatActionChangeListener

  • code 4: set the listener invoked after each StatAction update. time is the execution time of this invocation. If you want to print only when a threshold is exceeded, implement that logic in the listener.
var statActionInOut = new StatActionInOut();
builder.addInOut(statActionInOut);

statActionInOut.setListener((statAction, time, flowContext) -> {
System.out.println(statAction);
});

Summary

StatActionInOut is an action invocation statistics plugin used to collect action metrics, such as execution count, total time, average time, max time, exception count, and more.

Developers can use this data to identify hot methods and slow methods for precise optimization.

The plugin provides StatActionRegion, StatAction, and StatActionChangeListener for recording, processing, and collecting statistics, enabling full access to relevant metrics.

Developers can persist this information to logs or DB for later analysis.

Extend StatActionChangeListener

With the default configuration above, performance analysis needs are covered in most cases. If you also want to view execution-time distribution by time ranges, extend StatActionChangeListener.

StatAction{cmd[1 - 1], execution[50], exception[0], avgTime[1833], maxTime[2945], totalTime[91691]
requests in 500 ~ 1000 ms: [7]
requests in 1000 ~ 1500 ms: [11]
requests in 1500 ~ 2000 ms: [9]
requests > 2000 ms: [23]

StatAction{cmd[1 - 2], execution[50], exception[0], avgTime[1782], maxTime[2976], totalTime[89133]
requests in 500 ~ 1000 ms: [10]
requests in 1000 ~ 1500 ms: [7]
requests in 1500 ~ 2000 ms: [12]
requests > 2000 ms: [21]

Configure time ranges

The following defines two time-range stages:

  1. Requests where action execution time is between 1000 ~ 2000 ms are counted in the first range.
  2. Requests where action execution time is > 2000 ms are counted in the last range.

Each action invocation time is matched against configured TimeRange; if matched, that range counter increments by +1. If finer granularity is required, create more TimeRange entries.

statActionInOut.setListener(new StatActionInOut.StatActionChangeListener() {
...

@Override
public List<StatActionInOut.TimeRange> createTimeRangeList() {
return List.of(
StatActionInOut.TimeRange.create(1000, 2000),
StatActionInOut.TimeRange.create(2000, Long.MAX_VALUE, "> 2000"));
}
});

Default time-range configuration

StatActionChangeListener provides the following default range configuration. If it does not meet requirements, override createTimeRangeList.

interface StatActionChangeListener {
...
default List<TimeRange> createTimeRangeList() {
return List.of(
TimeRange.create(500, 1000),
TimeRange.create(1000, 1500),
TimeRange.create(1500, 2000),
TimeRange.create(2000, Long.MAX_VALUE, "> 2000"));
}
}