FileDocCategorySizeDatePackage
StatisticsAccumulator.javaAPI DocJava SE 5 API6546Fri Aug 26 14:54:38 BST 2005com.sun.corba.se.spi.monitoring

StatisticsAccumulator

public class StatisticsAccumulator extends Object

author
Hemanth Puttaswamy

StatisticsAccumulator accumulates the samples provided by the user and computes the value of minimum, maximum, sum and sample square sum. When the StatisticMonitoredAttribute calls getValue(), it will compute all the statistics for the collected samples (Which are Minimum, Maximum, Average, StandardDeviation) and provides a nice printable record as a String. Users can easily extend this class and provide the implementation of toString() method to format the stats as desired. By default all the stats are printed in a single line.

Fields Summary
protected double
max
protected double
min
private double
sampleSum
private double
sampleSquareSum
private long
sampleCount
protected String
unit
Constructors Summary
public StatisticsAccumulator(String unit)

Construct the Statistics Accumulator by providing the unit as a String. The examples of units are "Hours", "Minutes", "Seconds", "MilliSeconds", "Micro Seconds" etc.,

return
a StatisticsAccumulator with ...

param
unit a String representing the units for the samples collected

        this.unit = unit;
        sampleCount = 0;
        sampleSum = 0;
        sampleSquareSum = 0;
    
Methods Summary
voidclearState()
Clears the samples and starts fresh on new samples.

        min = Double.MAX_VALUE;
        max = Double.MIN_VALUE;
        sampleCount = 0;
        sampleSum = 0;
        sampleSquareSum = 0;
    
protected doublecomputeAverage()
If users choose to custom format the stats.

        return (sampleSum / sampleCount);
    
protected doublecomputeStandardDeviation()
We use a derived Standard Deviation formula to compute SD. This way there is no need to hold on to all the samples provided. The method is protected to let users extend and format the results.

        double sampleSumSquare = sampleSum * sampleSum;
        return Math.sqrt( 
            (sampleSquareSum-((sampleSumSquare)/sampleCount))/(sampleCount-1));
    
public java.lang.StringgetValue()
Computes the Standard Statistic Results based on the samples collected so far and provides the complete value as a formatted String

        return toString();
    
public voidsample(double value)

User will use this method to just register a sample with the StatisticsAccumulator. This is the only method that User will use to expose the statistics, internally the StatisticMonitoredAttribute will collect the information when requested from the ASAdmin.

param
value a double value to make it more precise




  ///////////////////////////////////////
  // operations



                                                         
                
        sampleCount++;
        if( value < min )  min = value;
        if( value > max) max = value;
        sampleSum += value;
        sampleSquareSum += (value * value);
    
public java.lang.StringtoString()
Users can extend StatisticsAccumulator to provide the complete Stats in the format they prefer, if the default format doesn't suffice.

        return "Minimum Value = " + min + " " + unit + " " +
            "Maximum Value = " + max + " " + unit + " " +
            "Average Value = " + computeAverage() + " " +  unit + " " +
            "Standard Deviation = " + computeStandardDeviation() + " " + unit + 
            " " + "Samples Collected = " + sampleCount;
    
public voidunitTestValidate(java.lang.String expectedUnit, double expectedMin, double expectedMax, long expectedSampleCount, double expectedAverage, double expectedStandardDeviation)
This is an internal API to test StatisticsAccumulator...

        if( !expectedUnit.equals( unit ) ){
            throw new RuntimeException( 
                "Unit is not same as expected Unit" +
                "\nUnit = " + unit + "ExpectedUnit = " + expectedUnit );
        } 
        if( min != expectedMin ) {
            throw new RuntimeException( 
                "Minimum value is not same as expected minimum value" +
                "\nMin Value = " + min + "Expected Min Value = " + expectedMin);
        } 
        if( max != expectedMax ) {
            throw new RuntimeException( 
                "Maximum value is not same as expected maximum value" + 
                "\nMax Value = " + max + "Expected Max Value = " + expectedMax);
        } 
        if( sampleCount != expectedSampleCount ) {
            throw new RuntimeException( 
                "Sample count is not same as expected Sample Count" + 
                "\nSampleCount = " + sampleCount + "Expected Sample Count = " + 
                expectedSampleCount);
        } 
        if( computeAverage() != expectedAverage ) {
            throw new RuntimeException( 
                "Average is not same as expected Average" + 
                "\nAverage = " + computeAverage() + "Expected Average = " + 
                expectedAverage);
        } 
        // We are computing Standard Deviation from two different methods
        // for comparison. So, the values will not be the exact same to the last
        // few digits. So, we are taking the difference and making sure that
        // the difference is not greater than 1.
        double difference = Math.abs(
            computeStandardDeviation() - expectedStandardDeviation);
        if( difference > 1 ) {
            throw new RuntimeException( 
                "Standard Deviation is not same as expected Std Deviation" + 
                "\nStandard Dev = " + computeStandardDeviation() + 
                "Expected Standard Dev = " + expectedStandardDeviation);
        }