Package com.acanx.utils.incubator.time
Class StopWatch
java.lang.Object
com.acanx.utils.incubator.time.StopWatch
ACANX-Utils / com.acanx.utils / StopWatch.java
文件由 ACANX 创建于 2019/7/26 - 17:37
Description StopWatch: Spring框架工具类
Simple stop watch, allowing for timing of a number of tasks,
exposing total running time and running time for each named task.
Conceals use of System.currentTimeMillis()
, improving the
readability of application code and reducing the likelihood of calculation errors.
Note that this object is not designed to be thread-safe and does not use synchronization.
This class is normally used to verify performance during proof-of-concepts and in development, rather than as part of production applications.
- Since:
- 0.0.1.10 May 2, 2001
- Author:
- Rod Johnson, Juergen Hoeller, Sam Brannen
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final class
Inner class to hold data about one task executed within the stop watch. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionReturn the name of the currently running task, if any.getId()
Return the id of this stop watch, as specified on construction.Return the last task as a TaskInfo object.Return the name of the last task.long
Return the time taken by the last task.int
Return the number of tasks timed.Return an array of the data for tasks performed.long
Return the total time in milliseconds for all tasks.double
Return the total time in seconds for all tasks.boolean
Return whether the stop watch is currently running.Return a string with a table describing all tasks performed.void
setKeepTaskList
(boolean keepTaskList) Determine whether the TaskInfo array is built over time.Return a short description of the total running time.void
start()
Start an unnamed task.void
Start a named task.void
stop()
Stop the current task.toString()
Return an informative string describing all tasks performed For custom reporting, callgetTaskInfo()
and use the task info directly.
-
Constructor Details
-
StopWatch
public StopWatch()Construct a new stop watch. Does not start any task. -
StopWatch
Construct a new stop watch with the given id. Does not start any task.- Parameters:
id
- identifier for this stop watch. Handy when we have output from multiple stop watches and need to distinguish between them.
-
-
Method Details
-
getId
Return the id of this stop watch, as specified on construction.- Returns:
- the id (empty String by default)
- Since:
- 4.2.2
- See Also:
-
setKeepTaskList
public void setKeepTaskList(boolean keepTaskList) Determine whether the TaskInfo array is built over time. Set this to "false" when using a StopWatch for millions of intervals, or the task info structure will consume excessive memory. Default is "true".- Parameters:
keepTaskList
- keepTaskList
-
start
Start an unnamed task. The results are undefined ifstop()
or timing methods are called without invoking this method.- Throws:
IllegalStateException
- See Also:
-
start
Start a named task. The results are undefined ifstop()
or timing methods are called without invoking this method.- Parameters:
taskName
- the name of the task to start- Throws:
IllegalStateException
- See Also:
-
stop
Stop the current task. The results are undefined if timing methods are called without invoking at least one pairstart()
/stop()
methods.- Throws:
IllegalStateException
- IllegalStateException- See Also:
-
isRunning
public boolean isRunning()Return whether the stop watch is currently running.- Returns:
- 结果
- See Also:
-
currentTaskName
Return the name of the currently running task, if any.- Returns:
- 结果
- Since:
- 4.2.2
- See Also:
-
getLastTaskTimeMillis
Return the time taken by the last task.- Returns:
- 结果
- Throws:
IllegalStateException
- IllegalStateException
-
getLastTaskName
Return the name of the last task.- Returns:
- 结果
- Throws:
IllegalStateException
- IllegalStateException
-
getLastTaskInfo
Return the last task as a TaskInfo object.- Returns:
- 结果
- Throws:
IllegalStateException
- IllegalStateException
-
getTotalTimeMillis
public long getTotalTimeMillis()Return the total time in milliseconds for all tasks.- Returns:
- 结果
-
getTotalTimeSeconds
public double getTotalTimeSeconds()Return the total time in seconds for all tasks.- Returns:
- 结果
-
getTaskCount
public int getTaskCount()Return the number of tasks timed.- Returns:
- 结果
-
getTaskInfo
Return an array of the data for tasks performed.- Returns:
- 结果
-
shortSummary
Return a short description of the total running time.- Returns:
- 结果
-
prettyPrint
Return a string with a table describing all tasks performed. For custom reporting, call getTaskInfo() and use the task info directly.- Returns:
- 结果
-
toString
Return an informative string describing all tasks performed For custom reporting, callgetTaskInfo()
and use the task info directly.
-