1 package org.exoplatform.test.jvm15; 2 3 import java.lang.management.ManagementFactory ; 4 import java.lang.management.ThreadMXBean ; 5 import java.lang.reflect.Method ; 6 import java.util.HashMap ; 7 import java.util.Map ; 8 import junit.framework.TestCase; 9 import net.sf.cglib.reflect.FastClass; 10 import net.sf.cglib.reflect.FastMethod; 11 17 public class TestMemthodInvokation extends TestCase { 18 private ThreadMXBean threadBean_ ; 19 20 public TestMemthodInvokation() { 21 threadBean_ = ManagementFactory.getThreadMXBean() ; 22 } 23 24 public void testBenchMark() throws Exception { 25 for(int i = 1 ; i <= 0 ; i++) { 26 int loop = i * 5000000 ; 27 singleInstruction(loop) ; 28 emptyMethodCall(loop) ; 29 directCall(loop) ; 30 directSynchronizedCall(loop) ; 31 newObject(loop) ; 32 reflectionCall("setter", loop) ; 33 reflectionCall("setterSynchronized", loop) ; 34 reflectionMethodCall("setter", loop) ; 35 reflectionMethodCall("setterSynchronized", loop) ; 36 reflectionMethodCallCache("setter", loop) ; 37 reflectionMethodCallCache("setterSynchronized", loop) ; 38 reflectionMethodCallCGLIB("getter", loop) ; 39 reflectionFastMethod("getter", loop) ; 40 System.out.println("===========================================================") ; 41 } 42 } 43 44 private void singleInstruction(int numOfCall) { 45 long start = threadBean_.getThreadCpuTime(Thread.currentThread().getId()); 46 for (int i = 0; i < numOfCall; i++) { 47 int a = i ; 48 } 49 long end = threadBean_.getThreadCpuTime(Thread.currentThread().getId()); 50 printInfo("singleInstruction()", numOfCall, start, end); 51 } 52 53 private void emptyMethodCall(int numOfCall) { 54 Dummy dummy = new Dummy(); 55 long start = threadBean_.getThreadCpuTime(Thread.currentThread().getId()); 56 for (int i = 0; i < numOfCall; i++) { 57 dummy.empty(); 58 } 59 long end = threadBean_.getThreadCpuTime(Thread.currentThread().getId()); 60 printInfo("emptyMethodCall()", numOfCall, start, end); 61 } 62 63 private void directCall(int numOfCall) { 64 Dummy dummy = new Dummy(); 65 long start = threadBean_.getThreadCpuTime(Thread.currentThread().getId()); 66 for (int i = 0; i < numOfCall; i++) { 67 dummy.setter("dummy"); 68 } 69 long end = threadBean_.getThreadCpuTime(Thread.currentThread().getId()); 70 printInfo("directCall()", numOfCall, start, end) ; 71 } 72 73 private void newObject(int numOfCall) { 74 long start = threadBean_.getThreadCpuTime(Thread.currentThread().getId()); 75 for (int i = 0; i < numOfCall; i++) { 76 Dummy dummy = new Dummy(); 77 } 78 long end = threadBean_.getThreadCpuTime(Thread.currentThread().getId()); 79 printInfo("newObject()", numOfCall, start, end) ; 80 } 81 82 private void directSynchronizedCall(int numOfCall) { 83 Dummy dummy = new Dummy(); 84 long start = threadBean_.getThreadCpuTime(Thread.currentThread().getId()); 85 for (int i = 0; i < numOfCall; i++) { 86 dummy.setterSynchronized("dummy"); 87 } 88 long end = threadBean_.getThreadCpuTime(Thread.currentThread().getId()); 89 printInfo("directSynchronizedCall()", numOfCall, start, end) ; 90 } 91 92 private void reflectionCall(String methodName, int numOfCall) throws Exception { 93 Dummy dummy = new Dummy(); 94 long start = threadBean_.getThreadCpuTime(Thread.currentThread().getId()); 95 Class [] types = {String .class} ; 96 Object [] args = {"dummy"} ; 97 for (int i = 0; i < numOfCall; i++) { 98 Method method = dummy.getClass().getMethod(methodName, types); 99 method.invoke(dummy, args); 100 } 101 long end = threadBean_.getThreadCpuTime(Thread.currentThread().getId()); 102 printInfo("reflectionCall(), method: " + methodName, numOfCall, start, end) ; 103 } 104 105 private void reflectionMethodCall(String methodName, int numOfCall) throws Exception { 106 Dummy dummy = new Dummy(); 107 Method method = dummy.getClass().getMethod(methodName, new Class []{String .class}); 108 Class [] types = {String .class} ; 109 Object [] args = {"dummy"} ; 110 long start = threadBean_.getThreadCpuTime(Thread.currentThread().getId()); 111 for (int i = 0; i < numOfCall; i++) { 112 method.invoke(dummy, args); 113 } 114 long end = threadBean_.getThreadCpuTime(Thread.currentThread().getId()); 115 printInfo("reflectionMethodCall(), method: " + methodName, numOfCall, start, end) ; 116 } 117 118 private void reflectionMethodCallCache(String methodName, int numOfCall) throws Exception { 119 Dummy dummy = new Dummy(); 120 Map map = new HashMap (); 121 Method method = dummy.getClass().getMethod(methodName, new Class []{String .class}); 122 map.put(methodName, method); 123 Object [] args = {"dummy"} ; 124 long start = threadBean_.getThreadCpuTime(Thread.currentThread().getId()); 125 for (int i = 0; i < numOfCall; i++) { 126 Method m = (Method ) map.get(methodName); 127 m.invoke(dummy, args); 128 } 129 long end = threadBean_.getThreadCpuTime(Thread.currentThread().getId()) ; 130 printInfo("reflectionCallMethodCache(), method: " + methodName, numOfCall, start, end) ; 131 } 132 133 private void reflectionMethodCallCGLIB(String methodName, int numOfCall) throws Exception { 134 FastClass fc = FastClass.create(Dummy.class); 135 Dummy dummy = new Dummy(); 136 Object [] args = {"dummy"} ; 137 Class [] emptyType = {} ; 138 Object [] emptyArgs = {} ; 139 long start = threadBean_.getThreadCpuTime(Thread.currentThread().getId()); 140 for (int i = 0; i < numOfCall; i++) { 141 fc.invoke(methodName, emptyType, dummy, emptyArgs); 142 } 143 long end = threadBean_.getThreadCpuTime(Thread.currentThread().getId()); 144 printInfo("reflectionCallMethodCGLIB(), method: " + methodName, numOfCall, start, end) ; 145 } 146 147 private void reflectionFastMethod(String methodName, int numOfCall) throws Exception { 148 Class [] emptyType = {} ; 149 Object [] emptyArgs = {} ; 150 FastClass fc = FastClass.create(Dummy.class); 151 FastMethod fm = fc.getMethod(methodName, emptyType ); 152 Dummy dummy = new Dummy(); 153 Object [] args = {"dummy"} ; 154 long start = threadBean_.getThreadCpuTime(Thread.currentThread().getId()); 155 for (int i = 0; i < numOfCall; i++) { 156 Object o = fm.invoke(dummy, null); 157 } 158 long end = threadBean_.getThreadCpuTime(Thread.currentThread().getId()); 159 printInfo("reflectionFastMethod(), method: " + methodName, numOfCall, start, end) ; 160 } 161 162 private void printInfo(String method, int numOfCall, long start, long end) { 163 System.out.println("Call method " + method + " " + numOfCall + " times in " + ((double)(end - start))/(1000*1000) +"ms") ; 164 } 165 } 166 | Popular Tags |