public class StopWatch extends Object
Conceals use of System.nanoTime()
, 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-concept work and in development, rather than as part of production applications.
As of Spring Framework 5.2, running time is tracked and reported in nanoseconds.
Modifier and Type | Class and Description |
---|---|
static class |
StopWatch.TaskInfo
Nested class to hold data about one task executed within the
StopWatch . |
Constructor and Description |
---|
StopWatch()
Construct a new
StopWatch . |
StopWatch(String id)
Construct a new
StopWatch with the given ID. |
Modifier and Type | Method and Description |
---|---|
String |
currentTaskName()
Get the name of the currently running task, if any.
|
String |
getId()
Get the ID of this
StopWatch , as specified on construction. |
StopWatch.TaskInfo |
getLastTaskInfo()
Get the last task as a
StopWatch.TaskInfo object. |
String |
getLastTaskName()
Get the name of the last task.
|
long |
getLastTaskTimeMillis()
Get the time taken by the last task in milliseconds.
|
long |
getLastTaskTimeNanos()
Get the time taken by the last task in nanoseconds.
|
int |
getTaskCount()
Get the number of tasks timed.
|
StopWatch.TaskInfo[] |
getTaskInfo()
Get an array of the data for tasks performed.
|
long |
getTotalTimeMillis()
Get the total time in milliseconds for all tasks.
|
long |
getTotalTimeNanos()
Get the total time in nanoseconds for all tasks.
|
double |
getTotalTimeSeconds()
Get the total time in seconds for all tasks.
|
boolean |
isRunning()
Determine whether this
StopWatch is currently running. |
String |
prettyPrint()
Generate a string with a table describing all tasks performed.
|
void |
setKeepTaskList(boolean keepTaskList)
Configure whether the
StopWatch.TaskInfo array is built over time. |
String |
shortSummary()
Get a short description of the total running time.
|
void |
start()
Start an unnamed task.
|
void |
start(String taskName)
Start a named task.
|
void |
stop()
Stop the current task.
|
String |
toString()
Generate an informative string describing all tasks performed
|
public StopWatch()
StopWatch
.
Does not start any task.
public StopWatch(String id)
StopWatch
with the given ID.
The ID is handy when we have output from multiple stop watches and need to distinguish between them.
Does not start any task.
id
- identifier for this stop watchpublic String getId()
StopWatch
, as specified on construction.StopWatch(String)
public void setKeepTaskList(boolean keepTaskList)
StopWatch.TaskInfo
array is built over time.
Set this to false
when using a StopWatch
for millions
of intervals; otherwise, the TaskInfo
structure will consume
excessive memory.
Default is true
.
public void start() throws IllegalStateException
The results are undefined if stop()
or timing methods are
called without invoking this method first.
IllegalStateException
start(String)
,
stop()
public void start(String taskName) throws IllegalStateException
The results are undefined if stop()
or timing methods are
called without invoking this method first.
taskName
- the name of the task to startIllegalStateException
start()
,
stop()
public void stop() throws IllegalStateException
The results are undefined if timing methods are called without invoking
at least one pair of start()
/ stop()
methods.
IllegalStateException
start()
,
start(String)
public boolean isRunning()
StopWatch
is currently running.currentTaskName()
@Nullable public String currentTaskName()
isRunning()
public long getLastTaskTimeNanos() throws IllegalStateException
IllegalStateException
getLastTaskTimeMillis()
public long getLastTaskTimeMillis() throws IllegalStateException
IllegalStateException
getLastTaskTimeNanos()
public String getLastTaskName() throws IllegalStateException
IllegalStateException
public StopWatch.TaskInfo getLastTaskInfo() throws IllegalStateException
StopWatch.TaskInfo
object.IllegalStateException
public long getTotalTimeNanos()
getTotalTimeMillis()
,
getTotalTimeSeconds()
public long getTotalTimeMillis()
getTotalTimeNanos()
,
getTotalTimeSeconds()
public double getTotalTimeSeconds()
getTotalTimeNanos()
,
getTotalTimeMillis()
public int getTaskCount()
public StopWatch.TaskInfo[] getTaskInfo()
public String shortSummary()
public String prettyPrint()
For custom reporting, call getTaskInfo()
and use the task info
directly.
public String toString()
For custom reporting, call getTaskInfo()
and use the task info
directly.