Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Written by Gil Tene of Azul Systems, and released to the public domain, as explained at http://creativecommons.org/publicdomain/zero/1.0/

Author(s):
Gil Tene
  
  
  package org.HdrHistogram;
  
Provides iteration capabilities for a histogram's data set, as well as access to common statistics information.
 
 public class HistogramData {
     final AbstractHistogram histogram;
     // Create instances of the needed iterators once. We will reset them on each use to avoid allocation:
     final int bucketCount;
     final int subBucketCount;
 
     HistogramData(final AbstractHistogram histogram) {
         this. = histogram;
         this. = new PercentileIterator(histogram, 1);
         this. = new RecordedValuesIterator(histogram);
         this. = histogram.bucketCount;
         this. = histogram.subBucketCount;
     }

    
The the total count of recorded values in the histogram data

Returns:
the total recorded value count in the (corrected) histogram data
 
     public long getTotalCount() {
         return .getTotalCount();
     }

    
Get the lowest recorded value level in the histogram

Returns:
the Min value recorded in the histogram
 
     public long getMinValue() {
         .reset();
         long min = 0;
         if (.hasNext()) {
             HistogramIterationValue iterationValue = .next();
             min = iterationValue.getValueIteratedTo();
         }
         return .lowestEquivalentValue(min);
     }
    
Get the highest recorded value level in the histogram

Returns:
the Max value recorded in the histogram
 
     public long getMaxValue() {
         .reset();
         long max = 0;
         while (.hasNext()) {
             HistogramIterationValue iterationValue = .next();
             max = iterationValue.getValueIteratedTo();
         }
         return .lowestEquivalentValue(max);
     }

    
Get the computed mean value of all recorded values in the histogram

Returns:
the mean value (in value units) of the histogram data
 
     public double getMean() {
         .reset();
         long totalValue = 0;
         while (.hasNext()) {
             HistogramIterationValue iterationValue = .next();
             totalValue = iterationValue.getTotalValueToThisValue();
         }
         return (totalValue * 1.0) / getTotalCount();
     }

    
Get the computed standard deviation of all recorded values in the histogram

Returns:
the standard deviation (in value units) of the histogram data
 
     public double getStdDeviation() {
         double mean =  getMean();
         double geometric_deviation_total = 0.0;
         .reset();
         while (.hasNext()) {
             HistogramIterationValue iterationValue = .next();
             Double deviation = (.medianEquivalentValue(iterationValue.getValueIteratedTo()) * 1.0) - mean;
            geometric_deviation_total += (deviation * deviation) * iterationValue.getCountAddedInThisIterationStep();
        }
        double std_deviation = Math.sqrt(geometric_deviation_total / getTotalCount());
        return std_deviation;
    }

    
Get the value at a given percentile

Parameters:
percentile The percentile for which the return the associated value
Returns:
The value below which a given percentage of the overall recorded value entries in the histogram all fall.
    public long getValueAtPercentile(final double percentile) {
        double requestedPercentile = Math.min(percentile, 100.0); // Truncate down to 100%
        long countAtPercentile = (long)(((requestedPercentile / 100.0) * getTotalCount()) + 0.5); // round to nearest
        countAtPercentile = Math.max(countAtPercentile, 1); // Make sure we at least reach the first recorded entry
        long totalToCurrentIJ = 0;
        for (int i = 0; i < i++) {
            int j = (i == 0) ? 0 : ( / 2);
            for (; j < j++) {
                totalToCurrentIJ += .getCountAt(ij);
                if (totalToCurrentIJ >= countAtPercentile) {
                    long valueAtIndex = .valueFromIndex(ij);
                    return valueAtIndex;
                }
            }
        }
        throw new ArrayIndexOutOfBoundsException("percentile value not found in range"); // should not reach here.
    }

    
Get the percentile at a given value

Parameters:
value The value for which the return the associated percentile
Returns:
The percentile of values recorded at or below the given percentage in the histogram all fall.
    public double getPercentileAtOrBelowValue(final long value) {
        long totalToCurrentIJ = 0;
        int targetBucketIndex = .getBucketIndex(value);
        int targetSubBucketIndex = .getSubBucketIndex(valuetargetBucketIndex);
        if (targetBucketIndex >= )
            return 100.0;
        for (int i = 0; i <= targetBucketIndexi++) {
            int j = (i == 0) ? 0 : ( / 2);
            int subBucketCap = (i == targetBucketIndex) ? (targetSubBucketIndex + 1): ;
            for (; j < subBucketCapj++) {
                totalToCurrentIJ += .getCountAt(ij);
            }
        }
        return (100.0 * totalToCurrentIJ) / getTotalCount();    }

    
Get the count of recorded values within a range of value levels. (inclusive to within the histogram's resolution)

Parameters:
lowValue The lower value bound on the range for which to provide the recorded count. Will be rounded down with lowestEquivalentValue.
highValue The higher value bound on the range for which to provide the recorded count. Will be rounded up with highestEquivalentValue.
Returns:
the total count of values recorded in the histogram within the value range that is >= lowestEquivalentValue(lowValue) and <= highestEquivalentValue(highValue)
Throws:
java.lang.ArrayIndexOutOfBoundsException
    public long getCountBetweenValues(final long lowValuefinal long highValuethrows ArrayIndexOutOfBoundsException {
        long count = 0;
        // Compute the sub-bucket-rounded values for low and high:
        int lowBucketIndex = .getBucketIndex(lowValue);
        int lowSubBucketIndex = .getSubBucketIndex(lowValuelowBucketIndex);
        long valueAtlowValue = .valueFromIndex(lowBucketIndexlowSubBucketIndex);
        int highBucketIndex = .getBucketIndex(highValue);
        int highSubBucketIndex = .getSubBucketIndex(highValuehighBucketIndex);
        long valueAtHighValue = .valueFromIndex(highBucketIndexhighSubBucketIndex);
        if ((lowBucketIndex >= ) || (highBucketIndex >= ))
            throw new ArrayIndexOutOfBoundsException();
        for (int i = lowBucketIndexi <= highBucketIndexi++) {
            int j = (i == 0) ? 0 : ( / 2);
            for (; j < j++) {
                long valueAtIndex = .valueFromIndex(ij);
                if (valueAtIndex > valueAtHighValue)
                    return count;
                if (valueAtIndex >= valueAtlowValue)
                    count += .getCountAt(ij);
            }
        }
        return count;
    }

    
Get the count of recorded values at a specific value

Parameters:
value The value for which to provide the recorded count
Returns:
The total count of values recorded in the histogram at the given value (to within the histogram resolution at the value level).
Throws:
java.lang.ArrayIndexOutOfBoundsException
    public long getCountAtValue(final long valuethrows ArrayIndexOutOfBoundsException {
        int bucketIndex = .getBucketIndex(value);
        int subBucketIndex = .getSubBucketIndex(valuebucketIndex);
        // May throw ArrayIndexOutOfBoundsException:
        return .getCountAt(bucketIndexsubBucketIndex);
    }

    
Provide a means of iterating through histogram values according to percentile levels. The iteration is performed in steps that start at 0% and reduce their distance to 100% according to the percentileTicksPerHalfDistance parameter, ultimately reaching 100% when all recorded histogram values are exhausted.

Parameters:
percentileTicksPerHalfDistance The number of iteration steps per half-distance to 100%.
Returns:
An java.lang.Iterable<HistogramIterationValue> through the histogram using a PercentileIterator
    public Percentiles percentiles(final int percentileTicksPerHalfDistance) {
        return new Percentiles(percentileTicksPerHalfDistance);
    }

    
Provide a means of iterating through histogram values using linear steps. The iteration is performed in steps of valueUnitsPerBucket in size, terminating when all recorded histogram values are exhausted.

Parameters:
valueUnitsPerBucket The size (in value units) of the linear buckets to use
Returns:
An java.lang.Iterable<HistogramIterationValue> through the histogram using a LinearIterator
    public LinearBucketValues linearBucketValues(final int valueUnitsPerBucket) {
        return new LinearBucketValues(valueUnitsPerBucket);
    }

    
Provide a means of iterating through histogram values at logarithmically increasing levels. The iteration is performed in steps that start at valueUnitsInFirstBucket and increase exponentially according to logBase, terminating when all recorded histogram values are exhausted.

Parameters:
valueUnitsInFirstBucket The size (in value units) of the first bucket in the iteration
logBase The multiplier by which bucket sizes will grow in eahc iteration step
Returns:
An java.lang.Iterable<HistogramIterationValue> through the histogram using a LogarithmicIterator
    public LogarithmicBucketValues logarithmicBucketValues(final int valueUnitsInFirstBucketfinal double logBase) {
        return new LogarithmicBucketValues(valueUnitsInFirstBucketlogBase);
    }

    
Provide a means of iterating through all recorded histogram values using the finest granularity steps supported by the underlying representation. The iteration steps through all non-zero recorded value counts, and terminates when all recorded histogram values are exhausted.

Returns:
An java.lang.Iterable<HistogramIterationValue> through the histogram using a RecordedValuesIterator
    public RecordedValues recordedValues() {
        return new RecordedValues();
    }

    
Provide a means of iterating through all histogram values using the finest granularity steps supported by the underlying representation. The iteration steps through all possible unit value levels, regardless of whether or not there were recorded values for that value level, and terminates when all recorded histogram values are exhausted.

Returns:
An java.lang.Iterable<HistogramIterationValue> through the histogram using a RecordedValuesIterator
    public AllValues allValues() {
        return new AllValues();
    }

    
Produce textual representation of the value distribution of histogram data by percentile. The distribution is output with exponentially increasing resolution, with each exponentially decreasing half-distance containing five (5) percentile reporting tick points.

Parameters:
printStream Stream into which the distribution will be output

outputValueUnitScalingRatio The scaling factor by which to divide histogram recorded values units in output
    public void outputPercentileDistribution(final PrintStream printStream,
                                             final Double outputValueUnitScalingRatio) {
        outputPercentileDistribution(printStream, 5, outputValueUnitScalingRatio);
    }

    
Produce textual representation of the value distribution of histogram data by percentile. The distribution is output with exponentially increasing resolution, with each exponentially decreasing half-distance containing dumpTicksPerHalf percentile reporting tick points.

Parameters:
printStream Stream into which the distribution will be output

percentileTicksPerHalfDistance The number of reporting points per exponentially decreasing half-distance

outputValueUnitScalingRatio The scaling factor by which to divide histogram recorded values units in output
    public void outputPercentileDistribution(final PrintStream printStream,
                                             final int percentileTicksPerHalfDistance,
                                             final Double outputValueUnitScalingRatio) {
        outputPercentileDistribution(printStreampercentileTicksPerHalfDistanceoutputValueUnitScalingRatiofalse);
    }

    
Produce textual representation of the value distribution of histogram data by percentile. The distribution is output with exponentially increasing resolution, with each exponentially decreasing half-distance containing dumpTicksPerHalf percentile reporting tick points.

Parameters:
printStream Stream into which the distribution will be output

percentileTicksPerHalfDistance The number of reporting points per exponentially decreasing half-distance

outputValueUnitScalingRatio The scaling factor by which to divide histogram recorded values units in output
useCsvFormat Output in CSV format if true. Otherwise use plain text form.
    public void outputPercentileDistribution(final PrintStream printStream,
                                             final int percentileTicksPerHalfDistance,
                                             final Double outputValueUnitScalingRatio,
                                             boolean useCsvFormat) {
        if (useCsvFormat) {
            printStream.format("\"Value\",\"Percentile\",\"TotalCount\",\"1/(1-Percentile)\"\n");
        } else {
            printStream.format("%12s %14s %10s %14s\n\n""Value""Percentile""TotalCount""1/(1-Percentile)");
        }
        PercentileIterator iterator = ;
        iterator.reset(percentileTicksPerHalfDistance);
        String percentileFormatString;
        String lastLinePercentileFormatString;
        if (useCsvFormat) {
            percentileFormatString = "%." + . + "f,%.12f,%d,%.2f\n";
            lastLinePercentileFormatString = "%." + . + "f,%.12f,%d,Infinity\n";
        } else {
            percentileFormatString = "%12." + . + "f %2.12f %10d %14.2f\n";
            lastLinePercentileFormatString = "%12." + . + "f %2.12f %10d\n";
        }
        try {
            while (iterator.hasNext()) {
                HistogramIterationValue iterationValue = iterator.next();
                if (iterationValue.getPercentileLevelIteratedTo() != 100.0D) {
                    printStream.format(.percentileFormatString,
                            iterationValue.getValueIteratedTo() / outputValueUnitScalingRatioiterationValue.getPercentileLevelIteratedTo()/100.0D,
                            iterationValue.getTotalCountToThisValue(),
                            1/(1.0D - (iterationValue.getPercentileLevelIteratedTo()/100.0D)) );
                } else {
                    printStream.format(.lastLinePercentileFormatString,
                            iterationValue.getValueIteratedTo() / outputValueUnitScalingRatioiterationValue.getPercentileLevelIteratedTo()/100.0D,
                            iterationValue.getTotalCountToThisValue());
                }
            }
            if (!useCsvFormat) {
                // Calculate and output mean and std. deviation.
                // Note: mean/std. deviation numbers are very often completely irrelevant when
                // data is extremely non-normal in distribution (e.g. in cases of strong multi-modal
                // response time distribution associated with GC pauses). However, reporting these numbers
                // can be very useful for contrasting with the detailed percentile distribution
                // reported by outputPercentileDistribution(). It is not at all surprising to find
                // percentile distributions where results fall many tens or even hundreds of standard
                // deviations away from the mean - such results simply indicate that the data sampled
                // exhibits a very non-normal distribution, highlighting situations for which the std.
                // deviation metric is a useless indicator.
                //
                double mean =  getMean() / outputValueUnitScalingRatio;
                double std_deviation = getStdDeviation() / outputValueUnitScalingRatio;
                printStream.format(.,
                        "#[Mean    = %12." + . + "f, StdDeviation   = %12." +
                                . +"f]\n",
                        meanstd_deviation);
                printStream.format(.,
                        "#[Max     = %12." + . + "f, Total count    = %12d]\n",
                        getMaxValue() / outputValueUnitScalingRatiogetTotalCount());
                printStream.format(."#[Buckets = %12d, SubBuckets     = %12d]\n",
                        ..);
            }
        } catch (ArrayIndexOutOfBoundsException e) {
            // Overflow conditions on histograms can lead to ArrayIndexOutOfBoundsException on iterations:
            if (.hasOverflowed()) {
                printStream.format(."# Histogram counts indicate OVERFLOW values");
            } else {
                // Re-throw if reason is not a known overflow:
                throw e;
            }
        }
    }
    // Percentile iterator support:

    
    public class Percentiles implements Iterable<HistogramIterationValue> {
        final AbstractHistogram histogram;
        final int percentileTicksPerHalfDistance;
        private Percentiles(final AbstractHistogram histogramfinal int percentileTicksPerHalfDistance) {
            this. = histogram;
            this. = percentileTicksPerHalfDistance;
        }

        
        public Iterator<HistogramIterationValueiterator() {
            return new PercentileIterator();
        }
    }
    // Linear iterator support:

    
An java.lang.Iterable<HistogramIterationValue> through the histogram using a LinearIterator
    public class LinearBucketValues implements Iterable<HistogramIterationValue> {
        final AbstractHistogram histogram;
        final int valueUnitsPerBucket;
        private LinearBucketValues(final AbstractHistogram histogramfinal int valueUnitsPerBucket) {
            this. = histogram;
            this. = valueUnitsPerBucket;
        }

        
        public Iterator<HistogramIterationValueiterator() {
            return new LinearIterator();
        }
    }
    // Logarithmic iterator support:

    
    public class LogarithmicBucketValues implements Iterable<HistogramIterationValue> {
        final AbstractHistogram histogram;
        final int valueUnitsInFirstBucket;
        final double logBase;
        private LogarithmicBucketValues(final AbstractHistogram histogram,
                                        final int valueUnitsInFirstBucketfinal double logBase) {
            this. = histogram;
            this. = valueUnitsInFirstBucket;
            this. = logBase;
        }

        
        public Iterator<HistogramIterationValueiterator() {
            return new LogarithmicIterator();
        }
    }
    // Recorded value iterator support:

    
    public class RecordedValues implements Iterable<HistogramIterationValue> {
        final AbstractHistogram histogram;
        private RecordedValues(final AbstractHistogram histogram) {
            this. = histogram;
        }

        
        public Iterator<HistogramIterationValueiterator() {
            return new RecordedValuesIterator();
        }
    }
    // AllValues iterator support:

    
    public class AllValues implements Iterable<HistogramIterationValue> {
        final AbstractHistogram histogram;
        private AllValues(final AbstractHistogram histogram) {
            this. = histogram;
        }

        
        public Iterator<HistogramIterationValueiterator() {
            return new AllValuesIterator();
        }
    }
New to GrepCode? Check out our FAQ X