10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25 package com.sun.corba.se.spi.monitoring;
26
27 import java.util.*;
28
29 /**
30 * <p>
31 *
32 * @author Hemanth Puttaswamy
33 * </p>
34 * <p>
35 * StatisticsAccumulator accumulates the samples provided by the user and
36 * computes the value of minimum, maximum, sum and sample square sum. When
37 * the StatisticMonitoredAttribute calls getValue(), it will compute all
38 * the statistics for the collected samples (Which are Minimum, Maximum,
39 * Average, StandardDeviation) and provides a nice printable record as a
40 * String.
41 *
42 * Users can easily extend this class and provide the implementation of
43 * toString() method to format the stats as desired. By default all the stats
44 * are printed in a single line.
45 * </p>
46 */
47 public class StatisticsAccumulator {
48
49 ///////////////////////////////////////
50 // attributes
51
52
53 // Users can extend this class to get access to current Max value
54 protected double max = Double.MIN_VALUE;
55
56 // Users can extend this class to get access to current Min value
57 protected double min = Double.MAX_VALUE;
58
59 private double sampleSum;
60
61 private double sampleSquareSum;
62
63 private long sampleCount;
64
65 protected String unit;
66
67
68
69 ///////////////////////////////////////
70 // operations
71
72
73
74 /**
75 * <p>
76 * User will use this method to just register a sample with the
77 * StatisticsAccumulator. This is the only method that User will use to
78 * expose the statistics, internally the StatisticMonitoredAttribute will
79 * collect the information when requested from the ASAdmin.
80 * </p>
81 * <p>
82 *
83 * </p>
84 * <p>
85 *
86 * @param value a double value to make it more precise
87 * </p>
88 */
89 public void sample(double value) {
90 sampleCount++;
91 if( value < min ) min = value;
92 if( value > max) max = value;
93 sampleSum += value;
94 sampleSquareSum += (value * value);
95 } // end sample
96
97
98
99 /**
100 * Computes the Standard Statistic Results based on the samples collected
101 * so far and provides the complete value as a formatted String
102 */
103 public String getValue( ) {
104 return toString();
105 }
106
107 /**
120 * If users choose to custom format the stats.
121 */
122 protected double computeAverage( ) {
123 return (sampleSum / sampleCount);
124 }
125
126
127 /**
128 * We use a derived Standard Deviation formula to compute SD. This way
129 * there is no need to hold on to all the samples provided.
130 *
131 * The method is protected to let users extend and format the results.
132 */
133 protected double computeStandardDeviation( ) {
134 double sampleSumSquare = sampleSum * sampleSum;
135 return Math.sqrt(
136 (sampleSquareSum-((sampleSumSquare)/sampleCount))/(sampleCount-1));
137 }
138
139 /**
140 * <p>
141 * Construct the Statistics Accumulator by providing the unit as a String.
142 * The examples of units are "Hours", "Minutes",
143 * "Seconds", "MilliSeconds", "Micro Seconds"
144 * etc.,
145 * </p>
146 * <p>
147 *
148 * @return a StatisticsAccumulator with ...
149 * </p>
150 * <p>
151 * @param unit a String representing the units for the samples collected
152 * </p>
153 */
154 public StatisticsAccumulator( String unit ) {
155 this.unit = unit;
156 sampleCount = 0;
157 sampleSum = 0;
158 sampleSquareSum = 0;
159 }
160
161
162 /**
163 * Clears the samples and starts fresh on new samples.
164 */
165 void clearState( ) {
166 min = Double.MAX_VALUE;
167 max = Double.MIN_VALUE;
168 sampleCount = 0;
169 sampleSum = 0;
170 sampleSquareSum = 0;
171 }
172
|
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25 package com.sun.corba.se.spi.monitoring;
26
27 import java.util.*;
28
29 /**
30 * @author Hemanth Puttaswamy
31 *
32 * StatisticsAccumulator accumulates the samples provided by the user and
33 * computes the value of minimum, maximum, sum and sample square sum. When
34 * the StatisticMonitoredAttribute calls getValue(), it will compute all
35 * the statistics for the collected samples (Which are Minimum, Maximum,
36 * Average, StandardDeviation) and provides a nice printable record as a
37 * String.
38 *
39 * Users can easily extend this class and provide the implementation of
40 * toString() method to format the stats as desired. By default all the stats
41 * are printed in a single line.
42 */
43 public class StatisticsAccumulator {
44
45 ///////////////////////////////////////
46 // attributes
47
48
49 // Users can extend this class to get access to current Max value
50 protected double max = Double.MIN_VALUE;
51
52 // Users can extend this class to get access to current Min value
53 protected double min = Double.MAX_VALUE;
54
55 private double sampleSum;
56
57 private double sampleSquareSum;
58
59 private long sampleCount;
60
61 protected String unit;
62
63
64
65 ///////////////////////////////////////
66 // operations
67
68
69
70 /**
71 * User will use this method to just register a sample with the
72 * StatisticsAccumulator. This is the only method that User will use to
73 * expose the statistics, internally the StatisticMonitoredAttribute will
74 * collect the information when requested from the ASAdmin.
75 *
76 * @param value a double value to make it more precise
77 */
78 public void sample(double value) {
79 sampleCount++;
80 if( value < min ) min = value;
81 if( value > max) max = value;
82 sampleSum += value;
83 sampleSquareSum += (value * value);
84 } // end sample
85
86
87
88 /**
89 * Computes the Standard Statistic Results based on the samples collected
90 * so far and provides the complete value as a formatted String
91 */
92 public String getValue( ) {
93 return toString();
94 }
95
96 /**
109 * If users choose to custom format the stats.
110 */
111 protected double computeAverage( ) {
112 return (sampleSum / sampleCount);
113 }
114
115
116 /**
117 * We use a derived Standard Deviation formula to compute SD. This way
118 * there is no need to hold on to all the samples provided.
119 *
120 * The method is protected to let users extend and format the results.
121 */
122 protected double computeStandardDeviation( ) {
123 double sampleSumSquare = sampleSum * sampleSum;
124 return Math.sqrt(
125 (sampleSquareSum-((sampleSumSquare)/sampleCount))/(sampleCount-1));
126 }
127
128 /**
129 * Construct the Statistics Accumulator by providing the unit as a String.
130 * The examples of units are {@literal "Hours", "Minutes",
131 * "Seconds", "MilliSeconds", "Micro Seconds"} etc.
132 *
133 * @param unit a String representing the units for the samples collected
134 */
135 public StatisticsAccumulator( String unit ) {
136 this.unit = unit;
137 sampleCount = 0;
138 sampleSum = 0;
139 sampleSquareSum = 0;
140 }
141
142
143 /**
144 * Clears the samples and starts fresh on new samples.
145 */
146 void clearState( ) {
147 min = Double.MAX_VALUE;
148 max = Double.MIN_VALUE;
149 sampleCount = 0;
150 sampleSum = 0;
151 sampleSquareSum = 0;
152 }
153
|