Spring timer StopWatch


Introduction to StopWatch

The StopWatch class is a tool class used to measure code execution time in the Spring framework. It provides a series of properties to record monitoring information.

This article is based on spring-boot-starter-web:2.2.2RELEASE version.

source code:

public class StopWatch {
    
    
    private final String id;
    private boolean keepTaskList = true;
    private final List<StopWatch.TaskInfo> taskList = new LinkedList<>();
    private long startTimeMillis;
    @Nullable
    private String currentTaskName;
    @Nullable
    private StopWatch.TaskInfo lastTaskInfo;
    private int taskCount;
    private long totalTimeMillis;

	......方法略
	public static final class TaskInfo {
    
    

		private final String taskName;
	
		private final long timeNanos;
	}
}

Detailed StopWatch attribute

Attributes describe
id Indicates the unique identifier of the StopWatch object, which can be passed in through the constructor.
keepTaskList Indicates whether to keep all recorded monitoring tasks, the default is true.
If the value is true, the program can obtain detailed information of all monitoring tasks through the getTaskInfo() method; otherwise, only the detailed information of the last monitoring task can be obtained.
taskList It is used to record all the monitoring task information. The task information is recorded only when the keepTaskList attribute value is true. This value adds a task information when the stop() method is called.
currentTaskName Indicates the name of the monitoring task currently being executed, and this value is set when the start() method is called.
lastTaskInfo Indicates the detailed information of the last monitoring task executed, including task name, execution time, etc. This value is set when the stop() method is called.
taskCount Indicates the number of monitoring tasks to be executed, and the value is +1 when the stop() method is called.
totalTimeMillis Indicates the total execution time of all monitoring tasks recorded by the StopWatch object, in milliseconds, the value when the stop() method is called + the execution time of the current task.
TaskInfo.taskName mission name.
TaskInfo.timeNanos Task execution time.

Detailed explanation of StopWatch method

method describe
void setKeepTaskList(boolean keepTaskList) Set whether to keep all recorded monitoring tasks, the default is true.
If the value is true, the program can obtain detailed information of all monitoring tasks through the getTaskInfo() method.
void start() Start a new monitoring task, you can set the task name. Record the current time and task name.
void stop() End the current monitoring task. Record the task execution time, update the total execution time and the number of tasks.
String getId() Get a unique identifier.
String currentTaskName() Get the name of the currently running monitoring task.
TaskInfo getLastTaskInfo() Obtain the detailed information of the last executed monitoring task, including the task name, start time, end time, and execution time, etc.
String getLastTaskName() Get the task name of the last monitoring task executed
long getLastTaskTimeMillis() Get the execution time of the last monitoring task executed, in milliseconds
long getLastTaskTimeNanos() Get the execution time of the last monitoring task executed, in nanoseconds
int getTaskCount() Get the number of monitoring tasks executed
TaskInfo[] getTaskInfo() Get all monitoring task information, only valid when the keepTaskList property is true.
double getTotalTimeSeconds() Get the total execution time of all monitoring tasks, in seconds.
long getTotalTimeMillis() Get the total execution time of all monitoring tasks, in milliseconds.
long getTotalTimeNanos() Get the total execution time of all monitoring tasks, in nanoseconds.
boolean isRunning() Determine whether the current StopWatch object is performing monitoring tasks.
String shortSummary() Output brief information of all monitoring tasks in string form, including task number and total execution time.
String prettyPrint() Output the execution time and details of all monitoring tasks as a string.

Example of using StopWatch

    public static void main(String[] args) throws InterruptedException {
    
    
        StopWatch stopWatch = new StopWatch();

        stopWatch.start("task1");
        System.out.println("当前任务名称:"+stopWatch.currentTaskName());
        Thread.sleep(1000);
        stopWatch.stop();
        System.out.println("task1耗时毫秒:"+stopWatch.getLastTaskTimeMillis());

        stopWatch.start("task2");
        System.out.println("当前任务名称:"+stopWatch.currentTaskName());
        Thread.sleep(2000);
        stopWatch.stop();
        System.out.println("task2耗时毫秒:"+stopWatch.getLastTaskTimeMillis());

        System.out.println("总任务数:"+stopWatch.getTaskCount());
        System.out.println("总耗时毫秒:"+stopWatch.getTotalTimeMillis());
        System.out.println("所有任务简要信息:\n"+stopWatch.shortSummary());
        System.out.println("所有任务详细信息:\n"+stopWatch.prettyPrint());
    }

output:

当前任务名称:task1
task1耗时毫秒:1013
当前任务名称:task2
task2耗时毫秒:2014
总任务数:2
总耗时毫秒:3027
所有任务简要信息:
StopWatch '': running time = 3027555400 ns
所有任务详细信息:
StopWatch '': running time = 3027555400 ns
---------------------------------------------
ns         %     Task name
---------------------------------------------
1013462900  033%  task1
2014092500  067%  task2

Guess you like

Origin blog.csdn.net/JokerLJG/article/details/129010224