1 17 package org.apache.geronimo.gbean; 18 19 import java.lang.reflect.Method ; 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 31 public class Speed { 32 private static final Object [] NO_ARGS = new Object [0]; 33 34 public static void main(String [] ignored) throws Exception { 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 } 46 47 private static void doNothingTimings() throws Exception { 48 Method myMethod = MockGBean.class.getMethod("doNothing", null); 49 50 FastClass myFastClass = FastClass.create(MockGBean.class); 51 int myMethodIndex = myFastClass.getIndex("doNothing", new Class [0]); 52 53 MockGBean instance = new MockGBean("foo", 12); 54 55 int iterations = 100000000; 57 String 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 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 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 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 (57)); 101 kernel.loadGBean(mockGBean, Speed.class.getClassLoader()); 102 kernel.startGBean(abstractName); 103 104 120 143 144 RawInvoker rawInvoker = (RawInvoker) kernel.getAttribute(mockGBean.getAbstractName(), "$$RAW_INVOKER$$"); 146 int rawIndex = ((Integer ) rawInvoker.getOperationIndex().get(new GOperationSignature("doNothing", new String [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 } 174 175 public static void echoTimings() throws Exception { 176 Method myMethod = MockGBean.class.getMethod("echo", new Class []{String .class}); 177 178 FastClass myFastClass = FastClass.create(MockGBean.class); 179 int myMethodIndex = myFastClass.getIndex("echo", new Class []{String .class}); 180 String msg = "Some message"; 181 Object [] args = new Object []{msg}; 182 String result; 183 184 MockGBean instance = new MockGBean("foo", 12); 185 186 187 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 iterations = 10000000; 201 for (int i = 0; i < iterations; i++) { 202 result = (String ) myMethod.invoke(instance, args); 203 } 204 start = System.currentTimeMillis(); 205 for (int i = 0; i < iterations; i++) { 206 result = (String ) myMethod.invoke(instance, args); 207 } 208 end = System.currentTimeMillis(); 209 printResults("Reflection", end, start, iterations); 210 211 iterations = 10000000; 213 for (int i = 0; i < iterations; i++) { 214 result = (String ) myFastClass.invoke(myMethodIndex, instance, args); 215 } 216 start = System.currentTimeMillis(); 217 for (int i = 0; i < iterations; i++) { 218 result = (String ) myFastClass.invoke(myMethodIndex, instance, args); 219 } 220 end = System.currentTimeMillis(); 221 printResults("FastClass", end, start, iterations); 222 223 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 (57)); 230 kernel.loadGBean(mockGBean, Speed.class.getClassLoader()); 231 kernel.startGBean(mockGBean.getAbstractName()); 232 233 249 } 265 266 private static void printResults(String 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 echo(String msg); 277 } 278 } 279 | Popular Tags |