KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > weavebench > GenerateClasses


1 /**************************************************************************************
2  * Copyright (c) Jonas Bonér, Alexandre Vasseur. All rights reserved. *
3  * http://aspectwerkz.codehaus.org *
4  * ---------------------------------------------------------------------------------- *
5  * The software in this package is published under the terms of the LGPL license *
6  * a copy of which has been included with this distribution in the license.txt file. *
7  **************************************************************************************/

8 package test.weavebench;
9
10 import org.codehaus.aspectwerkz.transform.inlining.AsmHelper;
11 import org.codehaus.aspectwerkz.transform.AspectWerkzPreProcessor;
12 import org.codehaus.aspectwerkz.hook.impl.WeavingClassLoader;
13 import org.codehaus.aspectwerkz.definition.SystemDefinition;
14 import org.codehaus.aspectwerkz.definition.DeploymentScope;
15 import org.codehaus.aspectwerkz.definition.SystemDefinitionContainer;
16 import org.codehaus.aspectwerkz.definition.DefinitionParserHelper;
17 import org.objectweb.asm.ClassVisitor;
18 import org.objectweb.asm.Constants;
19 import org.objectweb.asm.Type;
20 import org.objectweb.asm.CodeVisitor;
21 import org.objectweb.asm.ClassWriter;
22
23 import java.net.URL JavaDoc;
24 import java.net.URLClassLoader JavaDoc;
25 import java.io.File JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Set JavaDoc;
28 import java.lang.reflect.Method JavaDoc;
29
30 /**
31  * @author <a HREF="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
32  */

33 public class GenerateClasses implements Constants {
34
35     private final static String JavaDoc DUMP_DIR = "_dump2";
36
37     private final static String JavaDoc CLASS_NAME_PREFIX = "test/weavebench/Generated_";
38
39     public int m_classCount;
40
41     public int m_count;
42
43     public GenerateClasses(int classCount, int methodCount) {
44         m_classCount = classCount;
45         m_count = methodCount;
46     }
47
48     public void generate() throws Throwable JavaDoc {
49         for (int i = 0; i < m_classCount; i++) {
50             ClassWriter cv = AsmHelper.newClassWriter(true);
51
52             String JavaDoc className = CLASS_NAME_PREFIX + i;
53             cv.visit(
54                     AsmHelper.JAVA_VERSION,
55                     ACC_PUBLIC + ACC_SUPER + ACC_SYNTHETIC,
56                     className,
57                     Object JavaDoc.class.getName().replace('.', '/'),
58                     new String JavaDoc[]{IGenerated.class.getName().replace('.', '/')},
59                     null
60             );
61
62             CodeVisitor mv = cv.visitMethod(
63                     ACC_PUBLIC,
64                     "<init>",
65                     "()V",
66                     new String JavaDoc[0],
67                     null
68             );
69             mv.visitVarInsn(ALOAD, 0);
70             mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
71             mv.visitVarInsn(ALOAD, 0);
72             mv.visitInsn(RETURN);
73             mv.visitMaxs(0, 0);
74
75             for (int m = 0; m < m_count; m++) {
76                 mv = cv.visitMethod(
77                         (m==0)?ACC_PUBLIC:
78                         ACC_PRIVATE,//TODO change to private to have wrapper, public for no wrappers
79
"method_" + m,
80                         "()I",
81                         new String JavaDoc[0],
82                         null
83                 );
84
85                 // call next method
86
if (m != m_count-1) {
87                     mv.visitVarInsn(ALOAD, 0);
88                     mv.visitMethodInsn(INVOKEVIRTUAL, className, "method_" + (m+1), "()I");
89                 }
90                 AsmHelper.loadIntegerConstant(mv, m);
91                 mv.visitInsn(IRETURN);
92                 mv.visitMaxs(0, 0);
93             }
94
95             cv.visitEnd();
96
97             AsmHelper.dumpClass(DUMP_DIR, className, cv);
98         }
99     }
100
101     public static void main(String JavaDoc args[]) throws Throwable JavaDoc {
102         int CLASS_COUNT = 100;
103         int METHOD_COUNT = 100;
104         int JP_COUNT = (METHOD_COUNT*2 -1/*last method has no call jp*/ + 1/*init exec*/)*CLASS_COUNT;
105         GenerateClasses me = new GenerateClasses(CLASS_COUNT, METHOD_COUNT);
106
107         System.out.println("********* Bench for");
108         System.out.println(" classes: " + CLASS_COUNT);
109         System.out.println(" methods: " + METHOD_COUNT);
110         System.out.println(" jps: " + JP_COUNT);
111         System.out.println("*************************************");
112
113         me.generate();
114
115         ClassLoader JavaDoc custom_1 = new URLClassLoader JavaDoc(
116                 new URL JavaDoc[]{(new File JavaDoc(DUMP_DIR)).toURL()},
117                 GenerateClasses.class.getClassLoader()
118         );
119         bench("No weaver hooked in", custom_1, CLASS_COUNT, 0);
120
121         ClassLoader JavaDoc custom_2 = new WeavingClassLoader(
122                 new URL JavaDoc[]{(new File JavaDoc(DUMP_DIR)).toURL()},
123                 GenerateClasses.class.getClassLoader()
124         );
125         bench("Weaver hooked in and match none", custom_2, CLASS_COUNT, 0);
126
127         ClassLoader JavaDoc custom_3 = new WeavingClassLoader(
128                 new URL JavaDoc[]{(new File JavaDoc(DUMP_DIR)).toURL()},
129                 GenerateClasses.class.getClassLoader()
130         );
131         SystemDefinition sd = SystemDefinitionContainer.getVirtualDefinitionAt(custom_3);
132         sd.addDeploymentScope(DeploymentScope.MATCH_ALL);
133         DefinitionParserHelper.attachDeploymentScopeDefsToVirtualAdvice(sd);
134         Set JavaDoc defs = new HashSet JavaDoc();
135         defs.add(sd);
136         SystemDefinitionContainer.deployDefinitions(custom_3, defs);
137         bench("Weaver hooked in and match all", custom_3, CLASS_COUNT, JP_COUNT);
138
139     }
140
141     public static void bench(String JavaDoc label, ClassLoader JavaDoc loader, int classCount, int jpCount) throws Throwable JavaDoc {
142         System.out.println("*************************************");
143         System.out.print(label);
144         System.out.println(" ");
145         long t = System.currentTimeMillis();
146         Class JavaDoc[] classes = new Class JavaDoc[classCount];
147         for (int i = 0; i < classCount; i++) {
148             classes[i] = Class.forName((CLASS_NAME_PREFIX+i).replace('/','.'), false, loader);
149         }
150
151         System.out.println(" Total load time = " + (System.currentTimeMillis() - t));
152
153         long t2 = System.currentTimeMillis();
154         for (int i = 0; i < classCount; i++) {
155             Class JavaDoc gen = classes[i];
156             Method JavaDoc m0 = gen.getMethod("method_0", new Class JavaDoc[]{});
157             Object JavaDoc res = m0.invoke(gen.newInstance(), new Object JavaDoc[]{});
158             System.out.print(i);
159         }
160         System.out.println("");
161         long execTimeNotWarmedUp = System.currentTimeMillis() - t2;
162         System.out.println(" Exec time = " + execTimeNotWarmedUp);
163         System.out.println(" Total time = " + (System.currentTimeMillis() - t));
164         if (jpCount > 0)
165             System.out.println(" Exec / jp ratio (ns) = " + execTimeNotWarmedUp * 1000 / jpCount);
166
167         t2 = System.currentTimeMillis();
168         for (int i = 0; i < classCount; i++) {
169             Class JavaDoc gen = classes[i];
170             Method JavaDoc m0 = gen.getMethod("method_0", new Class JavaDoc[]{});
171             Object JavaDoc res = m0.invoke(gen.newInstance(), new Object JavaDoc[]{});
172         }
173         System.out.println(" Exec time warmed up = " + (System.currentTimeMillis() - t2));
174     }
175
176
177 }
178
Popular Tags