001: /*
002: * The contents of this file are subject to the Sapient Public License
003: * Version 1.0 (the "License"); you may not use this file except in compliance
004: * with the License. You may obtain a copy of the License at
005: * http://carbon.sf.net/License.html.
006: *
007: * Software distributed under the License is distributed on an "AS IS" basis,
008: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
009: * the specific language governing rights and limitations under the License.
010: *
011: * The Original Code is The Carbon Component Framework.
012: *
013: * The Initial Developer of the Original Code is Sapient Corporation
014: *
015: * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
016: */
017:
018: package org.sape.carbon.core.test;
019:
020: import java.lang.reflect.InvocationHandler;
021: import java.lang.reflect.Method;
022: import java.lang.reflect.Proxy;
023:
024: /**
025: *
026: * @author ghinkl
027: */
028: public class DynamicProxyPerformance {
029:
030: public static void main(String[] args)
031: throws ClassNotFoundException {
032:
033: Something mySomething = new DynamicProxyPerformance.IH();
034:
035: Something myDynamicSomething = (Something) Proxy
036: .newProxyInstance(Something.class.getClassLoader(),
037: new Class[] { Something.class },
038: (InvocationHandler) mySomething);
039:
040: final int ITERATIONS = 10000000;
041: System.out.println("Testing " + ITERATIONS + " iterations");
042:
043: /* ++ Test 1: For static speed ++ */
044: long start = System.currentTimeMillis();
045: for (int i = 0; i < ITERATIONS; i++) {
046: mySomething.doSomething();
047: }
048: long time1 = (System.currentTimeMillis() - start);
049: System.out.println("Static finished at " + mySomething
050: + " in: " + time1 + "ms");
051:
052: /* ++ Test 2: For dynamic proxy speed ++ */
053: start = System.currentTimeMillis();
054: for (int i = 0; i < ITERATIONS; i++) {
055: myDynamicSomething.doSomething();
056: }
057: long time2 = (System.currentTimeMillis() - start);
058: System.out.println("Dynamic finished at " + mySomething
059: + " in: " + time2 + "ms");
060: System.out.println("Speed of 1 is "
061: + ((((double) time2)) / ((double) time1) * 100D)
062: + "% of 2");
063:
064: /* ++ Test 3: For reflection speed ++ */
065: Method m = null;
066: try {
067: m = mySomething.getClass().getMethod("doSomething",
068: new Class[] {});
069: } catch (Exception e) {
070: }
071:
072: start = System.currentTimeMillis();
073: try {
074: for (int i = 0; i < ITERATIONS; i++) {
075: m.invoke(mySomething, new Object[] {});
076: }
077: } catch (Exception e) {
078: }
079:
080: long time3 = (System.currentTimeMillis() - start);
081: System.out.println("Reflection finished at " + mySomething
082: + " in: " + time3 + "ms");
083: System.out.println("Speed of 2 is "
084: + ((((double) time3)) / ((double) time2) * 100D)
085: + "% of 3");
086: }
087:
088: public static interface Something {
089: void doSomething();
090: }
091:
092: public static class IH implements InvocationHandler, Something {
093: private Method theMethod = null;
094:
095: public Object invoke(Object obj, Method method, Object[] obj2)
096: throws java.lang.Throwable {
097:
098: /* The quickest way will be to do cached method comparisons */
099:
100: if (theMethod == null)
101: theMethod = method;
102:
103: //if (method.getName().equals("doSomething")) {
104: if (method == theMethod) {
105: doSomething();
106: }
107: return null;
108: }
109:
110: public int x = 0;
111:
112: public void doSomething() {
113: ++x;
114: }
115:
116: public String toString() {
117: return "(" + x + ")";
118: }
119: }
120: }
|