001: /**************************************************************************************
002: * Copyright (c) Jonas Bonér, Alexandre Vasseur. All rights reserved. *
003: * http://aspectwerkz.codehaus.org *
004: * ---------------------------------------------------------------------------------- *
005: * The software in this package is published under the terms of the LGPL license *
006: * a copy of which has been included with this distribution in the license.txt file. *
007: **************************************************************************************/package test.weavebench;
008:
009: import org.codehaus.aspectwerkz.transform.inlining.AsmHelper;
010: import org.codehaus.aspectwerkz.transform.AspectWerkzPreProcessor;
011: import org.codehaus.aspectwerkz.hook.impl.WeavingClassLoader;
012: import org.codehaus.aspectwerkz.definition.SystemDefinition;
013: import org.codehaus.aspectwerkz.definition.DeploymentScope;
014: import org.codehaus.aspectwerkz.definition.SystemDefinitionContainer;
015: import org.codehaus.aspectwerkz.definition.DefinitionParserHelper;
016: import org.objectweb.asm.ClassVisitor;
017: import org.objectweb.asm.Constants;
018: import org.objectweb.asm.Type;
019: import org.objectweb.asm.CodeVisitor;
020: import org.objectweb.asm.ClassWriter;
021:
022: import java.net.URL;
023: import java.net.URLClassLoader;
024: import java.io.File;
025: import java.util.HashSet;
026: import java.util.Set;
027: import java.lang.reflect.Method;
028:
029: /**
030: * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
031: */
032: public class GenerateClasses implements Constants {
033:
034: private final static String DUMP_DIR = "_dump2";
035:
036: private final static String CLASS_NAME_PREFIX = "test/weavebench/Generated_";
037:
038: public int m_classCount;
039:
040: public int m_count;
041:
042: public GenerateClasses(int classCount, int methodCount) {
043: m_classCount = classCount;
044: m_count = methodCount;
045: }
046:
047: public void generate() throws Throwable {
048: for (int i = 0; i < m_classCount; i++) {
049: ClassWriter cv = AsmHelper.newClassWriter(true);
050:
051: String className = CLASS_NAME_PREFIX + i;
052: cv.visit(AsmHelper.JAVA_VERSION, ACC_PUBLIC + ACC_SUPER
053: + ACC_SYNTHETIC, className, Object.class.getName()
054: .replace('.', '/'), new String[] { IGenerated.class
055: .getName().replace('.', '/') }, null);
056:
057: CodeVisitor mv = cv.visitMethod(ACC_PUBLIC, "<init>",
058: "()V", new String[0], null);
059: mv.visitVarInsn(ALOAD, 0);
060: mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object",
061: "<init>", "()V");
062: mv.visitVarInsn(ALOAD, 0);
063: mv.visitInsn(RETURN);
064: mv.visitMaxs(0, 0);
065:
066: for (int m = 0; m < m_count; m++) {
067: mv = cv.visitMethod(
068: (m == 0) ? ACC_PUBLIC : ACC_PRIVATE,//TODO change to private to have wrapper, public for no wrappers
069: "method_" + m, "()I", new String[0], null);
070:
071: // call next method
072: if (m != m_count - 1) {
073: mv.visitVarInsn(ALOAD, 0);
074: mv.visitMethodInsn(INVOKEVIRTUAL, className,
075: "method_" + (m + 1), "()I");
076: }
077: AsmHelper.loadIntegerConstant(mv, m);
078: mv.visitInsn(IRETURN);
079: mv.visitMaxs(0, 0);
080: }
081:
082: cv.visitEnd();
083:
084: AsmHelper.dumpClass(DUMP_DIR, className, cv);
085: }
086: }
087:
088: public static void main(String args[]) throws Throwable {
089: int CLASS_COUNT = 100;
090: int METHOD_COUNT = 100;
091: int JP_COUNT = (METHOD_COUNT * 2 - 1/*last method has no call jp*/+ 1/*init exec*/)
092: * CLASS_COUNT;
093: GenerateClasses me = new GenerateClasses(CLASS_COUNT,
094: METHOD_COUNT);
095:
096: System.out.println("********* Bench for");
097: System.out.println(" classes: " + CLASS_COUNT);
098: System.out.println(" methods: " + METHOD_COUNT);
099: System.out.println(" jps: " + JP_COUNT);
100: System.out.println("*************************************");
101:
102: me.generate();
103:
104: ClassLoader custom_1 = new URLClassLoader(
105: new URL[] { (new File(DUMP_DIR)).toURL() },
106: GenerateClasses.class.getClassLoader());
107: bench("No weaver hooked in", custom_1, CLASS_COUNT, 0);
108:
109: ClassLoader custom_2 = new WeavingClassLoader(
110: new URL[] { (new File(DUMP_DIR)).toURL() },
111: GenerateClasses.class.getClassLoader());
112: bench("Weaver hooked in and match none", custom_2, CLASS_COUNT,
113: 0);
114:
115: ClassLoader custom_3 = new WeavingClassLoader(
116: new URL[] { (new File(DUMP_DIR)).toURL() },
117: GenerateClasses.class.getClassLoader());
118: SystemDefinition sd = SystemDefinitionContainer
119: .getVirtualDefinitionAt(custom_3);
120: sd.addDeploymentScope(DeploymentScope.MATCH_ALL);
121: DefinitionParserHelper
122: .attachDeploymentScopeDefsToVirtualAdvice(sd);
123: Set defs = new HashSet();
124: defs.add(sd);
125: SystemDefinitionContainer.deployDefinitions(custom_3, defs);
126: bench("Weaver hooked in and match all", custom_3, CLASS_COUNT,
127: JP_COUNT);
128:
129: }
130:
131: public static void bench(String label, ClassLoader loader,
132: int classCount, int jpCount) throws Throwable {
133: System.out.println("*************************************");
134: System.out.print(label);
135: System.out.println(" ");
136: long t = System.currentTimeMillis();
137: Class[] classes = new Class[classCount];
138: for (int i = 0; i < classCount; i++) {
139: classes[i] = Class.forName((CLASS_NAME_PREFIX + i).replace(
140: '/', '.'), false, loader);
141: }
142:
143: System.out.println(" Total load time = "
144: + (System.currentTimeMillis() - t));
145:
146: long t2 = System.currentTimeMillis();
147: for (int i = 0; i < classCount; i++) {
148: Class gen = classes[i];
149: Method m0 = gen.getMethod("method_0", new Class[] {});
150: Object res = m0.invoke(gen.newInstance(), new Object[] {});
151: System.out.print(i);
152: }
153: System.out.println("");
154: long execTimeNotWarmedUp = System.currentTimeMillis() - t2;
155: System.out.println(" Exec time = " + execTimeNotWarmedUp);
156: System.out.println(" Total time = "
157: + (System.currentTimeMillis() - t));
158: if (jpCount > 0)
159: System.out.println(" Exec / jp ratio (ns) = "
160: + execTimeNotWarmedUp * 1000 / jpCount);
161:
162: t2 = System.currentTimeMillis();
163: for (int i = 0; i < classCount; i++) {
164: Class gen = classes[i];
165: Method m0 = gen.getMethod("method_0", new Class[] {});
166: Object res = m0.invoke(gen.newInstance(), new Object[] {});
167: }
168: System.out.println(" Exec time warmed up = "
169: + (System.currentTimeMillis() - t2));
170: }
171:
172: }
|