KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > gbean > Speed


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.gbean;
18
19 import java.lang.reflect.Method JavaDoc;
20
21 import net.sf.cglib.reflect.FastClass;
22 import org.apache.geronimo.gbean.runtime.RawInvoker;
23 import org.apache.geronimo.kernel.Kernel;
24 import org.apache.geronimo.kernel.KernelFactory;
25 import org.apache.geronimo.kernel.MockGBean;
26 import org.apache.geronimo.kernel.repository.Artifact;
27
28 /**
29  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
30  */

31 public class Speed {
32     private static final Object JavaDoc[] NO_ARGS = new Object JavaDoc[0];
33
34     public static void main(String JavaDoc[] ignored) throws Exception JavaDoc {
35         System.out.println("Do Nothing Timings");
36         System.out.println("------------------");
37         doNothingTimings();
38         System.out.println();
39         System.out.println();
40         System.out.println();
41         doNothingTimings();
42 // System.out.println("Echo Timings");
43
// System.out.println("-------------");
44
// echoTimings();
45
}
46
47     private static void doNothingTimings() throws Exception JavaDoc {
48         Method JavaDoc myMethod = MockGBean.class.getMethod("doNothing", null);
49
50         FastClass myFastClass = FastClass.create(MockGBean.class);
51         int myMethodIndex = myFastClass.getIndex("doNothing", new Class JavaDoc[0]);
52
53         MockGBean instance = new MockGBean("foo", 12);
54
55         // normal invoke
56
int iterations = 100000000;
57         String JavaDoc msg = "hhhh";
58         for (int j=0 ; j < 10; j++) {
59             for (int i = 0; i < iterations; i++) {
60                 msg= instance.echo(msg);
61             }
62         }
63         long start = System.currentTimeMillis();
64         for (int i = 0; i < iterations; i++) {
65             instance.doNothing();
66         }
67         long end = System.currentTimeMillis();
68         printResults("Normal", end, start, iterations);
69
70         // reflection
71
iterations = 1000000;
72         for (int i = 0; i < iterations; i++) {
73             myMethod.invoke(instance, null);
74         }
75         start = System.currentTimeMillis();
76         for (int i = 0; i < iterations; i++) {
77             myMethod.invoke(instance, null);
78         }
79         end = System.currentTimeMillis();
80         printResults("Reflection", end, start, iterations);
81
82         // fast class
83
iterations = 5000000;
84         for (int i = 0; i < iterations; i++) {
85             myFastClass.invoke(myMethodIndex, instance, null);
86         }
87         start = System.currentTimeMillis();
88         for (int i = 0; i < iterations; i++) {
89             myFastClass.invoke(myMethodIndex, instance, null);
90         }
91         end = System.currentTimeMillis();
92         printResults("FastClass", end, start, iterations);
93
94         // start a kernel
95
Kernel kernel = KernelFactory.newInstance().createKernel("speed");
96         kernel.boot();
97         AbstractName abstractName = kernel.getNaming().createRootName(new Artifact("test", "foo", "1", "car"), "test", "test");
98         GBeanData mockGBean = new GBeanData(abstractName, MockGBean.getGBeanInfo());
99         mockGBean.setAttribute("Name", "bar");
100         mockGBean.setAttribute("FinalInt", new Integer JavaDoc(57));
101         kernel.loadGBean(mockGBean, Speed.class.getClassLoader());
102         kernel.startGBean(abstractName);
103
104         // reflect proxy
105
// ProxyFactory vmProxyFactory = new VMProxyFactory(MyInterface.class);
106
// ProxyMethodInterceptor vmMethodInterceptor = vmProxyFactory.getMethodInterceptor();
107
// MyInterface vmProxy = (MyInterface) vmProxyFactory.create(vmMethodInterceptor);
108
// vmMethodInterceptor.connect(kernel.getMBeanServer(), objectName);
109
// iterations = 50000;
110
// for (int i = 0; i < iterations; i++) {
111
// vmProxy.doNothing();
112
// }
113
// start = System.currentTimeMillis();
114
// for (int i = 0; i < iterations; i++) {
115
// vmProxy.doNothing();
116
// }
117
// end = System.currentTimeMillis();
118
// printResults("ReflectionProxy", end, start, iterations);
119

120         // cglib proxy (front half)
121
/*
122         ProxyFactory frontCGLibProxyFactory = new CGLibProxyFactory(MyInterface.class);
123         ProxyMethodInterceptor frontCGLibMethodInterceptor = new ProxyMethodInterceptor(MyInterface.class);
124         Class enhancedType = frontCGLibProxyFactory.create(frontCGLibMethodInterceptor).getClass();
125         frontCGLibMethodInterceptor = new ProxyMethodInterceptor(enhancedType) {
126             public Object intercept(Object object, Method method, Object[] args, MethodProxy proxy) throws Throwable {
127                 return null;
128             }
129         };
130         MyInterface frontCGLibProxy = (MyInterface) frontCGLibProxyFactory.create(frontCGLibMethodInterceptor);
131         frontCGLibMethodInterceptor.connect(kernel.getMBeanServer(), objectName);
132         iterations = 100000000;
133         for (int i = 0; i < iterations; i++) {
134             frontCGLibProxy.doNothing();
135         }
136         start = System.currentTimeMillis();
137         for (int i = 0; i < iterations; i++) {
138             frontCGLibProxy.doNothing();
139         }
140         end = System.currentTimeMillis();
141         printResults("Front CGLibProxy", end, start, iterations);
142 */

143
144         // Raw Invoker
145
RawInvoker rawInvoker = (RawInvoker) kernel.getAttribute(mockGBean.getAbstractName(), "$$RAW_INVOKER$$");
146         int rawIndex = ((Integer JavaDoc) rawInvoker.getOperationIndex().get(new GOperationSignature("doNothing", new String JavaDoc[0]))).intValue();
147         iterations = 2000000;
148         for (int i = 0; i < iterations; i++) {
149             rawInvoker.invoke(rawIndex, NO_ARGS);
150         }
151         start = System.currentTimeMillis();
152         for (int i = 0; i < iterations; i++) {
153             rawInvoker.invoke(rawIndex, NO_ARGS);
154         }
155         end = System.currentTimeMillis();
156         printResults("Raw Invoker", end, start, iterations);
157
158 // // cglib proxy
159
// ProxyFactory cgLibProxyFactory = new CGLibProxyFactory(MyInterface.class);
160
// ProxyMethodInterceptor cgLibMethodInterceptor = cgLibProxyFactory.getMethodInterceptor();
161
// MyInterface cgLibProxy = (MyInterface) cgLibProxyFactory.create(cgLibMethodInterceptor);
162
// cgLibMethodInterceptor.connect(kernel.getMBeanServer(), objectName);
163
// iterations = 1000000;
164
// for (int i = 0; i < iterations; i++) {
165
// cgLibProxy.doNothing();
166
// }
167
// start = System.currentTimeMillis();
168
// for (int i = 0; i < iterations; i++) {
169
// cgLibProxy.doNothing();
170
// }
171
// end = System.currentTimeMillis();
172
// printResults("CGLibProxy", end, start, iterations);
173
}
174
175     public static void echoTimings() throws Exception JavaDoc {
176         Method JavaDoc myMethod = MockGBean.class.getMethod("echo", new Class JavaDoc[]{String JavaDoc.class});
177
178         FastClass myFastClass = FastClass.create(MockGBean.class);
179         int myMethodIndex = myFastClass.getIndex("echo", new Class JavaDoc[]{String JavaDoc.class});
180         String JavaDoc msg = "Some message";
181         Object JavaDoc[] args = new Object JavaDoc[]{msg};
182         String JavaDoc result;
183
184         MockGBean instance = new MockGBean("foo", 12);
185
186
187         // normal invoke
188
int iterations = 100000000;
189         for (int i = 0; i < iterations; i++) {
190             result = instance.echo(msg);
191         }
192         long start = System.currentTimeMillis();
193         for (int i = 0; i < iterations; i++) {
194             result = instance.echo(msg);
195         }
196         long end = System.currentTimeMillis();
197         printResults("Normal", end, start, iterations);
198
199         // reflection
200
iterations = 10000000;
201         for (int i = 0; i < iterations; i++) {
202             result = (String JavaDoc) myMethod.invoke(instance, args);
203         }
204         start = System.currentTimeMillis();
205         for (int i = 0; i < iterations; i++) {
206             result = (String JavaDoc) myMethod.invoke(instance, args);
207         }
208         end = System.currentTimeMillis();
209         printResults("Reflection", end, start, iterations);
210
211         // fast class
212
iterations = 10000000;
213         for (int i = 0; i < iterations; i++) {
214             result = (String JavaDoc) myFastClass.invoke(myMethodIndex, instance, args);
215         }
216         start = System.currentTimeMillis();
217         for (int i = 0; i < iterations; i++) {
218             result = (String JavaDoc) myFastClass.invoke(myMethodIndex, instance, args);
219         }
220         end = System.currentTimeMillis();
221         printResults("FastClass", end, start, iterations);
222
223         // start a kernel
224
Kernel kernel = KernelFactory.newInstance().createKernel("speed");
225         kernel.boot();
226         AbstractName abstractName = kernel.getNaming().createRootName(new Artifact("test", "foo", "1", "car"), "test", "test");
227         GBeanData mockGBean = new GBeanData(abstractName, MockGBean.getGBeanInfo());
228         mockGBean.setAttribute("Name", "bar");
229         mockGBean.setAttribute("FinalInt", new Integer JavaDoc(57));
230         kernel.loadGBean(mockGBean, Speed.class.getClassLoader());
231         kernel.startGBean(mockGBean.getAbstractName());
232
233         // reflect proxy
234
// ProxyFactory vmProxyFactory = new VMProxyFactory(MyInterface.class);
235
// ProxyMethodInterceptor vmMethodInterceptor = vmProxyFactory.getMethodInterceptor();
236
// MyInterface vmProxy = (MyInterface) vmProxyFactory.create(vmMethodInterceptor);
237
// vmMethodInterceptor.connect(kernel.getMBeanServer(), objectName);
238
// iterations = 50000;
239
// for (int i = 0; i < iterations; i++) {
240
// result = vmProxy.echo(msg);
241
// }
242
// start = System.currentTimeMillis();
243
// for (int i = 0; i < iterations; i++) {
244
// result = vmProxy.echo(msg);
245
// }
246
// end = System.currentTimeMillis();
247
// printResults("ReflectionProxy", end, start, iterations);
248

249 // // cglib proxy
250
// ProxyFactory cgLibProxyFactory = new CGLibProxyFactory(MyInterface.class);
251
// ProxyMethodInterceptor cgLibMethodInterceptor = cgLibProxyFactory.getMethodInterceptor();
252
// MyInterface cgLibProxy = (MyInterface) cgLibProxyFactory.create(cgLibMethodInterceptor);
253
// cgLibMethodInterceptor.connect(kernel.getMBeanServer(), objectName);
254
// iterations = 1000000;
255
// for (int i = 0; i < iterations; i++) {
256
// result = cgLibProxy.echo(msg);
257
// }
258
// start = System.currentTimeMillis();
259
// for (int i = 0; i < iterations; i++) {
260
// result = cgLibProxy.echo(msg);
261
// }
262
// end = System.currentTimeMillis();
263
// printResults("CGLibProxy", end, start, iterations);
264
}
265
266     private static void printResults(String JavaDoc invocationType, long end, long start, int iterations) {
267         if(end - start < 400) {
268             System.out.println(invocationType + ": elapse time to short to calculate cost (total " + (end - start) + "ms)");
269         } else {
270             System.out.println(invocationType + ": " + ((end - start) * 1000000.0 / iterations) + "ns (total " + (end - start) + "ms)");
271         }
272     }
273
274     public static interface MyInterface {
275         void doNothing();
276         String JavaDoc echo(String JavaDoc msg);
277     }
278 }
279
Popular Tags