StatisticsAccumulatorpublic class StatisticsAccumulator extends Object
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.,
this.unit = unit;
sampleCount = 0;
sampleSum = 0;
sampleSquareSum = 0;
|
Methods Summary |
---|
void | clearState()Clears the samples and starts fresh on new samples.
min = Double.MAX_VALUE;
max = Double.MIN_VALUE;
sampleCount = 0;
sampleSum = 0;
sampleSquareSum = 0;
| protected double | computeAverage()If users choose to custom format the stats.
return (sampleSum / sampleCount);
| protected double | computeStandardDeviation()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.String | getValue()Computes the Standard Statistic Results based on the samples collected
so far and provides the complete value as a formatted String
return toString();
| public void | sample(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.
///////////////////////////////////////
// operations
sampleCount++;
if( value < min ) min = value;
if( value > max) max = value;
sampleSum += value;
sampleSquareSum += (value * value);
| public java.lang.String | toString()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 void | unitTestValidate(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);
}
|
|