public class StopWatch
extends java.lang.Object
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.
Modifier and Type | Class and Description |
---|---|
static class |
StopWatch.TaskInfo
Inner class to hold data about one task executed within the stop watch.
|
Modifier and Type | Field and Description |
---|---|
private java.lang.String |
currentTaskName
Name of the current task
|
private java.lang.String |
id
Identifier of this stop watch.
|
private boolean |
keepTaskList |
private StopWatch.TaskInfo |
lastTaskInfo |
private boolean |
running
Is the stop watch currently running?
|
private long |
startTimeMillis
Start time of the current task
|
private int |
taskCount |
private java.util.List<StopWatch.TaskInfo> |
taskList |
private long |
totalTimeMillis
Total running time
|
Constructor and Description |
---|
StopWatch()
Construct a new stop watch.
|
StopWatch(java.lang.String id)
Construct a new stop watch with the given id.
|
Modifier and Type | Method and Description |
---|---|
java.lang.String |
currentTaskName()
Return the name of the currently running task, if any.
|
java.lang.String |
getId()
Return the id of this stop watch, as specified on construction.
|
StopWatch.TaskInfo |
getLastTaskInfo()
Return the last task as a TaskInfo object.
|
java.lang.String |
getLastTaskName()
Return the name of the last task.
|
long |
getLastTaskTimeMillis()
Return the time taken by the last task.
|
int |
getTaskCount()
Return the number of tasks timed.
|
StopWatch.TaskInfo[] |
getTaskInfo()
Return an array of the data for tasks performed.
|
long |
getTotalTimeMillis()
Return the total time in milliseconds for all tasks.
|
double |
getTotalTimeSeconds()
Return the total time in seconds for all tasks.
|
boolean |
isRunning()
Return whether the stop watch is currently running.
|
java.lang.String |
prettyPrint()
Return a string with a table describing all tasks performed.
|
void |
setKeepTaskList(boolean keepTaskList)
Determine whether the TaskInfo array is built over time.
|
java.lang.String |
shortSummary()
Return a short description of the total running time.
|
void |
start()
Start an unnamed task.
|
void |
start(java.lang.String taskName)
Start a named task.
|
void |
stop()
Stop the current task.
|
java.lang.String |
toString()
Return an informative string describing all tasks performed
For custom reporting, call
getTaskInfo() and use the task info directly. |
private final java.lang.String id
private boolean keepTaskList
private final java.util.List<StopWatch.TaskInfo> taskList
private long startTimeMillis
private boolean running
private java.lang.String currentTaskName
private StopWatch.TaskInfo lastTaskInfo
private int taskCount
private long totalTimeMillis
public StopWatch()
public StopWatch(java.lang.String id)
id
- identifier for this stop watch.
Handy when we have output from multiple stop watches
and need to distinguish between them.public java.lang.String getId()
StopWatch(String)
public void setKeepTaskList(boolean keepTaskList)
public void start() throws java.lang.IllegalStateException
stop()
or timing methods are called without invoking this method.java.lang.IllegalStateException
stop()
public void start(java.lang.String taskName) throws java.lang.IllegalStateException
stop()
or timing methods are called without invoking this method.taskName
- the name of the task to startjava.lang.IllegalStateException
stop()
public void stop() throws java.lang.IllegalStateException
start()
/ stop()
methods.java.lang.IllegalStateException
start()
public boolean isRunning()
currentTaskName()
public java.lang.String currentTaskName()
isRunning()
public long getLastTaskTimeMillis() throws java.lang.IllegalStateException
java.lang.IllegalStateException
public java.lang.String getLastTaskName() throws java.lang.IllegalStateException
java.lang.IllegalStateException
public StopWatch.TaskInfo getLastTaskInfo() throws java.lang.IllegalStateException
java.lang.IllegalStateException
public long getTotalTimeMillis()
public double getTotalTimeSeconds()
public int getTaskCount()
public StopWatch.TaskInfo[] getTaskInfo()
public java.lang.String shortSummary()
public java.lang.String prettyPrint()
public java.lang.String toString()
getTaskInfo()
and use the task info directly.toString
in class java.lang.Object