001: /*
002: * Copyright (c) 2001-2007, Jean Tessier
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * * Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: *
016: * * Neither the name of Jean Tessier nor the names of his contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032:
033: package com.jeantessier.metrics;
034:
035: import junit.framework.*;
036:
037: import org.apache.log4j.*;
038:
039: public class TestStatisticalMeasurement extends TestCase implements
040: MeasurementVisitor {
041: private StatisticalMeasurement measurement;
042: private Metrics metrics;
043: private Measurement visited;
044:
045: protected void setUp() throws Exception {
046: Logger.getLogger(getClass())
047: .info("Starting test: " + getName());
048:
049: metrics = new Metrics("foo");
050: measurement = new StatisticalMeasurement(null, metrics, "bar");
051: }
052:
053: protected void tearDown() throws Exception {
054: Logger.getLogger(getClass()).info("End of " + getName());
055: }
056:
057: public void testAdd() {
058: measurement.add(new Integer(1));
059:
060: assertEquals(0, measurement.getNbDataPoints());
061: }
062:
063: public void testComputeEmpty() {
064: assertEquals("size", 0, measurement.getNbDataPoints());
065: assertTrue("minimum", Double.isNaN(measurement.getMinimum()));
066: assertTrue("median", Double.isNaN(measurement.getMedian()));
067: assertTrue("average", Double.isNaN(measurement.getAverage()));
068: assertTrue("standard deviation", Double.isNaN(measurement
069: .getStandardDeviation()));
070: assertTrue("maximum", Double.isNaN(measurement.getMaximum()));
071: assertEquals("sum", 0.0, measurement.getSum(), 0.01);
072: }
073:
074: public void testComputeSingle() {
075: Metrics m = new Metrics("m");
076: m.track("bar", new CounterMeasurement(null, null, null));
077: m.addToMeasurement("bar", 1);
078:
079: metrics.addSubMetrics(m);
080:
081: assertEquals("size", 1, measurement.getNbDataPoints());
082: assertEquals("minimum", 1.0, measurement.getMinimum(), 0.01);
083: assertEquals("median", 1.0, measurement.getMedian(), 0.01);
084: assertEquals("average", 1.0, measurement.getAverage(), 0.01);
085: assertEquals("standard deviation", 0.0, measurement
086: .getStandardDeviation(), 0.01);
087: assertEquals("maximum", 1.0, measurement.getMaximum(), 0.01);
088: assertEquals("sum", 1.0, measurement.getSum(), 0.01);
089: }
090:
091: public void testComputePair() {
092: Metrics m1 = new Metrics("m1");
093: Metrics m2 = new Metrics("m2");
094:
095: metrics.addSubMetrics(m1);
096: metrics.addSubMetrics(m2);
097:
098: m1.track("bar", new CounterMeasurement(null, null, null));
099: m2.track("bar", new CounterMeasurement(null, null, null));
100:
101: m1.addToMeasurement("bar", 1);
102: m2.addToMeasurement("bar", 100);
103:
104: assertEquals("size", 2, measurement.getNbDataPoints());
105: assertEquals("minimum", 1.0, measurement.getMinimum(), 0.01);
106: assertEquals("median", 100.0, measurement.getMedian(), 0.01);
107: assertEquals("average", 50.5, measurement.getAverage(), 0.01);
108: assertEquals("standard deviation", 49.5, measurement
109: .getStandardDeviation(), 0.01);
110: assertEquals("maximum", 100.0, measurement.getMaximum(), 0.01);
111: assertEquals("sum", 101.0, measurement.getSum(), 0.01);
112: }
113:
114: public void testComputeTriple() {
115: Metrics m1 = new Metrics("m1");
116: Metrics m2 = new Metrics("m2");
117: Metrics m3 = new Metrics("m3");
118:
119: metrics.addSubMetrics(m1);
120: metrics.addSubMetrics(m2);
121: metrics.addSubMetrics(m3);
122:
123: m1.track("bar", new CounterMeasurement(null, null, null));
124: m2.track("bar", new CounterMeasurement(null, null, null));
125: m3.track("bar", new CounterMeasurement(null, null, null));
126:
127: m1.addToMeasurement("bar", 1);
128: m2.addToMeasurement("bar", 10);
129: m3.addToMeasurement("bar", 100);
130:
131: assertEquals("size", 3, measurement.getNbDataPoints());
132: assertEquals("minimum", 1.0, measurement.getMinimum(), 0.01);
133: assertEquals("median", 10.0, measurement.getMedian(), 0.01);
134: assertEquals("average", 37.0, measurement.getAverage(), 0.01);
135: assertEquals("standard deviation", 44.7, measurement
136: .getStandardDeviation(), 0.01);
137: assertEquals("maximum", 100.0, measurement.getMaximum(), 0.01);
138: assertEquals("sum", 111.0, measurement.getSum(), 0.01);
139: }
140:
141: public void testComputeDie() {
142: Metrics m1 = new Metrics("m1");
143: Metrics m2 = new Metrics("m2");
144: Metrics m3 = new Metrics("m3");
145: Metrics m4 = new Metrics("m4");
146: Metrics m5 = new Metrics("m5");
147: Metrics m6 = new Metrics("m6");
148:
149: metrics.addSubMetrics(m1);
150: metrics.addSubMetrics(m2);
151: metrics.addSubMetrics(m3);
152: metrics.addSubMetrics(m4);
153: metrics.addSubMetrics(m5);
154: metrics.addSubMetrics(m6);
155:
156: m1.track("bar", new CounterMeasurement(null, null, null));
157: m2.track("bar", new CounterMeasurement(null, null, null));
158: m3.track("bar", new CounterMeasurement(null, null, null));
159: m4.track("bar", new CounterMeasurement(null, null, null));
160: m5.track("bar", new CounterMeasurement(null, null, null));
161: m6.track("bar", new CounterMeasurement(null, null, null));
162:
163: m1.addToMeasurement("bar", 1);
164: m2.addToMeasurement("bar", 2);
165: m3.addToMeasurement("bar", 3);
166: m4.addToMeasurement("bar", 4);
167: m5.addToMeasurement("bar", 5);
168: m6.addToMeasurement("bar", 6);
169:
170: assertEquals("size", 6, measurement.getNbDataPoints());
171: assertEquals("minimum", 1.0, measurement.getMinimum(), 0.01);
172: assertEquals("median", 4.0, measurement.getMedian(), 0.01);
173: assertEquals("average", 3.5, measurement.getAverage(), 0.01);
174: assertEquals("standard deviation", 1.71, measurement
175: .getStandardDeviation(), 0.01);
176: assertEquals("maximum", 6.0, measurement.getMaximum(), 0.01);
177: assertEquals("sum", 21.0, measurement.getSum(), 0.01);
178: }
179:
180: public void testComputeConstant() {
181: Metrics m1 = new Metrics("m1");
182: Metrics m2 = new Metrics("m2");
183: Metrics m3 = new Metrics("m3");
184: Metrics m4 = new Metrics("m4");
185: Metrics m5 = new Metrics("m5");
186: Metrics m6 = new Metrics("m6");
187:
188: metrics.addSubMetrics(m1);
189: metrics.addSubMetrics(m2);
190: metrics.addSubMetrics(m3);
191: metrics.addSubMetrics(m4);
192: metrics.addSubMetrics(m5);
193: metrics.addSubMetrics(m6);
194:
195: m1.track("bar", new CounterMeasurement(null, null, null));
196: m2.track("bar", new CounterMeasurement(null, null, null));
197: m3.track("bar", new CounterMeasurement(null, null, null));
198: m4.track("bar", new CounterMeasurement(null, null, null));
199: m5.track("bar", new CounterMeasurement(null, null, null));
200: m6.track("bar", new CounterMeasurement(null, null, null));
201:
202: m1.addToMeasurement("bar", 1);
203: m2.addToMeasurement("bar", 1);
204: m3.addToMeasurement("bar", 1);
205: m4.addToMeasurement("bar", 1);
206: m5.addToMeasurement("bar", 1);
207: m6.addToMeasurement("bar", 1);
208:
209: assertEquals("size", 6, measurement.getNbDataPoints());
210: assertEquals("minimum", 1.0, measurement.getMinimum(), 0.01);
211: assertEquals("median", 1.0, measurement.getMedian(), 0.01);
212: assertEquals("average", 1.0, measurement.getAverage(), 0.01);
213: assertEquals("standard deviation", 0.0, measurement
214: .getStandardDeviation(), 0.01);
215: assertEquals("maximum", 1.0, measurement.getMaximum(), 0.01);
216: assertEquals("sum", 6.0, measurement.getSum(), 0.01);
217: }
218:
219: public void testComputeExponential() {
220: Metrics m01 = new Metrics("m01");
221: Metrics m02 = new Metrics("m02");
222: Metrics m03 = new Metrics("m03");
223: Metrics m04 = new Metrics("m04");
224: Metrics m05 = new Metrics("m05");
225: Metrics m06 = new Metrics("m06");
226: Metrics m07 = new Metrics("m07");
227: Metrics m08 = new Metrics("m08");
228: Metrics m09 = new Metrics("m09");
229: Metrics m10 = new Metrics("m10");
230: Metrics m11 = new Metrics("m11");
231:
232: metrics.addSubMetrics(m01);
233: metrics.addSubMetrics(m02);
234: metrics.addSubMetrics(m03);
235: metrics.addSubMetrics(m04);
236: metrics.addSubMetrics(m05);
237: metrics.addSubMetrics(m06);
238: metrics.addSubMetrics(m07);
239: metrics.addSubMetrics(m08);
240: metrics.addSubMetrics(m09);
241: metrics.addSubMetrics(m10);
242: metrics.addSubMetrics(m11);
243:
244: m01.track("bar", new CounterMeasurement(null, null, null));
245: m02.track("bar", new CounterMeasurement(null, null, null));
246: m03.track("bar", new CounterMeasurement(null, null, null));
247: m04.track("bar", new CounterMeasurement(null, null, null));
248: m05.track("bar", new CounterMeasurement(null, null, null));
249: m06.track("bar", new CounterMeasurement(null, null, null));
250: m07.track("bar", new CounterMeasurement(null, null, null));
251: m08.track("bar", new CounterMeasurement(null, null, null));
252: m09.track("bar", new CounterMeasurement(null, null, null));
253: m10.track("bar", new CounterMeasurement(null, null, null));
254: m11.track("bar", new CounterMeasurement(null, null, null));
255:
256: m01.addToMeasurement("bar", 1);
257: m02.addToMeasurement("bar", 2);
258: m03.addToMeasurement("bar", 4);
259: m04.addToMeasurement("bar", 8);
260: m05.addToMeasurement("bar", 16);
261: m06.addToMeasurement("bar", 32);
262: m07.addToMeasurement("bar", 64);
263: m08.addToMeasurement("bar", 128);
264: m09.addToMeasurement("bar", 256);
265: m10.addToMeasurement("bar", 512);
266: m11.addToMeasurement("bar", 1024);
267:
268: assertEquals("size", 11, measurement.getNbDataPoints());
269: assertEquals("minimum", 1.0, measurement.getMinimum(), 0.01);
270: assertEquals("median", 32.0, measurement.getMedian(), 0.01);
271: assertEquals("average", 186.1, measurement.getAverage(), 0.01);
272: assertEquals("standard deviation", 304.09, measurement
273: .getStandardDeviation(), 0.01);
274: assertEquals("maximum", 1024.0, measurement.getMaximum(), 0.01);
275: assertEquals("sum", 2047.0, measurement.getSum(), 0.01);
276: }
277:
278: public void testAccept() {
279: visited = null;
280: measurement.accept(this );
281: assertSame(measurement, visited);
282: }
283:
284: public void testToString() {
285: Metrics m = new Metrics("m");
286: m.track("bar", new CounterMeasurement(null, null, null));
287: m.addToMeasurement("bar", 1);
288:
289: metrics.addSubMetrics(m);
290:
291: assertEquals("toString()", "[1 1/1 0 1 1 (1)]", measurement
292: .toString());
293: }
294:
295: public void testDisposeLabel() {
296: assertEquals(
297: "StatisticalMeasurement.DISPOSE_IGNORE",
298: "",
299: StatisticalMeasurement
300: .getDisposeLabel(StatisticalMeasurement.DISPOSE_IGNORE));
301: assertEquals(
302: "StatisticalMeasurement.DISPOSE_MINIMUM",
303: "minimum",
304: StatisticalMeasurement
305: .getDisposeLabel(StatisticalMeasurement.DISPOSE_MINIMUM));
306: assertEquals(
307: "StatisticalMeasurement.DISPOSE_MEDIAN",
308: "median",
309: StatisticalMeasurement
310: .getDisposeLabel(StatisticalMeasurement.DISPOSE_MEDIAN));
311: assertEquals(
312: "StatisticalMeasurement.DISPOSE_AVERAGE",
313: "average",
314: StatisticalMeasurement
315: .getDisposeLabel(StatisticalMeasurement.DISPOSE_AVERAGE));
316: assertEquals(
317: "StatisticalMeasurement.DISPOSE_STANDARD_DEVIATION",
318: "standard deviation",
319: StatisticalMeasurement
320: .getDisposeLabel(StatisticalMeasurement.DISPOSE_STANDARD_DEVIATION));
321: assertEquals(
322: "StatisticalMeasurement.DISPOSE_MAXIMUM",
323: "maximum",
324: StatisticalMeasurement
325: .getDisposeLabel(StatisticalMeasurement.DISPOSE_MAXIMUM));
326: assertEquals(
327: "StatisticalMeasurement.DISPOSE_SUM",
328: "sum",
329: StatisticalMeasurement
330: .getDisposeLabel(StatisticalMeasurement.DISPOSE_SUM));
331: assertEquals(
332: "StatisticalMeasurement.DISPOSE_NB_DATA_POINTS",
333: "number of data points",
334: StatisticalMeasurement
335: .getDisposeLabel(StatisticalMeasurement.DISPOSE_NB_DATA_POINTS));
336: }
337:
338: public void testDisposeAbbreviation() {
339: assertEquals(
340: "StatisticalMeasurement.DISPOSE_IGNORE",
341: "",
342: StatisticalMeasurement
343: .getDisposeAbbreviation(StatisticalMeasurement.DISPOSE_IGNORE));
344: assertEquals(
345: "StatisticalMeasurement.DISPOSE_MINIMUM",
346: "min",
347: StatisticalMeasurement
348: .getDisposeAbbreviation(StatisticalMeasurement.DISPOSE_MINIMUM));
349: assertEquals(
350: "StatisticalMeasurement.DISPOSE_MEDIAN",
351: "med",
352: StatisticalMeasurement
353: .getDisposeAbbreviation(StatisticalMeasurement.DISPOSE_MEDIAN));
354: assertEquals(
355: "StatisticalMeasurement.DISPOSE_AVERAGE",
356: "avg",
357: StatisticalMeasurement
358: .getDisposeAbbreviation(StatisticalMeasurement.DISPOSE_AVERAGE));
359: assertEquals(
360: "StatisticalMeasurement.DISPOSE_STANDARD_DEVIATION",
361: "sdv",
362: StatisticalMeasurement
363: .getDisposeAbbreviation(StatisticalMeasurement.DISPOSE_STANDARD_DEVIATION));
364: assertEquals(
365: "StatisticalMeasurement.DISPOSE_MAXIMUM",
366: "max",
367: StatisticalMeasurement
368: .getDisposeAbbreviation(StatisticalMeasurement.DISPOSE_MAXIMUM));
369: assertEquals(
370: "StatisticalMeasurement.DISPOSE_SUM",
371: "sum",
372: StatisticalMeasurement
373: .getDisposeAbbreviation(StatisticalMeasurement.DISPOSE_SUM));
374: assertEquals(
375: "StatisticalMeasurement.DISPOSE_NB_DATA_POINTS",
376: "nb",
377: StatisticalMeasurement
378: .getDisposeAbbreviation(StatisticalMeasurement.DISPOSE_NB_DATA_POINTS));
379: }
380:
381: public void testEmpty() {
382: assertTrue("Before AddSubMetrics()", measurement.isEmpty());
383:
384: Metrics m = new Metrics("m");
385: m.track("bar", new CounterMeasurement(null, null, null));
386: m.addToMeasurement("bar", 1);
387:
388: metrics.addSubMetrics(m);
389:
390: assertFalse("After AddSubMetrics()", measurement.isEmpty());
391: }
392:
393: public void visitStatisticalMeasurement(
394: StatisticalMeasurement measurement) {
395: visited = measurement;
396: }
397:
398: public void visitRatioMeasurement(RatioMeasurement measurement) {
399: // Do nothing
400: }
401:
402: public void visitNbSubMetricsMeasurement(
403: NbSubMetricsMeasurement measurement) {
404: // Do nothing
405: }
406:
407: public void visitCounterMeasurement(CounterMeasurement measurement) {
408: // Do nothing
409: }
410:
411: public void visitContextAccumulatorMeasurement(
412: ContextAccumulatorMeasurement measurement) {
413: // Do nothing
414: }
415:
416: public void visitNameListMeasurement(NameListMeasurement measurement) {
417: // Do nothing
418: }
419:
420: public void visitSubMetricsAccumulatorMeasurement(
421: SubMetricsAccumulatorMeasurement measurement) {
422: // Do nothing
423: }
424:
425: public void visitSumMeasurement(SumMeasurement measurement) {
426: // Do nothing
427: }
428: }
|