KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > core > test > DynamicProxyPerformance


1 /*
2  * The contents of this file are subject to the Sapient Public License
3  * Version 1.0 (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://carbon.sf.net/License.html.
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is The Carbon Component Framework.
12  *
13  * The Initial Developer of the Original Code is Sapient Corporation
14  *
15  * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
16  */

17
18 package org.sape.carbon.core.test;
19
20 import java.lang.reflect.InvocationHandler JavaDoc;
21 import java.lang.reflect.Method JavaDoc;
22 import java.lang.reflect.Proxy JavaDoc;
23
24 /**
25  *
26  * @author ghinkl
27  */

28 public class DynamicProxyPerformance {
29
30     public static void main(String JavaDoc[] args) throws ClassNotFoundException JavaDoc {
31
32         Something mySomething = new DynamicProxyPerformance.IH();
33
34         Something myDynamicSomething =
35             (Something)
36             Proxy.newProxyInstance(
37                 Something.class.getClassLoader(),
38                 new Class JavaDoc[] {Something.class},
39                 (InvocationHandler JavaDoc) mySomething);
40
41         final int ITERATIONS = 10000000;
42         System.out.println("Testing " + ITERATIONS + " iterations");
43
44         /* ++ Test 1: For static speed ++ */
45         long start = System.currentTimeMillis();
46         for (int i = 0; i < ITERATIONS; i++) {
47             mySomething.doSomething();
48         }
49         long time1 = (System.currentTimeMillis() - start);
50         System.out.println("Static finished at " + mySomething + " in: " + time1 + "ms");
51
52
53         /* ++ Test 2: For dynamic proxy speed ++ */
54         start = System.currentTimeMillis();
55         for (int i = 0; i < ITERATIONS; i++) {
56             myDynamicSomething.doSomething();
57         }
58         long time2 = (System.currentTimeMillis() - start);
59         System.out.println("Dynamic finished at " + mySomething + " in: " + time2 + "ms");
60         System.out.println("Speed of 1 is " + ((((double) time2))/((double) time1)*100D) + "% of 2");
61
62
63         /* ++ Test 3: For reflection speed ++ */
64         Method JavaDoc m = null;
65         try {
66             m = mySomething.getClass().getMethod("doSomething",new Class JavaDoc[] {});
67         } catch (Exception JavaDoc e) { }
68
69         start = System.currentTimeMillis();
70         try {
71             for (int i = 0; i < ITERATIONS; i++) {
72                     m.invoke(mySomething,new Object JavaDoc[] { });
73             }
74         } catch (Exception JavaDoc e) { }
75
76         long time3 = (System.currentTimeMillis() - start);
77         System.out.println("Reflection finished at " + mySomething + " in: " + time3 + "ms");
78         System.out.println("Speed of 2 is " + ((((double) time3))/((double) time2)*100D) + "% of 3");
79     }
80
81     public static interface Something {
82         void doSomething();
83     }
84
85
86     public static class IH implements InvocationHandler JavaDoc,Something {
87         private Method JavaDoc theMethod = null;
88         public Object JavaDoc invoke(Object JavaDoc obj, Method JavaDoc method, Object JavaDoc[] obj2)
89         throws java.lang.Throwable JavaDoc {
90
91             /* The quickest way will be to do cached method comparisons */
92
93             if (theMethod == null)
94                 theMethod = method;
95
96             //if (method.getName().equals("doSomething")) {
97
if (method == theMethod) {
98                 doSomething();
99             }
100             return null;
101         }
102
103         public int x = 0;
104         public void doSomething() {
105             ++x;
106         }
107
108         public String JavaDoc toString() {
109             return "(" + x + ")";
110         }
111     }
112 }
Popular Tags