|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.perf4j.StopWatch
public class StopWatch
The StopWatch class is used to time code blocks in Perf4J. The general usage pattern is to create a StopWatch before a section of code that is to be timed and then stop it before it is passed to a logging method:
StopWatch stopWatch = new StopWatch(); try { ...code being timed... log.info(stopWatch.stop("methodBeingTimed.success")); } catch (Exception e) { log.error(stopWatch.stop("methodBeingTimed.fail"), e); }Note that a StopWatch is reusable. That is, you can call start() and stop() in succession and the getElapsedTime() method will refer to the time since the most recent start() call. In general, most clients will find it simpler and cleaner to use the
LoggingStopWatch
class or one of its
subclasses in preference to this class.
Field Summary | |
---|---|
static String |
DEFAULT_LOGGER_NAME
|
Constructor Summary | |
---|---|
StopWatch()
Creates a StopWatch with a blank tag, no message and started at the instant of creation. |
|
StopWatch(long startTime,
long elapsedTime,
String tag,
String message)
Creates a StopWatch with a specified start and elapsed time, tag, and message. |
|
StopWatch(String tag)
Creates a StopWatch with the specified tag, no message and started at the instant of creation. |
|
StopWatch(String tag,
String message)
Creates a StopWatch with the specified tag and message, started an the instant of creation. |
Method Summary | |
---|---|
StopWatch |
clone()
|
boolean |
equals(Object o)
|
long |
getElapsedTime()
Gets the time in milliseconds between when this StopWatch was last started and stopped. |
String |
getMessage()
Gets any additional message that was set on this StopWatch instance. |
long |
getStartTime()
Gets the time when this instance was created, or when one of the start() messages was last called. |
String |
getTag()
Gets the tag used to group this StopWatch instance with other instances used to time the same code block. |
int |
hashCode()
|
String |
lap(String tag)
The lap method is useful when using a single StopWatch to time multiple consecutive blocks. |
String |
lap(String tag,
String message)
The lap method is useful when using a single StopWatch to time multiple consecutive blocks. |
void |
setMessage(String message)
Sends a message on this StopWatch instance to be printed when this instance is logged. |
void |
setTag(String tag)
Sets the grouping tag for this StopWatch instance. |
void |
start()
Starts this StopWatch, which sets its startTime property to the current time and resets the elapsedTime property. |
void |
start(String tag)
Starts this StopWatch and sets its tag to the specified value. |
void |
start(String tag,
String message)
Starts this StopWatch and sets its tag and message to the specified values. |
String |
stop()
Stops this StopWatch, which "freezes" its elapsed time. |
String |
stop(String tag)
Stops this StopWatch and sets its grouping tag. |
String |
stop(String tag,
String message)
Stops this StopWatch and sets its grouping tag and message. |
String |
toString()
|
Methods inherited from class java.lang.Object |
---|
finalize, getClass, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
public static final String DEFAULT_LOGGER_NAME
Constructor Detail |
---|
public StopWatch()
public StopWatch(String tag)
tag
- The tag name for this timing call. Tags are used to group timing logs, thus each block of code being
timed should have a unique tag. Note that tags can take a hierarchical format using dot notation.public StopWatch(String tag, String message)
tag
- The tag name for this timing call. Tags are used to group timing logs, thus each block of code
being timed should have a unique tag. Note that tags can take a hierarchical format using dot
notation.message
- Additional text to be printed with the logging statement of this StopWatch.public StopWatch(long startTime, long elapsedTime, String tag, String message)
startTime
- The start time in millisecondselapsedTime
- The elapsed time in millisecondstag
- The tag used to group timing logs of the same code blockmessage
- Additional message textMethod Detail |
---|
public long getStartTime()
public long getElapsedTime()
public String getTag()
public void setTag(String tag)
tag
- The grouping tag.public String getMessage()
public void setMessage(String message)
message
- The message associated with this StopWatch, which may be null.public void start()
public void start(String tag)
tag
- The grouping tag for this StopWatchpublic void start(String tag, String message)
tag
- The grouping tag for this StopWatchmessage
- A descriptive message about the code being timed, may be nullpublic String stop()
public String stop(String tag)
tag
- The grouping tag for this StopWatch
public String stop(String tag, String message)
tag
- The grouping tag for this StopWatchmessage
- A descriptive message about the code being timed, may be null
public String lap(String tag)
StopWatch stopWatch = new StopWatch(); ...some code log.info(stopWatch.lap("block1")); ...some more code log.info(stopWatch.lap("block2")); ...even more code log.info(stopWatch.stop("block3"));
tag
- The grouping tag to use for the execution block that was just stopped.
public String lap(String tag, String message)
StopWatch stopWatch = new StopWatch(); ...some code log.info(stopWatch.lap("block1", "message about block 1")); ...some more code log.info(stopWatch.lap("block2", "message about block 2")); ...even more code log.info(stopWatch.stop("block3", "message about block 3"));
tag
- The grouping tag to use for the execution block that was just stopped.message
- A descriptive message about the code being timed, may be null
public String toString()
toString
in class Object
public StopWatch clone()
clone
in class Object
public boolean equals(Object o)
equals
in class Object
public int hashCode()
hashCode
in class Object
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |