Class TouchTimer

  • All Implemented Interfaces:
    org.apache.logging.log4j.util.StringBuilderFormattable

    public final class TouchTimer
    extends java.lang.Object
    implements org.apache.logging.log4j.util.StringBuilderFormattable
    Handy 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 interface  TouchTimer.Visitor
      Message Visitor interface
    • Constructor Summary

      Constructors 
      Constructor Description
      TouchTimer()
      Default constructor
      TouchTimer​(int maxMessageCount)
      Alternative constructor
      TouchTimer​(long startTimeMillis, long startTimeNanos, int maxMessageCount)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void forEachMessage​(TouchTimer.Visitor visitor)
      Visit all the messages for this TouchTimer
      void formatTo​(java.lang.StringBuilder trace)  
      long getElapsedTimeMillis()
      Return the total time elapsed between the first and last events.
      long getElapsedTimeNanos()
      Return the total time elapsed between the first and last events.
      long getStartTimeMillis()  
      long getStartTimeNanos()  
      java.lang.String toString()  
      void touch​(java.lang.Class<?> klass, java.lang.String name)
      Add an event to this timer.
      void touch​(java.lang.Class<?> klass, java.lang.String name, java.lang.Object... args)
      Add an event to this timer.
      void touch​(java.lang.String name)
      Add an event to this timer.
      void touch​(java.lang.String name, java.lang.Object... args)
      Add an event to this timer.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • 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 name
        klass - 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:
        toString in class java.lang.Object
      • formatTo

        public void formatTo​(java.lang.StringBuilder trace)
        Specified by:
        formatTo in interface org.apache.logging.log4j.util.StringBuilderFormattable
      • forEachMessage

        public void forEachMessage​(@Nonnull
                                   TouchTimer.Visitor visitor)
        Visit all the messages for this TouchTimer
        Parameters:
        visitor - message visitor