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 de.mcs.jmeasurement.DefaultMeasurePoint;
020: import de.mcs.jmeasurement.MeasureData;
021: import de.mcs.jmeasurement.MeasurePoint;
022: import de.mcs.jmeasurement.MeasurementException;
023: import de.mcs.jmeasurement.Monitor;
024:
025: // /**
026: // * Testing the userdata mechanism.
027: // *
028: // * @author w.klaas
029: // */
030: // class TestUserData2 implements Serializable {
031: // /** serial id. */
032: // private static final long serialVersionUID = -472738475626730165L;
033: //
034: // String myString;
035: //
036: // int myValue;
037: //
038: // /*
039: // * (non-Javadoc)
040: // *
041: // * @see java.lang.Object#toString()
042: // */
043: // public String toString() {
044: // return "{" + myString + "," + Integer.toString(myValue) + "}";
045: // }
046: // }
047: //
048: /**
049: * @author W.Klaas
050: */
051:
052: public class DefaultMeasurePointTest extends MCSTestCase {
053:
054: private static final String BASEPOINT = "de.mcs.jmeasurement.test";
055:
056: public void testStart() throws InterruptedException,
057: MeasurementException {
058: MeasurePoint point = new DefaultMeasurePoint(BASEPOINT, null);
059:
060: Monitor monitor1 = point.start();
061: Monitor monitor2 = point.start();
062: Thread.sleep(75);
063: monitor2.stop();
064: Thread.sleep(75);
065: monitor1.stop();
066:
067: assertBetween(110, 120, ((Long) point.getData("averageMSec")
068: .getValue()).longValue());
069: assertBetween(220, 240, ((Long) point.getData("totalMSec")
070: .getValue()).longValue());
071: assertBetween(67, 82, ((Long) point.getData("minMSec")
072: .getValue()).longValue());
073: assertBetween(140, 160, ((Long) point.getData("maxMSec")
074: .getValue()).longValue());
075: assertEquals(2,
076: ((Long) point.getData("accessCount").getValue())
077: .longValue());
078: assertEquals(0, ((Long) point.getData("active").getValue())
079: .longValue());
080: assertEquals(2, ((Long) point.getData("maxActive").getValue())
081: .longValue());
082:
083: monitor1 = point.start();
084: monitor2 = point.start();
085: assertEquals(2, ((Long) point.getData("active").getValue())
086: .longValue());
087: Thread.sleep(75);
088: monitor2.stop();
089: assertEquals(1, ((Long) point.getData("active").getValue())
090: .longValue());
091: Thread.sleep(75);
092: monitor1.stop();
093:
094: System.out.println("------------");
095: System.out.println("measure point data");
096: System.out.println(point.asString());
097: }
098:
099: /**
100: * @throws InterruptedException
101: * @throws MeasurementException
102: */
103: public void testDeath() throws InterruptedException,
104: MeasurementException {
105: MeasurePoint point = new DefaultMeasurePoint(BASEPOINT, null);
106:
107: // constructing a death monitor
108: Monitor monitor2 = point.start();
109: monitor2.toString();
110: Thread.sleep(75);
111: // monitor2.stop();
112: monitor2 = null;
113:
114: MeasureData obj = point.getData("active");
115: if (obj.getValue() instanceof Long) {
116: assertEquals(1, ((Long) obj.getValue()).longValue());
117: } else {
118: fail("value not Long object");
119: }
120:
121: // we need 2 gc calls. because sometimes the first doesn't do anything.
122: Thread.sleep(1000);
123: System.gc();
124: System.gc();
125: obj = point.getData("active");
126: if (obj.getValue() instanceof Long) {
127: assertEquals(0, ((Long) obj.getValue()).longValue());
128: } else {
129: fail("value not Long object");
130: }
131:
132: obj = point.getData("deathCount");
133: if (obj.getValue() instanceof Long) {
134: assertEquals(1, ((Long) obj.getValue()).longValue());
135: } else {
136: fail("value not Long object");
137: }
138:
139: System.out.println("------------");
140: System.out.println("measure point data");
141: System.out.println(point.asString());
142: }
143:
144: public void testPriority() throws InterruptedException {
145: MeasurePoint point = new DefaultMeasurePoint(BASEPOINT, 5, null);
146: assertEquals(5, point.getPriority());
147: point = new DefaultMeasurePoint(BASEPOINT, null);
148: assertEquals(0, point.getPriority());
149: }
150: }
|