001: /*
002: * MCS Media Computer Software Copyright (c) 2006 by MCS
003: * -------------------------------------- Created on 26.07.2006 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. $Id$
016: */
017: /**
018: *
019: */package de.mcs.jmeasurement.test.proxy;
020:
021: import java.io.IOException;
022:
023: import org.xml.sax.SAXException;
024:
025: import de.mcs.jmeasurement.DefaultMeasurePoint;
026: import de.mcs.jmeasurement.DefaultMonitor;
027: import de.mcs.jmeasurement.MeasureFactory;
028: import de.mcs.jmeasurement.MeasurementException;
029: import de.mcs.jmeasurement.Monitor;
030: import de.mcs.jmeasurement.test.MCSTestCase;
031:
032: /**
033: * @author W.Klaas
034: * @version $Revision
035: */
036: public class ProxyTest extends MCSTestCase {
037:
038: private static final int LOOP_COUNT_PROXY = 2;
039:
040: private static final int LOOP_COUNT = 100000;
041:
042: private static final String TEST_STRING = "Das ist das Haus vom Nikolaus..."
043: + "Das ist das Haus vom Nikolaus..."
044: + "Das ist das Haus vom Nikolaus..."
045: + "Das ist das Haus vom Nikolaus..."
046: + "Das ist das Haus vom Nikolaus..."
047: + "Das ist das Haus vom Nikolaus..."
048: + "Das ist das Haus vom Nikolaus..."
049: + "Das ist das Haus vom Nikolaus...";
050:
051: /**
052: * testing the proxy functionality of JMeasurement.
053: *
054: * @throws Exception
055: */
056: public void testProxy() throws Exception {
057: System.out
058: .println("testing proxy functionality of JMeasurement.");
059: MeasureFactory.setApplicationName("ProxyTest");
060: MeasureFactory.clear();
061: ITestProxy testProxy = (ITestProxy) MeasureFactory
062: .registerInterface(new CTestProxy(), true, true);
063:
064: for (int i = 0; i < LOOP_COUNT_PROXY; i++) {
065: testProxy.echoNow("echo now: This is my line.");
066: String org = "echo: This is my line.";
067: String line = testProxy.echo(org);
068: assertEquals(org, line);
069: try {
070: testProxy.exceptionNow();
071: fail("ProxyException expected");
072: } catch (Exception e) {
073: }
074: }
075: System.out.println(MeasureFactory.asString());
076:
077: Long value = (Long) ((DefaultMeasurePoint) MeasureFactory
078: .getMeasurePoint("de.mcs.jmeasurement.test.proxy.CTestProxy#echo"))
079: .getData("averageMSec").getValue();
080:
081: assertBetween(CTestProxy.waitEcho - 10,
082: CTestProxy.waitEcho + 10, value.intValue());
083:
084: value = (Long) ((DefaultMeasurePoint) MeasureFactory
085: .getMeasurePoint("de.mcs.jmeasurement.test.proxy.CTestProxy#echo"))
086: .getData("accessCount").getValue();
087:
088: assertEquals(LOOP_COUNT_PROXY, value.intValue());
089:
090: value = (Long) ((DefaultMeasurePoint) MeasureFactory
091: .getMeasurePoint("de.mcs.jmeasurement.test.proxy.CTestProxy#echoNow"))
092: .getData("averageMSec").getValue();
093:
094: assertBetween(CTestProxy.waitEchoNow - 10,
095: CTestProxy.waitEchoNow + 10, value.intValue());
096:
097: value = (Long) ((DefaultMeasurePoint) MeasureFactory
098: .getMeasurePoint("de.mcs.jmeasurement.test.proxy.CTestProxy#echoNow"))
099: .getData("accessCount").getValue();
100:
101: assertEquals(LOOP_COUNT_PROXY, value.intValue());
102:
103: value = (Long) ((DefaultMeasurePoint) MeasureFactory
104: .getMeasurePoint("de.mcs.jmeasurement.test.proxy.CTestProxy#exceptionNow"))
105: .getData("averageMSec").getValue();
106:
107: assertBetween(CTestProxy.waitException - 10,
108: CTestProxy.waitException + 10, value.intValue());
109:
110: value = (Long) ((DefaultMeasurePoint) MeasureFactory
111: .getMeasurePoint("de.mcs.jmeasurement.test.proxy.CTestProxy#exceptionNow"))
112: .getData("accessCount").getValue();
113:
114: assertEquals(LOOP_COUNT_PROXY, value.intValue());
115: }
116:
117: /**
118: * testing the proxy functionality of JMeasurement.
119: *
120: * @throws Exception
121: */
122: public void testProxyMethodNames() throws Exception {
123: System.out
124: .println("testing proxy functionality with method names of JMeasurement.");
125: MeasureFactory.setApplicationName("ProxyTest");
126: MeasureFactory.clear();
127: ITestProxy testProxy = (ITestProxy) MeasureFactory
128: .registerInterface(new CTestProxy(), true, true,
129: new String[] { "echo" });
130:
131: for (int i = 0; i < LOOP_COUNT_PROXY; i++) {
132: testProxy.echoNow("echo now: This is my line.");
133: String org = "echo: This is my line.";
134: String line = testProxy.echo(org);
135: assertEquals(org, line);
136: try {
137: testProxy.exceptionNow();
138: fail("ProxyException expected");
139: } catch (Exception e) {
140: }
141: }
142: System.out.println(MeasureFactory.asString());
143:
144: Long value = (Long) ((DefaultMeasurePoint) MeasureFactory
145: .getMeasurePoint("de.mcs.jmeasurement.test.proxy.CTestProxy#echo"))
146: .getData("averageMSec").getValue();
147:
148: assertBetween(CTestProxy.waitEcho - 10,
149: CTestProxy.waitEcho + 10, value.intValue());
150:
151: value = (Long) ((DefaultMeasurePoint) MeasureFactory
152: .getMeasurePoint("de.mcs.jmeasurement.test.proxy.CTestProxy#echo"))
153: .getData("accessCount").getValue();
154:
155: assertEquals(LOOP_COUNT_PROXY, value.intValue());
156:
157: value = (Long) ((DefaultMeasurePoint) MeasureFactory
158: .getMeasurePoint("de.mcs.jmeasurement.test.proxy.CTestProxy#echoNow"))
159: .getData("averageMSec").getValue();
160:
161: assertBetween(CTestProxy.waitEchoNow - 10,
162: CTestProxy.waitEchoNow + 10, value.intValue());
163:
164: value = (Long) ((DefaultMeasurePoint) MeasureFactory
165: .getMeasurePoint("de.mcs.jmeasurement.test.proxy.CTestProxy#echoNow"))
166: .getData("accessCount").getValue();
167:
168: assertEquals(0, value.intValue());
169:
170: value = (Long) ((DefaultMeasurePoint) MeasureFactory
171: .getMeasurePoint("de.mcs.jmeasurement.test.proxy.CTestProxy#exceptionNow"))
172: .getData("averageMSec").getValue();
173:
174: assertBetween(CTestProxy.waitException - 10,
175: CTestProxy.waitException + 10, value.intValue());
176:
177: value = (Long) ((DefaultMeasurePoint) MeasureFactory
178: .getMeasurePoint("de.mcs.jmeasurement.test.proxy.CTestProxy#exceptionNow"))
179: .getData("accessCount").getValue();
180:
181: assertEquals(0, value.intValue());
182:
183: }
184:
185: public void testMethodeCalls() throws MeasurementException {
186: MeasureFactory.clear();
187: ITestProxy testProxy = (ITestProxy) MeasureFactory
188: .registerInterface(new CTestProxy(), true, true);
189: DefaultMonitor monitor = new DefaultMonitor(
190: "echotest_mit_proxy");
191: monitor.start();
192: for (int i = 0; i < LOOP_COUNT; i++) {
193: testProxy.iTestMethode(TEST_STRING);
194: }
195: monitor.stop();
196:
197: MeasureFactory.setEnable(false);
198: testProxy = (ITestProxy) MeasureFactory.registerInterface(
199: new CTestProxy(), true, false);
200: DefaultMonitor monitor1 = new DefaultMonitor(
201: "echotest_mit_proxy_aus");
202: monitor1.start();
203: for (int i = 0; i < LOOP_COUNT; i++) {
204: String murks = testProxy.iTestMethode(TEST_STRING);
205: }
206: monitor1.stop();
207:
208: CTestProxy myProxy = new CTestProxy();
209:
210: DefaultMonitor monitor2 = new DefaultMonitor(
211: "echotest_ohne_proxy");
212: monitor2.start();
213: for (int i = 0; i < LOOP_COUNT; i++) {
214: String murks = myProxy.iTestMethode(TEST_STRING);
215: }
216: monitor2.stop();
217:
218: System.out.println("Test access time of the proxy methods.");
219: System.out
220: .println("loop count:" + Integer.toString(LOOP_COUNT));
221: System.out.println("total time:" + monitor.toString());
222: System.out.println("total time:" + monitor1.toString());
223: System.out.println("total time:" + monitor2.toString());
224:
225: MeasureFactory.setEnable(true);
226: for (int i = 0; i < 10000; i++) {
227: Monitor monitor3 = MeasureFactory.getMonitor("dummy_point_"
228: + Integer.toString(i));
229: }
230: DefaultMonitor dmonitor3 = new DefaultMonitor(
231: "accesstest_with_factory_on");
232: dmonitor3.start();
233: for (int i = 0; i < LOOP_COUNT; i++) {
234: Monitor monitor3 = MeasureFactory
235: .getMonitor("accesstest_with_factory_on");
236: monitor3.start();
237: monitor3.stop();
238: }
239: dmonitor3.stop();
240:
241: MeasureFactory.setEnable(false);
242: DefaultMonitor dmonitor4 = new DefaultMonitor(
243: "accesstest_with_factory_off");
244: dmonitor4.start();
245: for (int i = 0; i < LOOP_COUNT; i++) {
246: Monitor monitor3 = MeasureFactory
247: .getMonitor("accesstest_with_factory_off");
248: monitor3.start();
249: monitor3.stop();
250: }
251: dmonitor4.stop();
252:
253: System.out.println();
254: System.out.println("Test access time of factory methods.");
255: System.out
256: .println("loop count:" + Integer.toString(LOOP_COUNT));
257: System.out.println("total time:" + dmonitor3.toString());
258: System.out.println("total time:" + dmonitor4.toString());
259:
260: }
261:
262: public static void main(String[] args) throws Exception {
263: ProxyTest proxyTest = new ProxyTest();
264: proxyTest.testProxy();
265: proxyTest.testMethodeCalls();
266: try {
267: MeasureFactory.saveToXMLFile("data.xml");
268: } catch (IOException e) {
269: // TODO Auto-generated catch block
270: e.printStackTrace();
271: } catch (SAXException e) {
272: // TODO Auto-generated catch block
273: e.printStackTrace();
274: }
275:
276: }
277: }
|