Package com.linkedin.alpini.base.misc
Class TouchTimer
- java.lang.Object
-
- com.linkedin.alpini.base.misc.TouchTimer
-
- All Implemented Interfaces:
org.apache.logging.log4j.util.StringBuilderFormattable
public final class TouchTimer extends java.lang.Object implements org.apache.logging.log4j.util.StringBuilderFormattableHandy utility class to track the timing while processing a request. A class can create a TouchTimer, then "touch" it at various points during processing. At the end, if the request took a long time, it can log the output. For example:void doSomething() { TouchTimer timer = new TouchTimer(); timer.touch("Calling fooService"); fooService.doSomething(); timer.touch("Calling barService"); barService.doSomething(); timer.touch("All Done!"); if(timer.getElapsedTimeMillis() > 1000) { log.warn("doSomething took over a second to complete! " + timer); } }The block of code tracks the time to call each service. If the total time spent was greater than 1 second, it logs a message. The logged message will include timing for each "touch". The timer is thread safe, and output from TouchTimer.toString() will include the thread name along with each touch.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceTouchTimer.VisitorMessage Visitor interface
-
Field Summary
Fields Modifier and Type Field Description static intDEFAULT_MAX_MESSAGESDefault maximum number of messages allowed on one timer.static java.lang.StringMAX_MESSAGES_PROPERTY
-
Constructor Summary
Constructors Constructor Description TouchTimer()Default constructorTouchTimer(int maxMessageCount)Alternative constructorTouchTimer(long startTimeMillis, long startTimeNanos, int maxMessageCount)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidforEachMessage(TouchTimer.Visitor visitor)Visit all the messages for this TouchTimervoidformatTo(java.lang.StringBuilder trace)longgetElapsedTimeMillis()Return the total time elapsed between the first and last events.longgetElapsedTimeNanos()Return the total time elapsed between the first and last events.longgetStartTimeMillis()longgetStartTimeNanos()java.lang.StringtoString()voidtouch(java.lang.Class<?> klass, java.lang.String name)Add an event to this timer.voidtouch(java.lang.Class<?> klass, java.lang.String name, java.lang.Object... args)Add an event to this timer.voidtouch(java.lang.String name)Add an event to this timer.voidtouch(java.lang.String name, java.lang.Object... args)Add an event to this timer.
-
-
-
Field Detail
-
DEFAULT_MAX_MESSAGES
public static final int DEFAULT_MAX_MESSAGES
Default maximum number of messages allowed on one timer. This is a safety to prevent a timer from growing without bounds if someone has it in a ThreadLocal or a static or something like that.
-
MAX_MESSAGES_PROPERTY
public static final java.lang.String MAX_MESSAGES_PROPERTY
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
TouchTimer
public TouchTimer()
Default constructor
-
TouchTimer
public TouchTimer(@Nonnegative int maxMessageCount)Alternative constructor- Parameters:
maxMessageCount- Maximum number of messages
-
TouchTimer
public TouchTimer(@Nonnegative long startTimeMillis, @Nonnegative long startTimeNanos, @Nonnegative int maxMessageCount)
-
-
Method Detail
-
getStartTimeMillis
public long getStartTimeMillis()
-
getStartTimeNanos
public long getStartTimeNanos()
-
touch
public void touch(java.lang.Class<?> klass, java.lang.String name, java.lang.Object... args)Add an event to this timer.- Parameters:
name- event nameklass- class generating the event
-
touch
public void touch(java.lang.Class<?> klass, java.lang.String name)Add an event to this timer.- Parameters:
name- event name
-
touch
public void touch(java.lang.String name, java.lang.Object... args)Add an event to this timer.- Parameters:
name- event name
-
touch
public void touch(java.lang.String name)
Add an event to this timer.- Parameters:
name- event name
-
getElapsedTimeMillis
public long getElapsedTimeMillis()
Return the total time elapsed between the first and last events.- Returns:
- time in milliseconds
-
getElapsedTimeNanos
public long getElapsedTimeNanos()
Return the total time elapsed between the first and last events.- Returns:
- time in nanoseconds
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
formatTo
public void formatTo(java.lang.StringBuilder trace)
- Specified by:
formatToin interfaceorg.apache.logging.log4j.util.StringBuilderFormattable
-
forEachMessage
public void forEachMessage(@Nonnull TouchTimer.Visitor visitor)Visit all the messages for this TouchTimer- Parameters:
visitor- message visitor
-
-