public class StopWatch
extends java.lang.Object
This class requires external synchronization if used from different threads.
About system clock issues and the JVM, see https://blogs.oracle.com/dholmes/entry/inside_the_hotspot_vm_clocks
Over the years also other open source libs have developed similar classes, but as of ACS 12.2 we do not see a need to replace our StopWatch or change its API. See http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/base/Stopwatch.html, http://jamonapi.sourceforge.net/, http://commons.apache.org/proper/commons-lang/javadocs/api-release/index.html?org/apache/commons/lang3/time/StopWatch.html
| Constructor and Description |
|---|
StopWatch()
Ctor without logger provided.
|
StopWatch(java.util.logging.Logger logger)
Constructor that resets the time count.
|
| Modifier and Type | Method and Description |
|---|---|
StopWatch |
createStopWatchForSubtask(java.lang.String newSubtaskName)
Creates a StopWatch for a subtask.
|
java.lang.String |
formattedMillis()
Converts
getLapTimeNanos() to fractional milliseconds,
using the format from millisecFormatter. |
long |
getLapTimeMillis()
Gets the time in milliseconds that has elapsed since this object was created
or
reset() was called. |
long |
getLapTimeNanos()
Gets the time in nanoseconds that has elapsed since this object was created
or
reset() was called. |
java.util.Date |
getStartTime()
Returns the time when this StopWatch was created / started.
|
java.lang.String |
getSubtaskDetails()
Gets the subtask timing data as a string.
|
void |
logLapTime(java.lang.String taskDesc)
Logs a message about the elapsed time for a certain task.
|
void |
logLapTime(java.lang.String taskDesc,
java.util.logging.Level logLevel)
Variant of
logLapTime(String) that allows the user to specify
the log level. |
void |
logLapTimeWithSubtaskDetails(java.lang.String taskDesc,
java.util.logging.Level logLevel) |
void |
reset() |
void |
setLogger(java.util.logging.Logger logger) |
void |
stop()
Stops the StopWatch.
|
public StopWatch()
setLogger later, or logs will be printed to stdout.public StopWatch(java.util.logging.Logger logger)
logger - the logger to be used in logLapTime(String). If null, logs will be printed to
stdout.public void setLogger(java.util.logging.Logger logger)
public void reset()
public StopWatch createStopWatchForSubtask(java.lang.String newSubtaskName)
stop() on the subtask's StopWatch.
Do not expect that subtask elapsed times add up exactly to the total elapsed time. The system-dependent timing granularity does not allow this.
public java.util.Date getStartTime()
public void stop()
createStopWatchForSubtask(String)).
It is optional in case of top-level StopWatches where it makes little difference whether or not
we first stop the watch right before retrieving or logging the lap time.
This method can be called more than once without (side) effects.
public long getLapTimeMillis()
reset() was called.
The implementation relies on System.nanoTime(),
so that the granularity of measurements is OS dependent and can be more than one ms.
Also, note that the time spent on all threads together is returned, which might
be a poor measure for runtime profiling of a particular method.
public java.lang.String formattedMillis()
getLapTimeNanos() to fractional milliseconds,
using the format from millisecFormatter.public long getLapTimeNanos()
reset() was called.
The implementation simply relies on System.nanoTime(),
so that the granularity of measurements is OS dependent.
Also, note that the time spent on all threads together is returned, which might
be a poor measure for runtime profiling a particular method.
public void logLapTime(java.lang.String taskDesc)
Level.FINE and inserts the supplied description:
"elapsed time in ms to taskDescription: elapsed-time".
If no logger has been supplied, it will get one using Logger.getLogger(java.lang.String).
This ad-hoc logger will likely not work in an ACS environment where log handlers are configured
for the needs of containers and remote logging.
todo: provide nicer text mask for message
taskDesc - the message to be include in the standard message
which should describe the task for which the lap time is taken.getLapTimeMillis()public void logLapTime(java.lang.String taskDesc,
java.util.logging.Level logLevel)
logLapTime(String) that allows the user to specify
the log level. Generally the FINE (=DEBUG) level is appropriate for performance logs,
but in special cases such as unit tests it can be necessary to log at INFO or other levels.public void logLapTimeWithSubtaskDetails(java.lang.String taskDesc,
java.util.logging.Level logLevel)
public java.lang.String getSubtaskDetails()
logLapTimeWithSubtaskDetails(String, Level).