com.netflix.stats.distribution
Class DataBuffer

java.lang.Object
  extended by com.netflix.stats.distribution.Distribution
      extended by com.netflix.stats.distribution.DataBuffer
All Implemented Interfaces:
DataCollector, DistributionMBean

public class DataBuffer
extends Distribution

A fixed-size data collection buffer that holds a sliding window of the most recent values added. The DataBuffer is also a Distribution and so collects basic statistics about the data added to the buffer. This statistical data is managed on-the-fly, and reflects all the data added, if those values that may have been dropped due to buffer overflow.

This class is not synchronized, but can instead managed by a Lock attached to the DataBuffer (see getLock()).


Constructor Summary
DataBuffer(int capacity)
          Creates a new DataBuffer with a given capacity.
 
Method Summary
 void clear()
          Clears out the distribution, resetting it to its initial state.
 void endCollection()
          Notifies the buffer that data has just ended.
 int getCapacity()
          Gets the capacity of the DataBuffer; that is, the maximum number of values that the DataBuffer can hold.
 java.util.concurrent.locks.Lock getLock()
          Gets the Lock to use to manage access to the contents of the DataBuffer.
 double[] getPercentiles(double[] percents, double[] percentiles)
          Gets the requested percentile statistics.
 long getSampleIntervalMillis()
          Gets the length of time over which the data was collected, in milliseconds.
 int getSampleSize()
          Gets the number of values currently held in the buffer.
 void noteValue(double val)
          Adds a value to the collected data.
 void startCollection()
          Notifies the buffer that data is collection is now enabled.
 
Methods inherited from class com.netflix.stats.distribution.Distribution
add, getMaximum, getMean, getMinimum, getNumValues, getStdDev, getVariance, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

DataBuffer

public DataBuffer(int capacity)
Creates a new DataBuffer with a given capacity.

Method Detail

getLock

public java.util.concurrent.locks.Lock getLock()
Gets the Lock to use to manage access to the contents of the DataBuffer.


getCapacity

public int getCapacity()
Gets the capacity of the DataBuffer; that is, the maximum number of values that the DataBuffer can hold.


getSampleIntervalMillis

public long getSampleIntervalMillis()
Gets the length of time over which the data was collected, in milliseconds. The value is only valid after endCollection() has been called (and before a subsequent call to startCollection()).


getSampleSize

public int getSampleSize()
Gets the number of values currently held in the buffer. This value may be smaller than the value of Distribution.getNumValues() depending on how the percentile values were computed.


clear

public void clear()
Clears out the distribution, resetting it to its initial state.

Specified by:
clear in interface DistributionMBean
Overrides:
clear in class Distribution

startCollection

public void startCollection()
Notifies the buffer that data is collection is now enabled.


endCollection

public void endCollection()
Notifies the buffer that data has just ended.

Performance Note:
This method sorts the underlying data buffer, and so may be slow. It is best to call this at most once and fetch all percentile values desired, instead of making a number of repeated calls.


noteValue

public void noteValue(double val)
Adds a value to the collected data. This must run very quickly, and so can safely be called in time-critical code.

The buffer wraps-around if it is full, overwriting the oldest entry with the new value.

Specified by:
noteValue in interface DataCollector
Overrides:
noteValue in class Distribution

getPercentiles

public double[] getPercentiles(double[] percents,
                               double[] percentiles)
Gets the requested percentile statistics.

Parameters:
percents - array of percentile values to compute, which must be in the range [0 .. 100]
percentiles - array to fill in with the percentile values; must be the same length as percents
Returns:
the percentiles array
See Also:
Percentile (Wikipedia), Percentile