1 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 ; 24 import java.net.URLClassLoader ; 25 import java.io.File ; 26 import java.util.HashSet ; 27 import java.util.Set ; 28 import java.lang.reflect.Method ; 29 30 33 public class GenerateClasses implements Constants { 34 35 private final static String DUMP_DIR = "_dump2"; 36 37 private final static String 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 { 49 for (int i = 0; i < m_classCount; i++) { 50 ClassWriter cv = AsmHelper.newClassWriter(true); 51 52 String className = CLASS_NAME_PREFIX + i; 53 cv.visit( 54 AsmHelper.JAVA_VERSION, 55 ACC_PUBLIC + ACC_SUPER + ACC_SYNTHETIC, 56 className, 57 Object .class.getName().replace('.', '/'), 58 new String []{IGenerated.class.getName().replace('.', '/')}, 59 null 60 ); 61 62 CodeVisitor mv = cv.visitMethod( 63 ACC_PUBLIC, 64 "<init>", 65 "()V", 66 new String [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, "method_" + m, 80 "()I", 81 new String [0], 82 null 83 ); 84 85 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 args[]) throws Throwable { 102 int CLASS_COUNT = 100; 103 int METHOD_COUNT = 100; 104 int JP_COUNT = (METHOD_COUNT*2 -1 + 1)*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 custom_1 = new URLClassLoader ( 116 new URL []{(new File (DUMP_DIR)).toURL()}, 117 GenerateClasses.class.getClassLoader() 118 ); 119 bench("No weaver hooked in", custom_1, CLASS_COUNT, 0); 120 121 ClassLoader custom_2 = new WeavingClassLoader( 122 new URL []{(new File (DUMP_DIR)).toURL()}, 123 GenerateClasses.class.getClassLoader() 124 ); 125 bench("Weaver hooked in and match none", custom_2, CLASS_COUNT, 0); 126 127 ClassLoader custom_3 = new WeavingClassLoader( 128 new URL []{(new File (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 defs = new HashSet (); 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 label, ClassLoader loader, int classCount, int jpCount) throws Throwable { 142 System.out.println("*************************************"); 143 System.out.print(label); 144 System.out.println(" "); 145 long t = System.currentTimeMillis(); 146 Class [] classes = new Class [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 gen = classes[i]; 156 Method m0 = gen.getMethod("method_0", new Class []{}); 157 Object res = m0.invoke(gen.newInstance(), new Object []{}); 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 gen = classes[i]; 170 Method m0 = gen.getMethod("method_0", new Class []{}); 171 Object res = m0.invoke(gen.newInstance(), new Object []{}); 172 } 173 System.out.println(" Exec time warmed up = " + (System.currentTimeMillis() - t2)); 174 } 175 176 177 } 178 | Popular Tags |