1 7 package com.sun.corba.se.spi.monitoring; 8 9 import java.util.*; 10 11 29 public class StatisticsAccumulator { 30 31 34 35 protected double max = Double.MIN_VALUE; 37 38 protected double min = Double.MAX_VALUE; 40 41 private double sampleSum; 42 43 private double sampleSquareSum; 44 45 private long sampleCount; 46 47 protected String unit; 48 49 50 51 54 55 56 71 public void sample(double value) { 72 sampleCount++; 73 if( value < min ) min = value; 74 if( value > max) max = value; 75 sampleSum += value; 76 sampleSquareSum += (value * value); 77 } 79 80 81 85 public String getValue( ) { 86 return toString(); 87 } 88 89 93 public String toString( ) { 94 return "Minimum Value = " + min + " " + unit + " " + 95 "Maximum Value = " + max + " " + unit + " " + 96 "Average Value = " + computeAverage() + " " + unit + " " + 97 "Standard Deviation = " + computeStandardDeviation() + " " + unit + 98 " " + "Samples Collected = " + sampleCount; 99 } 100 101 104 protected double computeAverage( ) { 105 return (sampleSum / sampleCount); 106 } 107 108 109 115 protected double computeStandardDeviation( ) { 116 double sampleSumSquare = sampleSum * sampleSum; 117 return Math.sqrt( 118 (sampleSquareSum-((sampleSumSquare)/sampleCount))/(sampleCount-1)); 119 } 120 121 136 public StatisticsAccumulator( String unit ) { 137 this.unit = unit; 138 sampleCount = 0; 139 sampleSum = 0; 140 sampleSquareSum = 0; 141 } 142 143 144 147 void clearState( ) { 148 min = Double.MAX_VALUE; 149 max = Double.MIN_VALUE; 150 sampleCount = 0; 151 sampleSum = 0; 152 sampleSquareSum = 0; 153 } 154 155 158 public void unitTestValidate( String expectedUnit, double expectedMin, 159 double expectedMax, long expectedSampleCount, double expectedAverage, 160 double expectedStandardDeviation ) 161 { 162 if( !expectedUnit.equals( unit ) ){ 163 throw new RuntimeException ( 164 "Unit is not same as expected Unit" + 165 "\nUnit = " + unit + "ExpectedUnit = " + expectedUnit ); 166 } 167 if( min != expectedMin ) { 168 throw new RuntimeException ( 169 "Minimum value is not same as expected minimum value" + 170 "\nMin Value = " + min + "Expected Min Value = " + expectedMin); 171 } 172 if( max != expectedMax ) { 173 throw new RuntimeException ( 174 "Maximum value is not same as expected maximum value" + 175 "\nMax Value = " + max + "Expected Max Value = " + expectedMax); 176 } 177 if( sampleCount != expectedSampleCount ) { 178 throw new RuntimeException ( 179 "Sample count is not same as expected Sample Count" + 180 "\nSampleCount = " + sampleCount + "Expected Sample Count = " + 181 expectedSampleCount); 182 } 183 if( computeAverage() != expectedAverage ) { 184 throw new RuntimeException ( 185 "Average is not same as expected Average" + 186 "\nAverage = " + computeAverage() + "Expected Average = " + 187 expectedAverage); 188 } 189 double difference = Math.abs( 194 computeStandardDeviation() - expectedStandardDeviation); 195 if( difference > 1 ) { 196 throw new RuntimeException ( 197 "Standard Deviation is not same as expected Std Deviation" + 198 "\nStandard Dev = " + computeStandardDeviation() + 199 "Expected Standard Dev = " + expectedStandardDeviation); 200 } 201 } 202 203 204 } 206 207 208 | Popular Tags |