001: /*
002: * Copyright 2003,2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package net.sf.cglib.reflect;
017:
018: import java.lang.reflect.Constructor;
019: import java.lang.reflect.Method;
020: import java.util.*;
021: import junit.framework.*;
022:
023: public class TestReflectPerf extends net.sf.cglib.CodeGenTestCase {
024: public interface IndexOf {
025: int indexOf(String s, int start);
026: }
027:
028: public void testReflectPerf() throws Throwable {
029: int iterations = 1000000;
030: System.out.println();
031: System.out.println("iteration count: " + iterations);
032:
033: String test = "abcabcabc";
034:
035: Class[] types = new Class[] { String.class, Integer.TYPE };
036: Method indexOf = String.class.getDeclaredMethod("indexOf",
037: types);
038: FastClass fc = FastClass.create(String.class);
039: FastMethod fm = fc.getMethod("indexOf", types);
040: int fidx = fm.getIndex();
041: Object[] args = new Object[] { "ab", new Integer(1) };
042:
043: IndexOf fast = (IndexOf) MethodDelegate.create(test, "indexOf",
044: IndexOf.class);
045:
046: int result;
047: long t1 = System.currentTimeMillis();
048: for (int i = 0; i < iterations; i++) {
049: result = ((Integer) fc.invoke("indexOf", types, test, args))
050: .intValue();
051: }
052: long t2 = System.currentTimeMillis();
053: for (int i = 0; i < iterations; i++) {
054: args = new Object[] { "ab", new Integer(1) };
055: result = ((Integer) indexOf.invoke(test, args)).intValue();
056: }
057: long t3 = System.currentTimeMillis();
058: for (int i = 0; i < iterations; i++) {
059: result = ((Integer) indexOf.invoke(test, args)).intValue();
060: }
061: long t4 = System.currentTimeMillis();
062: for (int i = 0; i < iterations; i++) {
063: args = new Object[] { "ab", new Integer(1) };
064: result = ((Integer) fm.invoke(test, args)).intValue();
065: }
066: long t5 = System.currentTimeMillis();
067: for (int i = 0; i < iterations; i++) {
068: result = ((Integer) fm.invoke(test, args)).intValue();
069: }
070: long t6 = System.currentTimeMillis();
071: for (int i = 0; i < iterations; i++) {
072: result = ((Integer) fc.invoke(fidx, test, args)).intValue();
073: }
074: long t7 = System.currentTimeMillis();
075: for (int i = 0; i < iterations; i++) {
076: result = fast.indexOf("ab", 1);
077: }
078: long t8 = System.currentTimeMillis();
079: for (int i = 0; i < iterations; i++) {
080: result = test.indexOf("ab", 1);
081: }
082: long t9 = System.currentTimeMillis();
083:
084: System.out.println("fc = " + (t2 - t1) + "\n"
085: + "reflect+args = " + (t3 - t2) + "\n"
086: + "reflect = " + (t4 - t3) + "\n"
087: + "fm+args = " + (t5 - t4) + "\n"
088: + "fm = " + (t6 - t5) + "\n"
089: + "fc w/idx = " + (t7 - t6) + "\n"
090: + "delegate = " + (t8 - t7) + "\n"
091: + "raw = " + (t9 - t8));
092: }
093:
094: public TestReflectPerf(String testName) {
095: super (testName);
096: }
097:
098: public static void main(String[] args) {
099: junit.textui.TestRunner.run(suite());
100: }
101:
102: public static Test suite() {
103: return new TestSuite(TestReflectPerf.class);
104: }
105:
106: public void perform(ClassLoader loader) throws Throwable {
107: }
108:
109: public void testFailOnMemoryLeak() throws Throwable {
110: }
111:
112: }
|