|
| 1 | +/* |
| 2 | +* |
| 3 | +* Copyright 2013 Netflix, Inc. |
| 4 | +* |
| 5 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +* you may not use this file except in compliance with the License. |
| 7 | +* You may obtain a copy of the License at |
| 8 | +* |
| 9 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +* |
| 11 | +* Unless required by applicable law or agreed to in writing, software |
| 12 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +* See the License for the specific language governing permissions and |
| 15 | +* limitations under the License. |
| 16 | +* |
| 17 | +*/ |
| 18 | +package com.netflix.stats.distribution; |
| 19 | + |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.concurrent.locks.Lock; |
| 22 | +import java.util.concurrent.locks.ReentrantLock; |
| 23 | + |
| 24 | + |
| 25 | +/** |
| 26 | + * A fixed-size data collection buffer that holds a sliding window |
| 27 | + * of the most recent values added. |
| 28 | + * The {@code DataBuffer} is also a {@link Distribution} and so collects |
| 29 | + * basic statistics about the data added to the buffer. |
| 30 | + * This statistical data is managed on-the-fly, and reflects all the data |
| 31 | + * added, if those values that may have been dropped due to buffer overflow. |
| 32 | + * <p> |
| 33 | + * This class is <em>not</em> synchronized, but can instead managed by a |
| 34 | + * {@link Lock} attached to the {@code DataBuffer} (see {@link #getLock}). |
| 35 | + * @author netflixoss |
| 36 | + */ |
| 37 | +public class DataBuffer extends Distribution { |
| 38 | + |
| 39 | + private final Lock lock; |
| 40 | + private final double[] buf; |
| 41 | + private long startMillis; |
| 42 | + private long endMillis; |
| 43 | + private int size; |
| 44 | + private int insertPos; |
| 45 | + |
| 46 | + /* |
| 47 | + * Constructors |
| 48 | + */ |
| 49 | + |
| 50 | + /** |
| 51 | + * Creates a new {@code DataBuffer} with a given capacity. |
| 52 | + */ |
| 53 | + public DataBuffer(int capacity) { |
| 54 | + lock = new ReentrantLock(); |
| 55 | + buf = new double[capacity]; |
| 56 | + startMillis = 0; |
| 57 | + size = 0; |
| 58 | + insertPos = 0; |
| 59 | + } |
| 60 | + |
| 61 | + /* |
| 62 | + * Accessors |
| 63 | + */ |
| 64 | + |
| 65 | + /** |
| 66 | + * Gets the {@link Lock} to use to manage access to the |
| 67 | + * contents of the {@code DataBuffer}. |
| 68 | + */ |
| 69 | + public Lock getLock() { |
| 70 | + return lock; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Gets the capacity of the {@code DataBuffer}; that is, |
| 75 | + * the maximum number of values that the {@code DataBuffer} can hold. |
| 76 | + */ |
| 77 | + public int getCapacity() { |
| 78 | + return buf.length; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Gets the length of time over which the data was collected, |
| 83 | + * in milliseconds. |
| 84 | + * The value is only valid after {@link #endCollection} |
| 85 | + * has been called (and before a subsequent call to {@link #startCollection}). |
| 86 | + */ |
| 87 | + public long getSampleIntervalMillis() { |
| 88 | + return (endMillis - startMillis); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Gets the number of values currently held in the buffer. |
| 93 | + * This value may be smaller than the value of {@link #getNumValues} |
| 94 | + * depending on how the percentile values were computed. |
| 95 | + */ |
| 96 | + public int getSampleSize() { |
| 97 | + return size; |
| 98 | + } |
| 99 | + |
| 100 | + /* |
| 101 | + * Managing the data |
| 102 | + */ |
| 103 | + |
| 104 | + /** {@inheritDoc} */ |
| 105 | + @Override |
| 106 | + public void clear() { |
| 107 | + super.clear(); |
| 108 | + startMillis = 0; |
| 109 | + size = 0; |
| 110 | + insertPos = 0; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Notifies the buffer that data is collection is now enabled. |
| 115 | + */ |
| 116 | + public void startCollection() { |
| 117 | + clear(); |
| 118 | + startMillis = System.currentTimeMillis(); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Notifies the buffer that data has just ended. |
| 123 | + * <p> |
| 124 | + * <b>Performance Note:</b> |
| 125 | + * <br>This method sorts the underlying data buffer, |
| 126 | + * and so may be slow. It is best to call this at most once |
| 127 | + * and fetch all percentile values desired, instead of making |
| 128 | + * a number of repeated calls. |
| 129 | + */ |
| 130 | + public void endCollection() { |
| 131 | + endMillis = System.currentTimeMillis(); |
| 132 | + Arrays.sort(buf, 0, size); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * {@inheritDoc} |
| 137 | + * <p> |
| 138 | + * The buffer wraps-around if it is full, overwriting the oldest |
| 139 | + * entry with the new value. |
| 140 | + */ |
| 141 | + @Override |
| 142 | + public void noteValue(double val) { |
| 143 | + super.noteValue(val); |
| 144 | + buf[insertPos++] = val; |
| 145 | + if (insertPos >= buf.length) { |
| 146 | + insertPos = 0; |
| 147 | + size = buf.length; |
| 148 | + } else if (insertPos > size) { |
| 149 | + size = insertPos; |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Gets the requested percentile statistics. |
| 155 | + * |
| 156 | + * @param percents array of percentile values to compute, |
| 157 | + * which must be in the range {@code [0 .. 100]} |
| 158 | + * @param percentiles array to fill in with the percentile values; |
| 159 | + * must be the same length as {@code percents} |
| 160 | + * @return the {@code percentiles} array |
| 161 | + * @see <a href="http://en.wikipedia.org/wiki/Percentile">Percentile (Wikipedia)</a> |
| 162 | + * @see <a href="http://cnx.org/content/m10805/latest/">Percentile</a> |
| 163 | + */ |
| 164 | + public double[] getPercentiles(double[] percents, double[] percentiles) { |
| 165 | + for (int i = 0; i < percents.length; i++) { |
| 166 | + percentiles[i] = computePercentile(percents[i]); |
| 167 | + } |
| 168 | + return percentiles; |
| 169 | + } |
| 170 | + |
| 171 | + private double computePercentile(double percent) { |
| 172 | + // Some just-in-case edge cases |
| 173 | + if (size <= 0) { |
| 174 | + return 0.0; |
| 175 | + } else if (percent <= 0.0) { |
| 176 | + return buf[0]; |
| 177 | + } else if (percent >= 100.0) { // SUPPRESS CHECKSTYLE MagicNumber |
| 178 | + return buf[size - 1]; |
| 179 | + } |
| 180 | + /* |
| 181 | + * Note: |
| 182 | + * Documents like http://cnx.org/content/m10805/latest |
| 183 | + * use a one-based ranking, while this code uses a zero-based index, |
| 184 | + * so the code may not look exactly like the formulas. |
| 185 | + */ |
| 186 | + double index = (percent / 100.0) * size; // SUPPRESS CHECKSTYLE MagicNumber |
| 187 | + int iLow = (int) Math.floor(index); |
| 188 | + int iHigh = (int) Math.ceil(index); |
| 189 | + assert 0 <= iLow && iLow <= index && index <= iHigh && iHigh <= size; |
| 190 | + assert (iHigh - iLow) <= 1; |
| 191 | + if (iHigh >= size) { |
| 192 | + // Another edge case |
| 193 | + return buf[size - 1]; |
| 194 | + } else if (iLow == iHigh) { |
| 195 | + return buf[iLow]; |
| 196 | + } else { |
| 197 | + // Interpolate between the two bounding values |
| 198 | + return buf[iLow] + (index - iLow) * (buf[iHigh] - buf[iLow]); |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | +} // DataBuffer |
0 commit comments