1 30 package org.objectweb.asm; 31 32 import java.io.File ; 33 import java.io.IOException ; 34 import java.io.InputStream ; 35 import java.io.FileOutputStream ; 36 import java.util.ArrayList ; 37 import java.util.Enumeration ; 38 import java.util.List ; 39 import java.util.zip.ZipEntry ; 40 import java.util.zip.ZipFile ; 41 import java.util.zip.ZipOutputStream ; 42 43 import org.objectweb.asm.ClassReader; 44 import org.objectweb.asm.ClassWriter; 45 import org.objectweb.asm.commons.EmptyVisitor; 46 import org.objectweb.asm.tree.ClassNode; 47 48 51 public abstract class ALLPerfTest extends ClassLoader { 52 53 private static ZipFile zip; 54 55 private static ZipOutputStream dst; 56 57 private static int mode; 58 59 private static int total; 60 61 private static int totalSize; 62 63 private static double[][] perfs; 64 65 static boolean compute; 66 67 static boolean skipDebug; 68 69 public static void main(String [] args) throws Exception { 70 ZipFile zip = new ZipFile (System.getProperty("java.home") 71 + "/lib/rt.jar"); 72 List classes = new ArrayList (); 73 74 Enumeration entries = zip.entries(); 75 while (entries.hasMoreElements()) { 76 ZipEntry e = (ZipEntry ) entries.nextElement(); 77 String s = e.getName(); 78 if (s.endsWith(".class")) { 79 s = s.substring(0, s.length() - 6).replace('/', '.'); 80 InputStream is = zip.getInputStream(e); 81 classes.add(readClass(is)); 82 } 83 } 84 85 for (int i = 0; i < 10; ++i) { 86 long t = System.currentTimeMillis(); 87 for (int j = 0; j < classes.size(); ++j) { 88 byte[] b = (byte[]) classes.get(j); 89 new ClassReader(b).accept(new EmptyVisitor(), false); 90 } 91 t = System.currentTimeMillis() - t; 92 System.out.println("Time to deserialize " + classes.size() 93 + " classes = " + t + " ms"); 94 } 95 96 for (int i = 0; i < 10; ++i) { 97 long t = System.currentTimeMillis(); 98 for (int j = 0; j < classes.size(); ++j) { 99 byte[] b = (byte[]) classes.get(j); 100 ClassWriter cw = new ClassWriter(false); 101 new ClassReader(b).accept(cw, false); 102 cw.toByteArray(); 103 } 104 t = System.currentTimeMillis() - t; 105 System.out.println("Time to deserialize and reserialize " 106 + classes.size() + " classes = " + t + " ms"); 107 } 108 109 for (int i = 0; i < 10; ++i) { 110 long t = System.currentTimeMillis(); 111 for (int j = 0; j < classes.size(); ++j) { 112 byte[] b = (byte[]) classes.get(j); 113 new ClassReader(b).accept(new ClassNode(), false); 114 } 115 t = System.currentTimeMillis() - t; 116 System.out.println("Time to deserialize " + classes.size() 117 + " classes with tree package = " + t + " ms"); 118 } 119 120 for (int i = 0; i < 10; ++i) { 121 long t = System.currentTimeMillis(); 122 for (int j = 0; j < classes.size(); ++j) { 123 byte[] b = (byte[]) classes.get(j); 124 ClassWriter cw = new ClassWriter(false); 125 ClassNode cn = new ClassNode(); 126 new ClassReader(b).accept(cn, false); 127 cn.accept(cw); 128 cw.toByteArray(); 129 } 130 t = System.currentTimeMillis() - t; 131 System.out.println("Time to deserialize and reserialize " 132 + classes.size() + " classes with tree package = " + t 133 + " ms"); 134 } 135 136 classes = null; 137 138 System.out.println("\nComparing ASM, BCEL, SERP and Javassist performances..."); 139 System.out.println("This may take 20 to 30 minutes\n"); 140 System.out.println("ASM PERFORMANCES\n"); 142 new ASMPerfTest().perfs(args); 143 double[][] asmPerfs = perfs; 144 System.out.println("\nBCEL PERFORMANCES\n"); 145 new BCELPerfTest().perfs(args); 146 double[][] bcelPerfs = perfs; 147 System.out.println("\nSERP PERFORMANCES\n"); 148 new SERPPerfTest().perfs(args); 149 double[][] serpPerfs = perfs; 150 System.out.println("\nJavassist PERFORMANCES\n"); 151 new JavassistPerfTest().perfs(args); 152 double[][] javassistPerfs = perfs; 153 154 System.out.println("\nGLOBAL RESULTS"); 156 System.out.println("\nWITH DEBUG INFORMATION\n"); 157 for (int step = 0; step < 2; ++step) { 158 for (mode = 0; mode < 4; ++mode) { 159 switch (mode) { 160 case 0: 161 System.out.print("NO ADAPT: "); 162 break; 163 case 1: 164 System.out.print("NULL ADAPT: "); 165 break; 166 case 2: 167 System.out.print("COMPUTE MAXS: "); 168 break; 169 default: 170 System.out.print("ADD COUNTER: "); 171 break; 172 } 173 System.out.print((float) asmPerfs[step][mode] + " ms"); 174 if (mode > 0) { 175 System.out.print(" (*"); 176 System.out.print((float) (asmPerfs[step][mode] / asmPerfs[step][0])); 177 System.out.print(")"); 178 } 179 System.out.print(" "); 180 System.out.print((float) bcelPerfs[step][mode] + " ms"); 181 if (mode > 0) { 182 System.out.print(" (*"); 183 System.out.print((float) (bcelPerfs[step][mode] / bcelPerfs[step][0])); 184 System.out.print(")"); 185 } 186 System.out.print(" "); 187 System.out.print((float) serpPerfs[step][mode] + " ms"); 188 if (mode > 0) { 189 System.out.print(" (*"); 190 System.out.print((float) (serpPerfs[step][mode] / serpPerfs[step][0])); 191 System.out.print(")"); 192 } 193 System.out.print(" "); 194 System.out.print((float) javassistPerfs[step][mode] + " ms"); 195 if (mode > 0) { 196 System.out.print(" (*"); 197 System.out.print((float) (javassistPerfs[step][mode] / javassistPerfs[step][0])); 198 System.out.print(")"); 199 } 200 System.out.println(); 201 } 202 if (step == 0) { 203 System.out.println("\nWITHOUT DEBUG INFORMATION\n"); 204 } 205 } 206 207 System.out.println("\nRELATIVE RESULTS"); 208 System.out.println("\nWITH DEBUG INFORMATION\n"); 209 for (int step = 0; step < 2; ++step) { 210 System.err.println("[MEASURE ASM BCEL SERP Javassist]"); 211 for (mode = 1; mode < 4; ++mode) { 212 int base; 213 switch (mode) { 214 case 1: 215 System.out.print("NULL ADAPT: "); 216 base = 0; 217 break; 218 case 2: 219 System.out.print("COMPUTE MAXS: "); 220 base = 1; 221 break; 222 default: 223 System.out.print("ADD COUNTER: "); 224 base = 1; 225 break; 226 } 227 double ref = asmPerfs[step][mode] - asmPerfs[step][base]; 228 System.out.print((float) ref + " ms "); 229 double f = bcelPerfs[step][mode] - bcelPerfs[step][base]; 230 System.out.print((float) f + " ms (*"); 231 System.out.print((float) (f / ref)); 232 System.out.print(") "); 233 double g = serpPerfs[step][mode] - serpPerfs[step][base]; 234 System.out.print((float) g + " ms (*"); 235 System.out.print((float) (g / ref)); 236 System.out.print(")"); 237 double h = javassistPerfs[step][mode] 238 - javassistPerfs[step][base]; 239 System.out.print((float) h + " ms (*"); 240 System.out.print((float) (h / ref)); 241 System.out.print(")"); 242 System.out.println(); 243 } 244 if (step == 0) { 245 System.out.println("\nWITHOUT DEBUG INFORMATION\n"); 246 } 247 } 248 } 249 250 void perfs(final String [] args) throws Exception { 251 if (!(new File (args[0] + "classes1.zip").exists())) { 253 System.out.println("Preparing zip files from " + args[1] + "..."); 254 for (int step = 0; step < 2; ++step) { 255 dst = new ZipOutputStream (new FileOutputStream (args[0] 256 + "classes" + (step + 1) + ".zip")); 257 mode = step == 0 ? 1 : 4; 258 for (int i = 1; i < args.length; ++i) { 259 ALLPerfTest loader = newInstance(); 260 zip = new ZipFile (args[i]); 261 Enumeration entries = zip.entries(); 262 while (entries.hasMoreElements()) { 263 String s = ((ZipEntry ) entries.nextElement()).getName(); 264 if (s.endsWith(".class")) { 265 s = s.substring(0, s.length() - 6) 266 .replace('/', '.'); 267 loader.loadClass(s); 268 } 269 } 270 } 271 dst.close(); 272 dst = null; 273 } 274 System.out.println(); 275 } 276 277 perfs = new double[2][4]; 279 System.out.println("FIRST STEP: WITH DEBUG INFORMATION"); 280 for (int step = 0; step < 2; ++step) { 281 zip = new ZipFile (args[0] + "classes" + (step + 1) + ".zip"); 282 for (mode = 0; mode < 4; ++mode) { 283 for (int i = 0; i < 4; ++i) { 284 ALLPerfTest loader = newInstance(); 285 total = 0; 286 totalSize = 0; 287 Enumeration entries = zip.entries(); 288 double t = System.currentTimeMillis(); 289 while (entries.hasMoreElements()) { 290 String s = ((ZipEntry ) entries.nextElement()).getName(); 291 if (s.endsWith(".class")) { 292 s = s.substring(0, s.length() - 6) 293 .replace('/', '.'); 294 loader.loadClass(s); 295 } 296 } 297 t = System.currentTimeMillis() - t; 298 if (i == 0) { 299 perfs[step][mode] = t; 300 } else { 301 perfs[step][mode] = Math.min(perfs[step][mode], t); 302 } 303 switch (mode) { 304 case 0: 305 System.out.print("NO ADAPT: "); 306 break; 307 case 1: 308 System.out.print("NULL ADAPT: "); 309 break; 310 case 2: 311 System.out.print("COMPUTE MAXS: "); 312 break; 313 default: 314 System.out.print("ADD COUNTER: "); 315 break; 316 } 317 System.out.print((float) t + " ms "); 318 System.out.print("(" + total + " classes"); 319 System.out.println(", " + totalSize + " bytes)"); 320 loader = null; 321 gc(); 322 } 323 } 324 if (step == 0) { 325 System.out.println("SECOND STEP: WITHOUT DEBUG INFORMATION"); 326 } 327 } 328 329 System.out.println("\nRESULTS"); 331 System.out.println("\nWITH DEBUG INFORMATION\n"); 332 for (int step = 0; step < 2; ++step) { 333 for (mode = 0; mode < 4; ++mode) { 334 switch (mode) { 335 case 0: 336 System.out.print("NO ADAPT: "); 337 break; 338 case 1: 339 System.out.print("NULL ADAPT: "); 340 break; 341 case 2: 342 System.out.print("COMPUTE MAXS: "); 343 break; 344 default: 345 System.out.print("ADD COUNTER: "); 346 break; 347 } 348 System.out.println((float) perfs[step][mode] + " ms"); 349 } 350 if (step == 0) { 351 System.out.println("\nWITHOUT DEBUG INFORMATION\n"); 352 } 353 } 354 } 355 356 private static byte[] readClass(final InputStream is) throws IOException { 357 if (is == null) { 358 throw new IOException ("Class not found"); 359 } 360 byte[] b = new byte[is.available()]; 361 int len = 0; 362 while (true) { 363 int n = is.read(b, len, b.length - len); 364 if (n == -1) { 365 if (len < b.length) { 366 byte[] c = new byte[len]; 367 System.arraycopy(b, 0, c, 0, len); 368 b = c; 369 } 370 return b; 371 } else { 372 len += n; 373 if (len == b.length) { 374 byte[] c = new byte[b.length + 1000]; 375 System.arraycopy(b, 0, c, 0, len); 376 b = c; 377 } 378 } 379 } 380 } 381 382 protected Class findClass(final String name) throws ClassNotFoundException { 383 try { 384 byte[] b; 385 String fileName = name.replace('.', '/') + ".class"; 386 InputStream is = zip.getInputStream(zip.getEntry(fileName)); 387 switch (mode) { 388 case 0: 389 b = new byte[is.available()]; 390 int len = 0; 391 while (true) { 392 int n = is.read(b, len, b.length - len); 393 if (n == -1) { 394 if (len < b.length) { 395 byte[] c = new byte[len]; 396 System.arraycopy(b, 0, c, 0, len); 397 b = c; 398 } 399 break; 400 } else { 401 len += n; 402 if (len == b.length) { 403 byte[] c = new byte[b.length + 1000]; 404 System.arraycopy(b, 0, c, 0, len); 405 b = c; 406 } 407 } 408 } 409 break; 410 case 1: 411 compute = false; 412 skipDebug = false; 413 b = nullAdaptClass(is, name); 414 break; 415 case 2: 416 compute = true; 417 skipDebug = false; 418 b = nullAdaptClass(is, name); 419 break; 420 case 3: 421 b = counterAdaptClass(is, name); 422 break; 423 default: 425 compute = false; 426 skipDebug = true; 427 b = nullAdaptClass(is, name); 428 break; 429 } 430 if (dst != null) { 431 dst.putNextEntry(new ZipEntry (fileName)); 432 dst.write(b, 0, b.length); 433 dst.closeEntry(); 434 } 435 total += 1; 436 totalSize += b.length; 437 return defineClass(name, b, 0, b.length); 438 } catch (Exception e) { 439 e.printStackTrace(); 440 throw new ClassNotFoundException (name); 441 } 442 } 443 444 private static void gc() { 445 try { 446 Runtime.getRuntime().gc(); 447 Thread.sleep(50); 448 Runtime.getRuntime().gc(); 449 Thread.sleep(50); 450 Runtime.getRuntime().gc(); 451 Thread.sleep(50); 452 } catch (InterruptedException e) { 453 } 454 } 455 456 abstract ALLPerfTest newInstance(); 457 458 abstract byte[] nullAdaptClass(final InputStream is, final String name) 459 throws Exception ; 460 461 abstract byte[] counterAdaptClass(final InputStream is, final String name) 462 throws Exception ; 463 } 464 | Popular Tags |