org.jrabbit.standard.profiler.util
Class Timer

java.lang.Object
  extended by org.jrabbit.standard.profiler.util.Message
      extended by org.jrabbit.standard.profiler.util.Timer
All Implemented Interfaces:
Referenced

public class Timer
extends Message

Like Counter, Timer extends MessageCounter to provide a particular type of data to a ProfilerEntity. However, instead of recording the number of times a particular event occurs in an interval, a Timer averages the amount of time a particular process takes. Basically, a Timer is structured so that it only really needs to use two methods: begin() and end(). By calling begin() and end(), Timer calculates the amount of time taken between the two calls. Each time this occurs, the Timer fills a spot in the buffer with the recorded time. If the buffer is filled, all of its values are averaged.

Author:
Chris Molini

Field Summary
protected  double averageTime
          The average time recorded, in seconds.
protected  long beginTime
          The time, in nanoseconds, that the current process began at.
protected  long[] buffer
          The buffer of recorded times (in nanoseconds)
protected  int bufferPosition
          The current slot in the buffer that the Timer is filling.
protected  int decimalSize
          The size of the decimal that the message will display.
protected  int resolution
          The number of units per second at which the Timer is to display its average recorded time.
 
Fields inherited from class org.jrabbit.standard.profiler.util.Message
listener, message, reference
 
Constructor Summary
Timer(java.lang.String reference)
          Creates Timer with the indicated identifier.
Timer(java.lang.String reference, int bufferSize)
          Creates a Timer with the indicated identifier and buffer size.
 
Method Summary
 double averageTime()
          Learns the average amount of time that the process has been taking.
 void begin()
          Alerts the Timer that it should start recording the duration of a new process.
 void calculateAverage()
          Calculates the average time of all entries in the buffer, and resets the buffer position back to 0.
 int decimalSize()
          Learns the size of the decimal this Timer uses to display its recorded times.
 void end()
          Alerts the Timer that the end of the currently recorded process has occurred, and that it should update its recorded times.
 int resolution()
          Learns the current resolution of the timer.
 void setBufferSize(int size)
          Resizes the buffer.
 void setDecimalSize(int decimalSize)
          Redefines the size of the decimal this timer uses.
 void setResolution(int resolution)
          Redefines the Timer's resolution.
protected static java.lang.String unitTag(int resolution)
          Learns the tag associated with the supplied resolution.
protected  void updateMessage()
          Updates the message to display the average recorded time.
 
Methods inherited from class org.jrabbit.standard.profiler.util.Message
alertListener, listener, message, reference, setListener, setMessage
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

buffer

protected long[] buffer
The buffer of recorded times (in nanoseconds)


bufferPosition

protected int bufferPosition
The current slot in the buffer that the Timer is filling.


beginTime

protected long beginTime
The time, in nanoseconds, that the current process began at. If the Timer is not in the middle of recording, this is set to -1.


averageTime

protected double averageTime
The average time recorded, in seconds. This value is only ever calculated when the entire buffer is filled.


resolution

protected int resolution
The number of units per second at which the Timer is to display its average recorded time.


decimalSize

protected int decimalSize
The size of the decimal that the message will display.

Constructor Detail

Timer

public Timer(java.lang.String reference)
Creates Timer with the indicated identifier. The Timer uses a buffer of size 60.

Parameters:
reference - The String to identify this Timer.

Timer

public Timer(java.lang.String reference,
             int bufferSize)
Creates a Timer with the indicated identifier and buffer size.

Parameters:
reference - The String to identify this Timer.
bufferSize - The number of times to record before calculating their average.
Method Detail

unitTag

protected static java.lang.String unitTag(int resolution)
Learns the tag associated with the supplied resolution.

Parameters:
resolution - The number of units per second.
Returns:
The sequence of characters that defines the units being dealt with. E.g.: If resolution is 1000 (i.e., the resolution is in milliseconds), the returned String is "ms" to indicate this.

resolution

public int resolution()
Learns the current resolution of the timer.

Returns:
The number of Timer units in a second.

setResolution

public void setResolution(int resolution)
Redefines the Timer's resolution.

Parameters:
resolution - The new number of units in a second to use.

decimalSize

public int decimalSize()
Learns the size of the decimal this Timer uses to display its recorded times.

Returns:
The number of digits after the decimal this Timer displays.

setDecimalSize

public void setDecimalSize(int decimalSize)
Redefines the size of the decimal this timer uses.

Parameters:
decimalSize - The number of digits after the decimal to display.

setBufferSize

public void setBufferSize(int size)
Resizes the buffer. NOTE: This truncates the current buffer. If the buffer is resized to less that the current

Parameters:
size - The number of recordings to make

averageTime

public double averageTime()
Learns the average amount of time that the process has been taking.

Returns:
The average number of seconds that the process being recorded has taken.

begin

public void begin()
Alerts the Timer that it should start recording the duration of a new process. NOTE: If the Timer is in the middle of recording a time, it automatically calls end().


end

public void end()
Alerts the Timer that the end of the currently recorded process has occurred, and that it should update its recorded times. If the entire buffer is filled, the average of all of their values is calculated, and the message is updated to reflect this average.


calculateAverage

public void calculateAverage()
Calculates the average time of all entries in the buffer, and resets the buffer position back to 0.


updateMessage

protected void updateMessage()
Updates the message to display the average recorded time.