KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > metrics > TestStatisticalMeasurement


1 /*
2  * Copyright (c) 2001-2005, Jean Tessier
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Jean Tessier nor the names of his contributors
17  * may be used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */

32
33 package com.jeantessier.metrics;
34
35 import junit.framework.*;
36
37 import org.apache.log4j.*;
38
39 public class TestStatisticalMeasurement extends TestCase implements MeasurementVisitor {
40     private StatisticalMeasurement measurement;
41     private Metrics metrics;
42     private Measurement visited;
43     
44     protected void setUp() throws Exception JavaDoc {
45         Logger.getLogger(getClass()).info("Starting test: " + getName());
46
47         metrics = new Metrics("foo");
48         measurement = new StatisticalMeasurement(null, metrics, "bar");
49     }
50     
51     protected void tearDown() throws Exception JavaDoc {
52         Logger.getLogger(getClass()).info("End of " + getName());
53     }
54
55     public void testAdd() {
56         measurement.add(new Integer JavaDoc(1));
57
58         assertEquals(0, measurement.getNbDataPoints());
59     }
60
61     public void testComputeEmpty() {
62         assertEquals("size", 0, measurement.getNbDataPoints());
63         assertTrue("minimum", Double.isNaN(measurement.getMinimum()));
64         assertTrue("median", Double.isNaN(measurement.getMedian()));
65         assertTrue("average", Double.isNaN(measurement.getAverage()));
66         assertTrue("standard deviation", Double.isNaN(measurement.getStandardDeviation()));
67         assertTrue("maximum", Double.isNaN(measurement.getMaximum()));
68         assertEquals("sum", 0.0, measurement.getSum(), 0.01);
69     }
70
71     public void testComputeSingle() {
72         Metrics m = new Metrics("m");
73         m.track("bar", new CounterMeasurement(null, null, null));
74         m.addToMeasurement("bar", 1);
75
76         metrics.addSubMetrics(m);
77
78         assertEquals("size", 1, measurement.getNbDataPoints());
79         assertEquals("minimum", 1.0, measurement.getMinimum(), 0.01);
80         assertEquals("median", 1.0, measurement.getMedian(), 0.01);
81         assertEquals("average", 1.0, measurement.getAverage(), 0.01);
82         assertEquals("standard deviation", 0.0, measurement.getStandardDeviation(), 0.01);
83         assertEquals("maximum", 1.0, measurement.getMaximum(), 0.01);
84         assertEquals("sum", 1.0, measurement.getSum(), 0.01);
85     }
86
87     public void testComputePair() {
88         Metrics m1 = new Metrics("m1");
89         Metrics m2 = new Metrics("m2");
90
91         metrics.addSubMetrics(m1);
92         metrics.addSubMetrics(m2);
93         
94         m1.track("bar", new CounterMeasurement(null, null, null));
95         m2.track("bar", new CounterMeasurement(null, null, null));
96
97         m1.addToMeasurement("bar", 1);
98         m2.addToMeasurement("bar", 100);
99
100         assertEquals("size", 2, measurement.getNbDataPoints());
101         assertEquals("minimum", 1.0, measurement.getMinimum(), 0.01);
102         assertEquals("median", 100.0, measurement.getMedian(), 0.01);
103         assertEquals("average", 50.5, measurement.getAverage(), 0.01);
104         assertEquals("standard deviation", 49.5, measurement.getStandardDeviation(), 0.01);
105         assertEquals("maximum", 100.0, measurement.getMaximum(), 0.01);
106         assertEquals("sum", 101.0, measurement.getSum(), 0.01);
107     }
108
109     public void testComputeTriple() {
110         Metrics m1 = new Metrics("m1");
111         Metrics m2 = new Metrics("m2");
112         Metrics m3 = new Metrics("m3");
113
114         metrics.addSubMetrics(m1);
115         metrics.addSubMetrics(m2);
116         metrics.addSubMetrics(m3);
117         
118         m1.track("bar", new CounterMeasurement(null, null, null));
119         m2.track("bar", new CounterMeasurement(null, null, null));
120         m3.track("bar", new CounterMeasurement(null, null, null));
121
122         m1.addToMeasurement("bar", 1);
123         m2.addToMeasurement("bar", 10);
124         m3.addToMeasurement("bar", 100);
125
126         assertEquals("size", 3, measurement.getNbDataPoints());
127         assertEquals("minimum", 1.0, measurement.getMinimum(), 0.01);
128         assertEquals("median", 10.0, measurement.getMedian(), 0.01);
129         assertEquals("average", 37.0, measurement.getAverage(), 0.01);
130         assertEquals("standard deviation", 44.7, measurement.getStandardDeviation(), 0.01);
131         assertEquals("maximum", 100.0, measurement.getMaximum(), 0.01);
132         assertEquals("sum", 111.0, measurement.getSum(), 0.01);
133     }
134
135     public void testComputeDie() {
136         Metrics m1 = new Metrics("m1");
137         Metrics m2 = new Metrics("m2");
138         Metrics m3 = new Metrics("m3");
139         Metrics m4 = new Metrics("m4");
140         Metrics m5 = new Metrics("m5");
141         Metrics m6 = new Metrics("m6");
142
143         metrics.addSubMetrics(m1);
144         metrics.addSubMetrics(m2);
145         metrics.addSubMetrics(m3);
146         metrics.addSubMetrics(m4);
147         metrics.addSubMetrics(m5);
148         metrics.addSubMetrics(m6);
149         
150         m1.track("bar", new CounterMeasurement(null, null, null));
151         m2.track("bar", new CounterMeasurement(null, null, null));
152         m3.track("bar", new CounterMeasurement(null, null, null));
153         m4.track("bar", new CounterMeasurement(null, null, null));
154         m5.track("bar", new CounterMeasurement(null, null, null));
155         m6.track("bar", new CounterMeasurement(null, null, null));
156
157         m1.addToMeasurement("bar", 1);
158         m2.addToMeasurement("bar", 2);
159         m3.addToMeasurement("bar", 3);
160         m4.addToMeasurement("bar", 4);
161         m5.addToMeasurement("bar", 5);
162         m6.addToMeasurement("bar", 6);
163
164         assertEquals("size", 6, measurement.getNbDataPoints());
165         assertEquals("minimum", 1.0, measurement.getMinimum(), 0.01);
166         assertEquals("median", 4.0, measurement.getMedian(), 0.01);
167         assertEquals("average", 3.5, measurement.getAverage(), 0.01);
168         assertEquals("standard deviation", 1.71, measurement.getStandardDeviation(), 0.01);
169         assertEquals("maximum", 6.0, measurement.getMaximum(), 0.01);
170         assertEquals("sum", 21.0, measurement.getSum(), 0.01);
171     }
172
173     public void testComputeConstant() {
174         Metrics m1 = new Metrics("m1");
175         Metrics m2 = new Metrics("m2");
176         Metrics m3 = new Metrics("m3");
177         Metrics m4 = new Metrics("m4");
178         Metrics m5 = new Metrics("m5");
179         Metrics m6 = new Metrics("m6");
180
181         metrics.addSubMetrics(m1);
182         metrics.addSubMetrics(m2);
183         metrics.addSubMetrics(m3);
184         metrics.addSubMetrics(m4);
185         metrics.addSubMetrics(m5);
186         metrics.addSubMetrics(m6);
187         
188         m1.track("bar", new CounterMeasurement(null, null, null));
189         m2.track("bar", new CounterMeasurement(null, null, null));
190         m3.track("bar", new CounterMeasurement(null, null, null));
191         m4.track("bar", new CounterMeasurement(null, null, null));
192         m5.track("bar", new CounterMeasurement(null, null, null));
193         m6.track("bar", new CounterMeasurement(null, null, null));
194
195         m1.addToMeasurement("bar", 1);
196         m2.addToMeasurement("bar", 1);
197         m3.addToMeasurement("bar", 1);
198         m4.addToMeasurement("bar", 1);
199         m5.addToMeasurement("bar", 1);
200         m6.addToMeasurement("bar", 1);
201
202         assertEquals("size", 6, measurement.getNbDataPoints());
203         assertEquals("minimum", 1.0, measurement.getMinimum(), 0.01);
204         assertEquals("median", 1.0, measurement.getMedian(), 0.01);
205         assertEquals("average", 1.0, measurement.getAverage(), 0.01);
206         assertEquals("standard deviation", 0.0, measurement.getStandardDeviation(), 0.01);
207         assertEquals("maximum", 1.0, measurement.getMaximum(), 0.01);
208         assertEquals("sum", 6.0, measurement.getSum(), 0.01);
209     }
210
211     public void testComputeExponential() {
212         Metrics m01 = new Metrics("m01");
213         Metrics m02 = new Metrics("m02");
214         Metrics m03 = new Metrics("m03");
215         Metrics m04 = new Metrics("m04");
216         Metrics m05 = new Metrics("m05");
217         Metrics m06 = new Metrics("m06");
218         Metrics m07 = new Metrics("m07");
219         Metrics m08 = new Metrics("m08");
220         Metrics m09 = new Metrics("m09");
221         Metrics m10 = new Metrics("m10");
222         Metrics m11 = new Metrics("m11");
223
224         metrics.addSubMetrics(m01);
225         metrics.addSubMetrics(m02);
226         metrics.addSubMetrics(m03);
227         metrics.addSubMetrics(m04);
228         metrics.addSubMetrics(m05);
229         metrics.addSubMetrics(m06);
230         metrics.addSubMetrics(m07);
231         metrics.addSubMetrics(m08);
232         metrics.addSubMetrics(m09);
233         metrics.addSubMetrics(m10);
234         metrics.addSubMetrics(m11);
235         
236         m01.track("bar", new CounterMeasurement(null, null, null));
237         m02.track("bar", new CounterMeasurement(null, null, null));
238         m03.track("bar", new CounterMeasurement(null, null, null));
239         m04.track("bar", new CounterMeasurement(null, null, null));
240         m05.track("bar", new CounterMeasurement(null, null, null));
241         m06.track("bar", new CounterMeasurement(null, null, null));
242         m07.track("bar", new CounterMeasurement(null, null, null));
243         m08.track("bar", new CounterMeasurement(null, null, null));
244         m09.track("bar", new CounterMeasurement(null, null, null));
245         m10.track("bar", new CounterMeasurement(null, null, null));
246         m11.track("bar", new CounterMeasurement(null, null, null));
247
248         m01.addToMeasurement("bar", 1);
249         m02.addToMeasurement("bar", 2);
250         m03.addToMeasurement("bar", 4);
251         m04.addToMeasurement("bar", 8);
252         m05.addToMeasurement("bar", 16);
253         m06.addToMeasurement("bar", 32);
254         m07.addToMeasurement("bar", 64);
255         m08.addToMeasurement("bar", 128);
256         m09.addToMeasurement("bar", 256);
257         m10.addToMeasurement("bar", 512);
258         m11.addToMeasurement("bar", 1024);
259
260         assertEquals("size", 11, measurement.getNbDataPoints());
261         assertEquals("minimum", 1.0, measurement.getMinimum(), 0.01);
262         assertEquals("median", 32.0, measurement.getMedian(), 0.01);
263         assertEquals("average", 186.1, measurement.getAverage(), 0.01);
264         assertEquals("standard deviation", 304.09, measurement.getStandardDeviation(), 0.01);
265         assertEquals("maximum", 1024.0, measurement.getMaximum(), 0.01);
266         assertEquals("sum", 2047.0, measurement.getSum(), 0.01);
267     }
268
269     public void testAccept() {
270         visited = null;
271         measurement.accept(this);
272         assertSame(measurement, visited);
273     }
274
275     public void testToString() {
276         Metrics m = new Metrics("m");
277         m.track("bar", new CounterMeasurement(null, null, null));
278         m.addToMeasurement("bar", 1);
279
280         metrics.addSubMetrics(m);
281
282         assertEquals("toString()", "[1 1/1 0 1 1 (1)]", measurement.toString());
283     }
284
285     public void testDisposeLabel() {
286         assertEquals("StatisticalMeasurement.DISPOSE_IGNORE", "", StatisticalMeasurement.getDisposeLabel(StatisticalMeasurement.DISPOSE_IGNORE));
287         assertEquals("StatisticalMeasurement.DISPOSE_MINIMUM", "minimum", StatisticalMeasurement.getDisposeLabel(StatisticalMeasurement.DISPOSE_MINIMUM));
288         assertEquals("StatisticalMeasurement.DISPOSE_MEDIAN", "median", StatisticalMeasurement.getDisposeLabel(StatisticalMeasurement.DISPOSE_MEDIAN));
289         assertEquals("StatisticalMeasurement.DISPOSE_AVERAGE", "average", StatisticalMeasurement.getDisposeLabel(StatisticalMeasurement.DISPOSE_AVERAGE));
290         assertEquals("StatisticalMeasurement.DISPOSE_STANDARD_DEVIATION", "standard deviation", StatisticalMeasurement.getDisposeLabel(StatisticalMeasurement.DISPOSE_STANDARD_DEVIATION));
291         assertEquals("StatisticalMeasurement.DISPOSE_MAXIMUM", "maximum", StatisticalMeasurement.getDisposeLabel(StatisticalMeasurement.DISPOSE_MAXIMUM));
292         assertEquals("StatisticalMeasurement.DISPOSE_SUM", "sum", StatisticalMeasurement.getDisposeLabel(StatisticalMeasurement.DISPOSE_SUM));
293         assertEquals("StatisticalMeasurement.DISPOSE_NB_DATA_POINTS", "number of data points", StatisticalMeasurement.getDisposeLabel(StatisticalMeasurement.DISPOSE_NB_DATA_POINTS));
294     }
295     
296     public void testDisposeAbbreviation() {
297         assertEquals("StatisticalMeasurement.DISPOSE_IGNORE", "", StatisticalMeasurement.getDisposeAbbreviation(StatisticalMeasurement.DISPOSE_IGNORE));
298         assertEquals("StatisticalMeasurement.DISPOSE_MINIMUM", "min", StatisticalMeasurement.getDisposeAbbreviation(StatisticalMeasurement.DISPOSE_MINIMUM));
299         assertEquals("StatisticalMeasurement.DISPOSE_MEDIAN", "med", StatisticalMeasurement.getDisposeAbbreviation(StatisticalMeasurement.DISPOSE_MEDIAN));
300         assertEquals("StatisticalMeasurement.DISPOSE_AVERAGE", "avg", StatisticalMeasurement.getDisposeAbbreviation(StatisticalMeasurement.DISPOSE_AVERAGE));
301         assertEquals("StatisticalMeasurement.DISPOSE_STANDARD_DEVIATION", "sdv", StatisticalMeasurement.getDisposeAbbreviation(StatisticalMeasurement.DISPOSE_STANDARD_DEVIATION));
302         assertEquals("StatisticalMeasurement.DISPOSE_MAXIMUM", "max", StatisticalMeasurement.getDisposeAbbreviation(StatisticalMeasurement.DISPOSE_MAXIMUM));
303         assertEquals("StatisticalMeasurement.DISPOSE_SUM", "sum", StatisticalMeasurement.getDisposeAbbreviation(StatisticalMeasurement.DISPOSE_SUM));
304         assertEquals("StatisticalMeasurement.DISPOSE_NB_DATA_POINTS", "nb", StatisticalMeasurement.getDisposeAbbreviation(StatisticalMeasurement.DISPOSE_NB_DATA_POINTS));
305     }
306
307     public void testEmpty() throws Exception JavaDoc {
308         assertTrue("Before AddSubMetrics()", measurement.isEmpty());
309         
310         Metrics m = new Metrics("m");
311         m.track("bar", new CounterMeasurement(null, null, null));
312         m.addToMeasurement("bar", 1);
313
314         metrics.addSubMetrics(m);
315
316         assertFalse("After AddSubMetrics()", measurement.isEmpty());
317     }
318
319     public void visitStatisticalMeasurement(StatisticalMeasurement measurement) {
320         visited = measurement;
321     }
322     
323     public void visitRatioMeasurement(RatioMeasurement measurement) {
324         // Do nothing
325
}
326     
327     public void visitNbSubMetricsMeasurement(NbSubMetricsMeasurement measurement) {
328         // Do nothing
329
}
330     
331     public void visitCounterMeasurement(CounterMeasurement measurement) {
332         // Do nothing
333
}
334     
335     public void visitContextAccumulatorMeasurement(ContextAccumulatorMeasurement measurement) {
336         // Do nothing
337
}
338     
339     public void visitNameListMeasurement(NameListMeasurement measurement) {
340         // Do nothing
341
}
342     
343     public void visitSubMetricsAccumulatorMeasurement(SubMetricsAccumulatorMeasurement measurement) {
344         // Do nothing
345
}
346
347     public void visitSumMeasurement(SumMeasurement measurement) {
348         // Do nothing
349
}
350 }
351
Popular Tags