Package trclib.timer

Class TrcElapsedTimer

java.lang.Object
trclib.timer.TrcElapsedTimer

public class TrcElapsedTimer extends Object
This class implements an elapsed timer to record elapsed time. It is a performance monitoring tool. It is especially important for PID controlled loops that the loops are executed at a high enough frequency or they will oscillate wildly. This class records the elapsed time of some operations in an averaging window queue. To calculate the average elapsed time, all elapsed time values recorded in the averaging window queue are averaged. It also records the min and max elapsed time values it has seen since the last reset.
  • Field Details

    • instanceName

      private final String instanceName
    • averageWindow

      private final long averageWindow
    • elapsedTimeQueue

      private final Queue<Long> elapsedTimeQueue
    • minElapsedTime

      private Long minElapsedTime
    • maxElapsedTime

      private Long maxElapsedTime
    • totalElapsedTime

      private long totalElapsedTime
    • startTime

      private long startTime
  • Constructor Details

    • TrcElapsedTimer

      public TrcElapsedTimer(String instanceName, double averageWindow)
      Constructor: Creates an instance of the object.
      Parameters:
      instanceName - specifies the name to identify this instance of the timer.
      averageWindow - specifies the time window in seconds for averaging elapsed time.
  • Method Details

    • toString

      public String toString()
      This method returns the performance data in string form.
      Overrides:
      toString in class Object
      Returns:
      performance data in string form.
    • reset

      public void reset()
      This method resets the performance data.
    • recordStartTime

      public void recordStartTime()
      This method is called to record the start time.
    • recordEndTime

      public void recordEndTime()
      This method is called to record the elapsed time since the last start time into the elapsed time queue. It also checks if the elapsed time is the minimum or maximum it has seen since last reset.
    • recordPeriodTime

      public void recordPeriodTime()
      This method is called in a loop to have its period time measured. It records a number of loop periods in a queue for a given time window.
    • getLastElapsedTime

      public double getLastElapsedTime()
      This method returns the last recorded elapsed time in seconds.
      Returns:
      last recorded elapsed time in seconds.
    • getAverageElapsedTime

      public double getAverageElapsedTime()
      This method calculates the average elapsed time within the given time window.
      Returns:
      average elapsed time in seconds.
    • getMinElapsedTime

      public double getMinElapsedTime()
      This method returns the minimum elapsed time since the last reset.
      Returns:
      minimum elapsed time in seconds.
    • getMaxElapsedTime

      public double getMaxElapsedTime()
      This method returns the maximum elapsed time since the last reset.
      Returns:
      maximum elapsed time in seconds.
    • printElapsedTime

      public void printElapsedTime(TrcDbgTrace tracer)
      This method prints the elapsed time info using the given tracer.
      Parameters:
      tracer - specifies the tracer to be used to print the info.