Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
  *
  * Copyright 2013 Netflix, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
  * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
 package com.netflix.stats.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 java.util.concurrent.locks.Lock attached to the DataBuffer (see getLock()).

Author(s):
netflixoss
 
 public class DataBuffer extends Distribution {
 
     private final Lock lock;
     private final double[] buf;
     private long startMillis;
     private long endMillis;
     private int size;
     private int insertPos;
 
     /*
      * Constructors
      */

    
Creates a new DataBuffer with a given capacity.
 
     public DataBuffer(int capacity) {
          = new ReentrantLock();
          = new double[capacity];
          = 0;
          = 0;
          = 0;
     }
 
     /*
      * Accessors
      */

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

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

    
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()).
 
     public long getSampleIntervalMillis() {
         return ( - );
     }

    
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.
 
     public int getSampleSize() {
         return ;
     }
 
    /*
     * Managing the data
     */

    
    @Override
    public void clear() {
        super.clear();
         = 0;
         = 0;
         = 0;
    }

    
Notifies the buffer that data is collection is now enabled.
    public void startCollection() {
        clear();
         = System.currentTimeMillis();
    }

    
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.

    public void endCollection() {
         = System.currentTimeMillis();
        Arrays.sort(, 0, );
    }

    

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

    @Override
    public void noteValue(double val) {
        super.noteValue(val);
        [++] = val;
        if ( >= .) {
             = 0;
             = .;
        } else if ( > ) {
             = ;
        }
    }

    
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
    public double[] getPercentiles(double[] percentsdouble[] percentiles) {
        for (int i = 0; i < percents.lengthi++) {
            percentiles[i] = computePercentile(percents[i]);
        }
        return percentiles;
    }
    private double computePercentile(double percent) {
        // Some just-in-case edge cases
        if ( <= 0) {
            return 0.0;
        } else if (percent <= 0.0) {
            return [0];
        } else if (percent >= 100.0) {        // SUPPRESS CHECKSTYLE MagicNumber
            return [ - 1];
        }
        /*
         * Note:
         * Documents like http://cnx.org/content/m10805/latest
         * use a one-based ranking, while this code uses a zero-based index,
         * so the code may not look exactly like the formulas.
         */
        double index = (percent / 100.0) * // SUPPRESS CHECKSTYLE MagicNumber
        int iLow = (int) Math.floor(index);
        int iHigh = (int) Math.ceil(index);
        assert 0 <= iLow && iLow <= index && index <= iHigh && iHigh <= ;
        assert (iHigh - iLow) <= 1;
        if (iHigh >= ) {
            // Another edge case
            return [ - 1];
        } else if (iLow == iHigh) {
            return [iLow];
        } else {
            // Interpolate between the two bounding values
            return [iLow] + (index - iLow) * ([iHigh] - [iLow]);
        }
    }
// DataBuffer
New to GrepCode? Check out our FAQ X