KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > test > jvm15 > TestMemthodInvokation


1 package org.exoplatform.test.jvm15;
2
3 import java.lang.management.ManagementFactory JavaDoc;
4 import java.lang.management.ThreadMXBean JavaDoc;
5 import java.lang.reflect.Method JavaDoc;
6 import java.util.HashMap JavaDoc;
7 import java.util.Map JavaDoc;
8 import junit.framework.TestCase;
9 import net.sf.cglib.reflect.FastClass;
10 import net.sf.cglib.reflect.FastMethod;
11 /**
12  * Apr 22, 2004
13  * @author: Tuan Nguyen
14  * @email: tuan08@users.sourceforge.net
15  * @version: $Id: TestBenchMark.java,v 1.1 2004/10/11 23:27:27 tuan08 Exp $
16  **/

17 public class TestMemthodInvokation extends TestCase {
18     private ThreadMXBean JavaDoc threadBean_ ;
19   
20   public TestMemthodInvokation() {
21     threadBean_ = ManagementFactory.getThreadMXBean() ;
22   }
23   
24   public void testBenchMark() throws Exception JavaDoc {
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 JavaDoc methodName, int numOfCall) throws Exception JavaDoc {
93         Dummy dummy = new Dummy();
94       long start = threadBean_.getThreadCpuTime(Thread.currentThread().getId());
95       Class JavaDoc[] types = {String JavaDoc.class} ;
96         Object JavaDoc[] args = {"dummy"} ;
97         for (int i = 0; i < numOfCall; i++) {
98             Method JavaDoc 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 JavaDoc methodName, int numOfCall) throws Exception JavaDoc {
106         Dummy dummy = new Dummy();
107         Method JavaDoc method = dummy.getClass().getMethod(methodName, new Class JavaDoc[]{String JavaDoc.class});
108       Class JavaDoc[] types = {String JavaDoc.class} ;
109         Object JavaDoc[] 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 JavaDoc methodName, int numOfCall) throws Exception JavaDoc {
119         Dummy dummy = new Dummy();
120         Map JavaDoc map = new HashMap JavaDoc();
121         Method JavaDoc method = dummy.getClass().getMethod(methodName, new Class JavaDoc[]{String JavaDoc.class});
122         map.put(methodName, method);
123         Object JavaDoc[] args = {"dummy"} ;
124       long start = threadBean_.getThreadCpuTime(Thread.currentThread().getId());
125         for (int i = 0; i < numOfCall; i++) {
126             Method JavaDoc m = (Method JavaDoc) 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 JavaDoc methodName, int numOfCall) throws Exception JavaDoc {
134     FastClass fc = FastClass.create(Dummy.class);
135     Dummy dummy = new Dummy();
136     Object JavaDoc[] args = {"dummy"} ;
137     Class JavaDoc[] emptyType = {} ;
138     Object JavaDoc[] 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 JavaDoc methodName, int numOfCall) throws Exception JavaDoc {
148     Class JavaDoc[] emptyType = {} ;
149     Object JavaDoc[] emptyArgs = {} ;
150     FastClass fc = FastClass.create(Dummy.class);
151     FastMethod fm = fc.getMethod(methodName, emptyType );
152     Dummy dummy = new Dummy();
153     Object JavaDoc[] args = {"dummy"} ;
154     long start = threadBean_.getThreadCpuTime(Thread.currentThread().getId());
155     for (int i = 0; i < numOfCall; i++) {
156       Object JavaDoc 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 JavaDoc 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