001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of 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,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.gbean;
017:
018: import java.lang.reflect.Method;
019:
020: import net.sf.cglib.reflect.FastClass;
021: import org.apache.geronimo.gbean.runtime.RawInvoker;
022: import org.apache.geronimo.kernel.Kernel;
023: import org.apache.geronimo.kernel.KernelFactory;
024: import org.apache.geronimo.kernel.MockGBean;
025: import org.apache.geronimo.kernel.repository.Artifact;
026:
027: /**
028: * @version $Rev: 476049 $ $Date: 2006-11-16 20:35:17 -0800 (Thu, 16 Nov 2006) $
029: */
030: public class Speed {
031: private static final Object[] NO_ARGS = new Object[0];
032:
033: public static void main(String[] ignored) throws Exception {
034: System.out.println("Do Nothing Timings");
035: System.out.println("------------------");
036: doNothingTimings();
037: System.out.println();
038: System.out.println();
039: System.out.println();
040: doNothingTimings();
041: // System.out.println("Echo Timings");
042: // System.out.println("-------------");
043: // echoTimings();
044: }
045:
046: private static void doNothingTimings() throws Exception {
047: Method myMethod = MockGBean.class.getMethod("doNothing", null);
048:
049: FastClass myFastClass = FastClass.create(MockGBean.class);
050: int myMethodIndex = myFastClass.getIndex("doNothing",
051: new Class[0]);
052:
053: MockGBean instance = new MockGBean("foo", 12);
054:
055: // normal invoke
056: int iterations = 100000000;
057: String msg = "hhhh";
058: for (int j = 0; j < 10; j++) {
059: for (int i = 0; i < iterations; i++) {
060: msg = instance.echo(msg);
061: }
062: }
063: long start = System.currentTimeMillis();
064: for (int i = 0; i < iterations; i++) {
065: instance.doNothing();
066: }
067: long end = System.currentTimeMillis();
068: printResults("Normal", end, start, iterations);
069:
070: // reflection
071: iterations = 1000000;
072: for (int i = 0; i < iterations; i++) {
073: myMethod.invoke(instance, null);
074: }
075: start = System.currentTimeMillis();
076: for (int i = 0; i < iterations; i++) {
077: myMethod.invoke(instance, null);
078: }
079: end = System.currentTimeMillis();
080: printResults("Reflection", end, start, iterations);
081:
082: // fast class
083: iterations = 5000000;
084: for (int i = 0; i < iterations; i++) {
085: myFastClass.invoke(myMethodIndex, instance, null);
086: }
087: start = System.currentTimeMillis();
088: for (int i = 0; i < iterations; i++) {
089: myFastClass.invoke(myMethodIndex, instance, null);
090: }
091: end = System.currentTimeMillis();
092: printResults("FastClass", end, start, iterations);
093:
094: // start a kernel
095: Kernel kernel = KernelFactory.newInstance().createKernel(
096: "speed");
097: kernel.boot();
098: AbstractName abstractName = kernel.getNaming()
099: .createRootName(
100: new Artifact("test", "foo", "1", "car"),
101: "test", "test");
102: GBeanData mockGBean = new GBeanData(abstractName, MockGBean
103: .getGBeanInfo());
104: mockGBean.setAttribute("Name", "bar");
105: mockGBean.setAttribute("FinalInt", new Integer(57));
106: kernel.loadGBean(mockGBean, Speed.class.getClassLoader());
107: kernel.startGBean(abstractName);
108:
109: // reflect proxy
110: // ProxyFactory vmProxyFactory = new VMProxyFactory(MyInterface.class);
111: // ProxyMethodInterceptor vmMethodInterceptor = vmProxyFactory.getMethodInterceptor();
112: // MyInterface vmProxy = (MyInterface) vmProxyFactory.create(vmMethodInterceptor);
113: // vmMethodInterceptor.connect(kernel.getMBeanServer(), objectName);
114: // iterations = 50000;
115: // for (int i = 0; i < iterations; i++) {
116: // vmProxy.doNothing();
117: // }
118: // start = System.currentTimeMillis();
119: // for (int i = 0; i < iterations; i++) {
120: // vmProxy.doNothing();
121: // }
122: // end = System.currentTimeMillis();
123: // printResults("ReflectionProxy", end, start, iterations);
124:
125: // cglib proxy (front half)
126: /*
127: ProxyFactory frontCGLibProxyFactory = new CGLibProxyFactory(MyInterface.class);
128: ProxyMethodInterceptor frontCGLibMethodInterceptor = new ProxyMethodInterceptor(MyInterface.class);
129: Class enhancedType = frontCGLibProxyFactory.create(frontCGLibMethodInterceptor).getClass();
130: frontCGLibMethodInterceptor = new ProxyMethodInterceptor(enhancedType) {
131: public Object intercept(Object object, Method method, Object[] args, MethodProxy proxy) throws Throwable {
132: return null;
133: }
134: };
135: MyInterface frontCGLibProxy = (MyInterface) frontCGLibProxyFactory.create(frontCGLibMethodInterceptor);
136: frontCGLibMethodInterceptor.connect(kernel.getMBeanServer(), objectName);
137: iterations = 100000000;
138: for (int i = 0; i < iterations; i++) {
139: frontCGLibProxy.doNothing();
140: }
141: start = System.currentTimeMillis();
142: for (int i = 0; i < iterations; i++) {
143: frontCGLibProxy.doNothing();
144: }
145: end = System.currentTimeMillis();
146: printResults("Front CGLibProxy", end, start, iterations);
147: */
148:
149: // Raw Invoker
150: RawInvoker rawInvoker = (RawInvoker) kernel.getAttribute(
151: mockGBean.getAbstractName(), "$$RAW_INVOKER$$");
152: int rawIndex = ((Integer) rawInvoker.getOperationIndex().get(
153: new GOperationSignature("doNothing", new String[0])))
154: .intValue();
155: iterations = 2000000;
156: for (int i = 0; i < iterations; i++) {
157: rawInvoker.invoke(rawIndex, NO_ARGS);
158: }
159: start = System.currentTimeMillis();
160: for (int i = 0; i < iterations; i++) {
161: rawInvoker.invoke(rawIndex, NO_ARGS);
162: }
163: end = System.currentTimeMillis();
164: printResults("Raw Invoker", end, start, iterations);
165:
166: // // cglib proxy
167: // ProxyFactory cgLibProxyFactory = new CGLibProxyFactory(MyInterface.class);
168: // ProxyMethodInterceptor cgLibMethodInterceptor = cgLibProxyFactory.getMethodInterceptor();
169: // MyInterface cgLibProxy = (MyInterface) cgLibProxyFactory.create(cgLibMethodInterceptor);
170: // cgLibMethodInterceptor.connect(kernel.getMBeanServer(), objectName);
171: // iterations = 1000000;
172: // for (int i = 0; i < iterations; i++) {
173: // cgLibProxy.doNothing();
174: // }
175: // start = System.currentTimeMillis();
176: // for (int i = 0; i < iterations; i++) {
177: // cgLibProxy.doNothing();
178: // }
179: // end = System.currentTimeMillis();
180: // printResults("CGLibProxy", end, start, iterations);
181: }
182:
183: public static void echoTimings() throws Exception {
184: Method myMethod = MockGBean.class.getMethod("echo",
185: new Class[] { String.class });
186:
187: FastClass myFastClass = FastClass.create(MockGBean.class);
188: int myMethodIndex = myFastClass.getIndex("echo",
189: new Class[] { String.class });
190: String msg = "Some message";
191: Object[] args = new Object[] { msg };
192: String result;
193:
194: MockGBean instance = new MockGBean("foo", 12);
195:
196: // normal invoke
197: int iterations = 100000000;
198: for (int i = 0; i < iterations; i++) {
199: result = instance.echo(msg);
200: }
201: long start = System.currentTimeMillis();
202: for (int i = 0; i < iterations; i++) {
203: result = instance.echo(msg);
204: }
205: long end = System.currentTimeMillis();
206: printResults("Normal", end, start, iterations);
207:
208: // reflection
209: iterations = 10000000;
210: for (int i = 0; i < iterations; i++) {
211: result = (String) myMethod.invoke(instance, args);
212: }
213: start = System.currentTimeMillis();
214: for (int i = 0; i < iterations; i++) {
215: result = (String) myMethod.invoke(instance, args);
216: }
217: end = System.currentTimeMillis();
218: printResults("Reflection", end, start, iterations);
219:
220: // fast class
221: iterations = 10000000;
222: for (int i = 0; i < iterations; i++) {
223: result = (String) myFastClass.invoke(myMethodIndex,
224: instance, args);
225: }
226: start = System.currentTimeMillis();
227: for (int i = 0; i < iterations; i++) {
228: result = (String) myFastClass.invoke(myMethodIndex,
229: instance, args);
230: }
231: end = System.currentTimeMillis();
232: printResults("FastClass", end, start, iterations);
233:
234: // start a kernel
235: Kernel kernel = KernelFactory.newInstance().createKernel(
236: "speed");
237: kernel.boot();
238: AbstractName abstractName = kernel.getNaming()
239: .createRootName(
240: new Artifact("test", "foo", "1", "car"),
241: "test", "test");
242: GBeanData mockGBean = new GBeanData(abstractName, MockGBean
243: .getGBeanInfo());
244: mockGBean.setAttribute("Name", "bar");
245: mockGBean.setAttribute("FinalInt", new Integer(57));
246: kernel.loadGBean(mockGBean, Speed.class.getClassLoader());
247: kernel.startGBean(mockGBean.getAbstractName());
248:
249: // reflect proxy
250: // ProxyFactory vmProxyFactory = new VMProxyFactory(MyInterface.class);
251: // ProxyMethodInterceptor vmMethodInterceptor = vmProxyFactory.getMethodInterceptor();
252: // MyInterface vmProxy = (MyInterface) vmProxyFactory.create(vmMethodInterceptor);
253: // vmMethodInterceptor.connect(kernel.getMBeanServer(), objectName);
254: // iterations = 50000;
255: // for (int i = 0; i < iterations; i++) {
256: // result = vmProxy.echo(msg);
257: // }
258: // start = System.currentTimeMillis();
259: // for (int i = 0; i < iterations; i++) {
260: // result = vmProxy.echo(msg);
261: // }
262: // end = System.currentTimeMillis();
263: // printResults("ReflectionProxy", end, start, iterations);
264:
265: // // cglib proxy
266: // ProxyFactory cgLibProxyFactory = new CGLibProxyFactory(MyInterface.class);
267: // ProxyMethodInterceptor cgLibMethodInterceptor = cgLibProxyFactory.getMethodInterceptor();
268: // MyInterface cgLibProxy = (MyInterface) cgLibProxyFactory.create(cgLibMethodInterceptor);
269: // cgLibMethodInterceptor.connect(kernel.getMBeanServer(), objectName);
270: // iterations = 1000000;
271: // for (int i = 0; i < iterations; i++) {
272: // result = cgLibProxy.echo(msg);
273: // }
274: // start = System.currentTimeMillis();
275: // for (int i = 0; i < iterations; i++) {
276: // result = cgLibProxy.echo(msg);
277: // }
278: // end = System.currentTimeMillis();
279: // printResults("CGLibProxy", end, start, iterations);
280: }
281:
282: private static void printResults(String invocationType, long end,
283: long start, int iterations) {
284: if (end - start < 400) {
285: System.out
286: .println(invocationType
287: + ": elapse time to short to calculate cost (total "
288: + (end - start) + "ms)");
289: } else {
290: System.out.println(invocationType + ": "
291: + ((end - start) * 1000000.0 / iterations)
292: + "ns (total " + (end - start) + "ms)");
293: }
294: }
295:
296: public static interface MyInterface {
297: void doNothing();
298:
299: String echo(String msg);
300: }
301: }
|