public class Stopwatch extends 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.
|
| Constructor and Description |
|---|
Stopwatch()
Construct a new stop watch.
|
Stopwatch(String id)
Construct a new stop watch with the given id.
|
| Modifier and Type | Method and Description |
|---|---|
String |
currentTaskName()
Return the name of the currently running task, if any.
|
void |
flip(String taskName,
Object... args)
Stops a current running time measurement and start a new one with given
taskName description. |
String |
getId()
Return the id of this stop watch, as specified on construction.
|
Stopwatch.TaskInfo |
getLastTaskInfo()
Return the last task as a TaskInfo object.
|
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.
|
String |
prettyPrint()
Return a string with a table describing all tasks performed.
|
String |
prettyPrintAggregations() |
void |
setKeepTaskList(boolean keepTaskList)
Determine whether the TaskInfo array is built over time.
|
String |
shortSummary()
Return a short description of the total running time.
|
void |
start()
Start an unnamed task.
|
void |
start(String taskName,
Object... args)
Start a named task.
|
void |
stop()
Stop the current task.
|
String |
toString()
Return an informative string describing all tasks performed For custom reporting, call
getTaskInfo() and use the
task info directly. |
public Stopwatch()
public Stopwatch(String id)
id - identifier for this stop watch. Handy when we have output from multiple stop watches and need to distinguish
between them.public String getId()
#StopWatch(String)public void setKeepTaskList(boolean keepTaskList)
public void start()
throws IllegalStateException
stop() or timing methods are called without invoking this
method.IllegalStateExceptionstop()public void start(String taskName, Object... args) throws IllegalStateException
stop() or timing methods are called without invoking this method.taskName - A string, which support the String.format(String, Object...) formattingargs - optional parameters for the stringIllegalStateExceptionstop()public void flip(String taskName, Object... args)
taskName description.taskName - A string, which support the String.format(String, Object...) formattingargs - optional parameters for the stringpublic void stop()
throws IllegalStateException
#start() / #stop() methods.IllegalStateExceptionstart()public boolean isRunning()
currentTaskName()public String currentTaskName()
isRunning()public long getLastTaskTimeMillis()
throws IllegalStateException
IllegalStateExceptionpublic String getLastTaskName() throws IllegalStateException
IllegalStateExceptionpublic Stopwatch.TaskInfo getLastTaskInfo() throws IllegalStateException
IllegalStateExceptionpublic long getTotalTimeMillis()
public double getTotalTimeSeconds()
public int getTaskCount()
public Stopwatch.TaskInfo[] getTaskInfo()
public String shortSummary()
public String prettyPrint()
public String prettyPrintAggregations()
Copyright © 2020 LeanIX GmbH. All rights reserved.