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 TestStatisticalMeasurementWithDispose extends TestCase {
040: private Metrics m1;
041: private Metrics m2;
042: private Metrics m3;
043: private Metrics m4;
044: private Metrics m5;
045: private Metrics m6;
046:
047: private Metrics c1;
048: private Metrics c2;
049:
050: private Metrics g;
051:
052: private MeasurementDescriptor descriptor;
053:
054: protected void setUp() throws Exception {
055: Logger.getLogger(getClass())
056: .info("Starting test: " + getName());
057:
058: descriptor = new MeasurementDescriptor();
059: descriptor.setShortName("bar");
060:
061: m1 = new Metrics("m1");
062: m2 = new Metrics("m2");
063: m3 = new Metrics("m3");
064: m4 = new Metrics("m4");
065: m5 = new Metrics("m5");
066: m6 = new Metrics("m6");
067:
068: m1.track("bar", new CounterMeasurement(null, null, null));
069: m2.track("bar", new CounterMeasurement(null, null, null));
070: m3.track("bar", new CounterMeasurement(null, null, null));
071: m4.track("bar", new CounterMeasurement(null, null, null));
072: m5.track("bar", new CounterMeasurement(null, null, null));
073: m6.track("bar", new CounterMeasurement(null, null, null));
074:
075: m1.addToMeasurement("bar", 1);
076: m2.addToMeasurement("bar", 2);
077: m3.addToMeasurement("bar", 3);
078: m4.addToMeasurement("bar", 4);
079: m5.addToMeasurement("bar", 5);
080: m6.addToMeasurement("bar", 6);
081:
082: c1 = new Metrics("c1");
083: c2 = new Metrics("c2");
084:
085: c1.track("bar", new StatisticalMeasurement(descriptor, c1,
086: "bar"));
087: c2.track("bar", new StatisticalMeasurement(descriptor, c2,
088: "bar"));
089:
090: c1.addSubMetrics(m1);
091: c1.addSubMetrics(m2);
092: c2.addSubMetrics(m3);
093: c2.addSubMetrics(m4);
094: c2.addSubMetrics(m5);
095: c2.addSubMetrics(m6);
096:
097: g = new Metrics("g");
098:
099: g.addSubMetrics(c1);
100: g.addSubMetrics(c2);
101: }
102:
103: protected void tearDown() throws Exception {
104: Logger.getLogger(getClass()).info("End of " + getName());
105: }
106:
107: public void testDefault() {
108: StatisticalMeasurement sm = new StatisticalMeasurement(
109: descriptor, g, "bar");
110:
111: assertEquals("size " + sm, 6, sm.getNbDataPoints());
112: assertEquals("Minimum " + sm, 1.0, sm.getMinimum(), 0.01);
113: assertEquals("Median " + sm, 4.0, sm.getMedian(), 0.01);
114: assertEquals("Average " + sm, 3.5, sm.getAverage(), 0.01);
115: assertEquals("Standard Deviation " + sm, 1.71, sm
116: .getStandardDeviation(), 0.01);
117: assertEquals("Maximum " + sm, 6.0, sm.getMaximum(), 0.01);
118: assertEquals("Sum " + sm, 21.0, sm.getSum(), 0.01);
119: }
120:
121: public void testIgnore() {
122: StatisticalMeasurement sm = new StatisticalMeasurement(
123: descriptor, g, "bar DISPOSE_IGNORE");
124:
125: assertEquals("size " + sm, 6, sm.getNbDataPoints());
126: assertEquals("Minimum " + sm, 1.0, sm.getMinimum(), 0.01);
127: assertEquals("Median " + sm, 4.0, sm.getMedian(), 0.01);
128: assertEquals("Average " + sm, 3.5, sm.getAverage(), 0.01);
129: assertEquals("Standard Deviation " + sm, 1.71, sm
130: .getStandardDeviation(), 0.01);
131: assertEquals("Maximum " + sm, 6.0, sm.getMaximum(), 0.01);
132: assertEquals("Sum " + sm, 21.0, sm.getSum(), 0.01);
133: }
134:
135: public void testMinimum() {
136: StatisticalMeasurement sm = new StatisticalMeasurement(
137: descriptor, g, "bar DISPOSE_MINIMUM");
138:
139: assertEquals("size " + sm, 2, sm.getNbDataPoints());
140: assertEquals("Minimum " + sm, 1.0, sm.getMinimum(), 0.01);
141: assertEquals("Median " + sm, 3.0, sm.getMedian(), 0.01);
142: assertEquals("Average " + sm, 2.0, sm.getAverage(), 0.01);
143: assertEquals("Standard Deviation " + sm, 1.0, sm
144: .getStandardDeviation(), 0.01);
145: assertEquals("Maximum " + sm, 3.0, sm.getMaximum(), 0.01);
146: assertEquals("Sum " + sm, 4.0, sm.getSum(), 0.01);
147: }
148:
149: public void testMedian() {
150: StatisticalMeasurement sm = new StatisticalMeasurement(
151: descriptor, g, "bar DISPOSE_MEDIAN");
152:
153: assertEquals("size " + sm, 2, sm.getNbDataPoints());
154: assertEquals("Minimum " + sm, 2.0, sm.getMinimum(), 0.01);
155: assertEquals("Median " + sm, 5.0, sm.getMedian(), 0.01);
156: assertEquals("Average " + sm, 3.5, sm.getAverage(), 0.01);
157: assertEquals("Standard Deviation " + sm, 1.5, sm
158: .getStandardDeviation(), 0.01);
159: assertEquals("Maximum " + sm, 5.0, sm.getMaximum(), 0.01);
160: assertEquals("Sum " + sm, 7.0, sm.getSum(), 0.01);
161: }
162:
163: public void testAverage() {
164: StatisticalMeasurement sm = new StatisticalMeasurement(
165: descriptor, g, "bar DISPOSE_AVERAGE");
166:
167: assertEquals("size " + sm, 2, sm.getNbDataPoints());
168: assertEquals("Minimum " + sm, 1.5, sm.getMinimum(), 0.01);
169: assertEquals("Median " + sm, 4.5, sm.getMedian(), 0.01);
170: assertEquals("Average " + sm, 3.0, sm.getAverage(), 0.01);
171: assertEquals("Standard Deviation " + sm, 1.5, sm
172: .getStandardDeviation(), 0.01);
173: assertEquals("Maximum " + sm, 4.5, sm.getMaximum(), 0.01);
174: assertEquals("Sum " + sm, 6.0, sm.getSum(), 0.01);
175: }
176:
177: public void testStandardDeviation() {
178: StatisticalMeasurement sm = new StatisticalMeasurement(
179: descriptor, g, "bar DISPOSE_STANDARD_DEVIATION");
180:
181: assertEquals("size " + sm, 2, sm.getNbDataPoints());
182: assertEquals("Minimum " + sm, 0.5, sm.getMinimum(), 0.01);
183: assertEquals("Median " + sm, 1.12, sm.getMedian(), 0.01);
184: assertEquals("Average " + sm, 0.81, sm.getAverage(), 0.01);
185: assertEquals("Standard Deviation " + sm, 0.31, sm
186: .getStandardDeviation(), 0.01);
187: assertEquals("Maximum " + sm, 1.12, sm.getMaximum(), 0.01);
188: assertEquals("Sum " + sm, 1.62, sm.getSum(), 0.01);
189: }
190:
191: public void testMaximum() {
192: StatisticalMeasurement sm = new StatisticalMeasurement(
193: descriptor, g, "bar DISPOSE_MAXIMUM");
194:
195: assertEquals("size " + sm, 2, sm.getNbDataPoints());
196: assertEquals("Minimum " + sm, 2.0, sm.getMinimum(), 0.01);
197: assertEquals("Median " + sm, 6.0, sm.getMedian(), 0.01);
198: assertEquals("Average " + sm, 4.0, sm.getAverage(), 0.01);
199: assertEquals("Standard Deviation " + sm, 2.0, sm
200: .getStandardDeviation(), 0.01);
201: assertEquals("Maximum " + sm, 6.0, sm.getMaximum(), 0.01);
202: assertEquals("Sum " + sm, 8.0, sm.getSum(), 0.01);
203: }
204:
205: public void testSum() {
206: StatisticalMeasurement sm = new StatisticalMeasurement(
207: descriptor, g, "bar DISPOSE_SUM");
208:
209: assertEquals("size " + sm, 2, sm.getNbDataPoints());
210: assertEquals("Minimum " + sm, 3.0, sm.getMinimum(), 0.01);
211: assertEquals("Median " + sm, 18.0, sm.getMedian(), 0.01);
212: assertEquals("Average " + sm, 10.5, sm.getAverage(), 0.01);
213: assertEquals("Standard Deviation " + sm, 7.5, sm
214: .getStandardDeviation(), 0.01);
215: assertEquals("Maximum " + sm, 18.0, sm.getMaximum(), 0.01);
216: assertEquals("Sum " + sm, 21.0, sm.getSum(), 0.01);
217: }
218:
219: public void testNbDataPoints() {
220: StatisticalMeasurement sm = new StatisticalMeasurement(
221: descriptor, g, "bar DISPOSE_NB_DATA_POINTS");
222:
223: assertEquals("size " + sm, 2, sm.getNbDataPoints());
224: assertEquals("Minimum " + sm, 2.0, sm.getMinimum(), 0.01);
225: assertEquals("Median " + sm, 4.0, sm.getMedian(), 0.01);
226: assertEquals("Average " + sm, 3.0, sm.getAverage(), 0.01);
227: assertEquals("Standard Deviation " + sm, 1.0, sm
228: .getStandardDeviation(), 0.01);
229: assertEquals("Maximum " + sm, 4.0, sm.getMaximum(), 0.01);
230: assertEquals("Sum " + sm, 6.0, sm.getSum(), 0.01);
231: }
232: }
|