001: /*
002: * MCS Media Computer Software Copyright (c) 2005 by MCS
003: * -------------------------------------- Created on 23.04.2005 by w.klaas
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
006: * use this file except in compliance with the License. You may obtain a copy of
007: * the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
013: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
014: * License for the specific language governing permissions and limitations under
015: * the License.
016: */
017: package de.mcs.jmeasurement.test;
018:
019: import java.io.FileOutputStream;
020: import java.io.IOException;
021: import java.io.PrintWriter;
022: import java.io.Serializable;
023:
024: import junit.framework.TestCase;
025:
026: import org.xml.sax.SAXException;
027:
028: import de.mcs.jmeasurement.DefaultMonitor;
029: import de.mcs.jmeasurement.IUserData;
030: import de.mcs.jmeasurement.MeasureFactory;
031: import de.mcs.jmeasurement.MeasurePoint;
032: import de.mcs.jmeasurement.MeasurementException;
033: import de.mcs.jmeasurement.Monitor;
034: import de.mcs.jmeasurement.NullMonitor;
035: import de.mcs.jmeasurement.RendererMustNotBeNullException;
036: import de.mcs.jmeasurement.renderer.DefaultCSVDataRenderer;
037: import de.mcs.jmeasurement.renderer.DefaultHTMLRenderer;
038:
039: /**
040: * @author w.klaas
041: */
042: class TestUserData implements IUserData, Serializable {
043: /**
044: *
045: */
046: private static final long serialVersionUID = 8399742355555423119L;
047:
048: String myString;
049:
050: int myValue;
051:
052: /*
053: * (non-Javadoc)
054: *
055: * @see java.lang.Object#toString()
056: */
057: public String toString() {
058: return "{" + myString + "," + Integer.toString(myValue) + "}";
059: }
060:
061: public Object clone() {
062: TestUserData udat = new TestUserData();
063: udat.myString = this .myString;
064: udat.myValue = this .myValue;
065: return udat;
066: }
067: }
068:
069: public class MeasureFactoryTest extends TestCase {
070:
071: private static final String BASEPOINT = "de.mcs.jmeasurement.test";
072:
073: private static final String APPNAME = "de.mcs.jmeasurement.test.Application";
074:
075: public void testStart() throws InterruptedException,
076: MeasurementException {
077: Monitor monitor1;
078: Monitor monitor2;
079:
080: MeasureFactory.setApplicationName(APPNAME);
081: assertEquals(APPNAME, MeasureFactory.getApplicationName());
082:
083: monitor1 = MeasureFactory.start(BASEPOINT);
084: Thread.sleep(100);
085: monitor1.stop();
086:
087: monitor1 = MeasureFactory.start(BASEPOINT + ".1");
088: Thread.sleep(50);
089: monitor1.stop();
090:
091: monitor1 = MeasureFactory.start(BASEPOINT + ".1.a");
092: Thread.sleep(50);
093: monitor1.stop();
094:
095: monitor1 = MeasureFactory.start(BASEPOINT + ".1.b");
096: Thread.sleep(100);
097: monitor1.stop();
098:
099: monitor1 = MeasureFactory.start(BASEPOINT + ".1.c");
100: Thread.sleep(150);
101: monitor1.stop();
102:
103: monitor1 = MeasureFactory.start(BASEPOINT + ".2");
104: Thread.sleep(150);
105: monitor1.stop();
106:
107: monitor1 = MeasureFactory.start(BASEPOINT + ".1");
108: Thread.sleep(75);
109: monitor1.stop();
110:
111: monitor1 = MeasureFactory.start(BASEPOINT + ".3");
112: monitor2 = MeasureFactory.start(BASEPOINT + ".3");
113: Thread.sleep(75);
114: monitor2.stop();
115: Thread.sleep(75);
116: monitor1.stop();
117:
118: assertEquals("2", MeasureFactory.getMeasurePoint(
119: BASEPOINT + ".3").getData("accessCount").getAsString());
120: assertEquals(7,
121: MeasureFactory.getMeasurePointNames(null).length);
122: assertEquals(7, MeasureFactory.getMeasurePointNames(BASEPOINT
123: + ".*").length);
124: assertEquals(4, MeasureFactory.getMeasurePointNames(BASEPOINT
125: + ".1.*").length);
126:
127: assertEquals(7, MeasureFactory.getMeasurePoints(null).length);
128: assertEquals(7, MeasureFactory.getMeasurePoints(BASEPOINT
129: + ".*").length);
130: assertEquals(4, MeasureFactory.getMeasurePoints(BASEPOINT
131: + ".1.*").length);
132: assertEquals(0, MeasureFactory.getMeasurePoints(BASEPOINT
133: + ".4.*").length);
134:
135: assertNotNull(MeasureFactory.getMeasurePoint(BASEPOINT));
136: assertNotNull(MeasureFactory.getMeasurePoint(BASEPOINT + ".4"));
137:
138: System.out.println(MeasureFactory.asString());
139: }
140:
141: public void testPriority() throws InterruptedException,
142: MeasurementException, IOException, SAXException {
143: Monitor monitor1;
144: MeasurePoint point;
145: MeasureFactory.setApplicationName("testFactory");
146: MeasureFactory.clear();
147: MeasureFactory.setPriority(5);
148: assertEquals(5, MeasureFactory.getPriority());
149: for (int i = 0; i < 10; i++) {
150: point = MeasureFactory.getMeasurePoint(BASEPOINT + "."
151: + Integer.toString(i));
152: point.setPriority(i);
153: monitor1 = MeasureFactory.start(point.getName());
154: Thread.sleep(i * 10);
155: monitor1.stop();
156: }
157:
158: for (int i = 0; i < 10; i++) {
159: point = MeasureFactory.getMeasurePoint(BASEPOINT + "."
160: + Integer.toString(i));
161: monitor1 = MeasureFactory.start(point.getName());
162: Thread.sleep(i * 20);
163: monitor1.stop();
164: }
165:
166: assertEquals(10, MeasureFactory.getMeasurePoints(null).length);
167: int count = 0;
168: MeasurePoint[] points = MeasureFactory.getMeasurePoints(null);
169: for (int i = 0; i < points.length; i++) {
170: MeasurePoint point2 = points[i];
171: if (point2.getData("accessCount").getAsString().equals("2")) {
172: count++;
173: }
174: }
175: assertEquals(5, count);
176:
177: System.out.println("------------");
178: System.out.println("check priority");
179: System.out.println(MeasureFactory.asString());
180: }
181:
182: public void testSavings() throws InterruptedException, IOException,
183: SAXException, MeasurementException {
184: Monitor monitor1;
185: MeasurePoint point;
186: MeasureFactory.setApplicationName("testFactory");
187: MeasureFactory.clear();
188: MeasureFactory.setPriority(5);
189: for (int i = 0; i < 10; i++) {
190: point = MeasureFactory.getMeasurePoint(BASEPOINT + "."
191: + Integer.toString(i));
192: point.setPriority(i);
193: TestUserData userData = new TestUserData();
194: userData.myString = point.getName() + ".userdata";
195: userData.myValue = i;
196: point.setUserData(userData);
197: monitor1 = MeasureFactory.start(point.getName());
198: Thread.sleep(i * 10);
199: if (i == 6) {
200: monitor1
201: .setException("Exception with something to do.");
202: }
203: monitor1.stop();
204:
205: }
206:
207: try {
208: MeasureFactory.getReport(null);
209: fail("An exception was not thrown.");
210: } catch (Exception e) {
211: assertTrue(e instanceof RendererMustNotBeNullException);
212: }
213:
214: String csvReport1 = MeasureFactory
215: .getReport(new DefaultCSVDataRenderer());
216: System.out.println("------------");
217: System.out.println("first report");
218: System.out.println(MeasureFactory.asString());
219:
220: System.out.println("------------");
221: System.out.println("taking snapshot 1");
222: MeasureFactory.takeSnapshot("mein 1. snapshot");
223: MeasureFactory.saveToXMLFile("test.xml");
224: MeasureFactory.clear();
225:
226: System.out.println("------------");
227: System.out.println("empty report");
228: System.out.println(MeasureFactory.asString());
229:
230: MeasureFactory.loadFromXMLFile("test.xml", true);
231: System.out.println("------------");
232: System.out.println("taking snapshot 2");
233: MeasureFactory.takeSnapshot("mein 2. snapshot");
234: System.out.println("------------");
235: System.out.println("report after loading xml");
236: System.out.println(MeasureFactory.asString());
237: String csvReport2 = MeasureFactory
238: .getReport(new DefaultCSVDataRenderer());
239: System.out.println("------------");
240: System.out.print("testing report equality:");
241: try {
242: assertEquals(csvReport1, csvReport2);
243: System.out.println("OK.");
244: } finally {
245: System.out.println();
246: }
247:
248: System.out.println("------------");
249: System.out.println("taking snapshot 3");
250: MeasureFactory.takeSnapshot("mein 3. snapshot");
251: PrintWriter output = new PrintWriter(new FileOutputStream(
252: "e:\\temp\\output.html"));
253: MeasureFactory.getReport(null, new DefaultHTMLRenderer(),
254: output);
255: output.close();
256: }
257:
258: public void testEnable() {
259:
260: MeasureFactory.clear();
261: MeasureFactory.setPriority(0);
262: MeasureFactory.setEnable(true);
263: assertTrue(MeasureFactory.isEnable());
264: Monitor monitor = MeasureFactory.getMonitor(BASEPOINT);
265:
266: assertTrue(monitor instanceof DefaultMonitor);
267:
268: MeasureFactory.setEnable(false);
269: assertFalse(MeasureFactory.isEnable());
270: monitor = MeasureFactory.getMonitor(BASEPOINT);
271:
272: assertTrue(monitor instanceof NullMonitor);
273:
274: }
275: }
|