KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > spi > monitoring > StatisticsAccumulator


1 /*
2  * @(#)StatisticsAccumulator.java 1.3 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package com.sun.corba.se.spi.monitoring;
8
9 import java.util.*;
10
11 /**
12  * <p>
13  *
14  * @author Hemanth Puttaswamy
15  * </p>
16  * <p>
17  * StatisticsAccumulator accumulates the samples provided by the user and
18  * computes the value of minimum, maximum, sum and sample square sum. When
19  * the StatisticMonitoredAttribute calls getValue(), it will compute all
20  * the statistics for the collected samples (Which are Minimum, Maximum,
21  * Average, StandardDeviation) and provides a nice printable record as a
22  * String.
23  *
24  * Users can easily extend this class and provide the implementation of
25  * toString() method to format the stats as desired. By default all the stats
26  * are printed in a single line.
27  * </p>
28  */

29 public class StatisticsAccumulator {
30
31   ///////////////////////////////////////
32
// attributes
33

34
35     // Users can extend this class to get access to current Max value
36
protected double max = Double.MIN_VALUE;
37
38     // Users can extend this class to get access to current Min value
39
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 JavaDoc unit;
48
49
50
51   ///////////////////////////////////////
52
// operations
53

54
55
56 /**
57  * <p>
58  * User will use this method to just register a sample with the
59  * StatisticsAccumulator. This is the only method that User will use to
60  * expose the statistics, internally the StatisticMonitoredAttribute will
61  * collect the information when requested from the ASAdmin.
62  * </p>
63  * <p>
64  *
65  * </p>
66  * <p>
67  *
68  * @param value a double value to make it more precise
69  * </p>
70  */

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     } // end sample
78

79
80
81     /**
82      * Computes the Standard Statistic Results based on the samples collected
83      * so far and provides the complete value as a formatted String
84      */

85     public String JavaDoc getValue( ) {
86         return toString();
87     }
88
89     /**
90      * Users can extend StatisticsAccumulator to provide the complete
91      * Stats in the format they prefer, if the default format doesn't suffice.
92      */

93     public String JavaDoc 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     /**
102      * If users choose to custom format the stats.
103      */

104     protected double computeAverage( ) {
105         return (sampleSum / sampleCount);
106     }
107
108
109     /**
110      * We use a derived Standard Deviation formula to compute SD. This way
111      * there is no need to hold on to all the samples provided.
112      *
113      * The method is protected to let users extend and format the results.
114      */

115     protected double computeStandardDeviation( ) {
116         double sampleSumSquare = sampleSum * sampleSum;
117         return Math.sqrt(
118             (sampleSquareSum-((sampleSumSquare)/sampleCount))/(sampleCount-1));
119     }
120
121 /**
122  * <p>
123  * Construct the Statistics Accumulator by providing the unit as a String.
124  * The examples of units are &quot;Hours&quot;, &quot;Minutes&quot;,
125  * &quot;Seconds&quot;, &quot;MilliSeconds&quot;, &quot;Micro Seconds&quot;
126  * etc.,
127  * </p>
128  * <p>
129  *
130  * @return a StatisticsAccumulator with ...
131  * </p>
132  * <p>
133  * @param unit a String representing the units for the samples collected
134  * </p>
135  */

136     public StatisticsAccumulator( String JavaDoc unit ) {
137         this.unit = unit;
138         sampleCount = 0;
139         sampleSum = 0;
140         sampleSquareSum = 0;
141     }
142
143
144     /**
145      * Clears the samples and starts fresh on new samples.
146      */

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     /**
156      * This is an internal API to test StatisticsAccumulator...
157      */

158     public void unitTestValidate( String JavaDoc expectedUnit, double expectedMin,
159         double expectedMax, long expectedSampleCount, double expectedAverage,
160         double expectedStandardDeviation )
161     {
162         if( !expectedUnit.equals( unit ) ){
163             throw new RuntimeException JavaDoc(
164                 "Unit is not same as expected Unit" +
165                 "\nUnit = " + unit + "ExpectedUnit = " + expectedUnit );
166         }
167         if( min != expectedMin ) {
168             throw new RuntimeException JavaDoc(
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 JavaDoc(
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 JavaDoc(
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 JavaDoc(
185                 "Average is not same as expected Average" +
186                 "\nAverage = " + computeAverage() + "Expected Average = " +
187                 expectedAverage);
188         }
189         // We are computing Standard Deviation from two different methods
190
// for comparison. So, the values will not be the exact same to the last
191
// few digits. So, we are taking the difference and making sure that
192
// the difference is not greater than 1.
193
double difference = Math.abs(
194             computeStandardDeviation() - expectedStandardDeviation);
195         if( difference > 1 ) {
196             throw new RuntimeException JavaDoc(
197                 "Standard Deviation is not same as expected Std Deviation" +
198                 "\nStandard Dev = " + computeStandardDeviation() +
199                 "Expected Standard Dev = " + expectedStandardDeviation);
200         }
201     }
202          
203
204 } // end StatisticsAccumulator
205

206
207
208
Popular Tags