KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > asm > ALLPerfTest


1 /***
2  * ASM performance test: measures the performances of asm package
3  * Copyright (c) 2002-2005 France Telecom
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the copyright holders nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28  * THE POSSIBILITY OF SUCH DAMAGE.
29  */

30 package org.objectweb.asm;
31
32 import java.io.File JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.io.InputStream JavaDoc;
35 import java.io.FileOutputStream JavaDoc;
36 import java.util.ArrayList JavaDoc;
37 import java.util.Enumeration JavaDoc;
38 import java.util.List JavaDoc;
39 import java.util.zip.ZipEntry JavaDoc;
40 import java.util.zip.ZipFile JavaDoc;
41 import java.util.zip.ZipOutputStream JavaDoc;
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 /**
49  * @author Eric Bruneton
50  */

51 public abstract class ALLPerfTest extends ClassLoader JavaDoc {
52
53     private static ZipFile JavaDoc zip;
54
55     private static ZipOutputStream JavaDoc 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 JavaDoc[] args) throws Exception JavaDoc {
70         ZipFile JavaDoc zip = new ZipFile JavaDoc(System.getProperty("java.home")
71                 + "/lib/rt.jar");
72         List JavaDoc classes = new ArrayList JavaDoc();
73
74         Enumeration JavaDoc entries = zip.entries();
75         while (entries.hasMoreElements()) {
76             ZipEntry JavaDoc e = (ZipEntry JavaDoc) entries.nextElement();
77             String JavaDoc s = e.getName();
78             if (s.endsWith(".class")) {
79                 s = s.substring(0, s.length() - 6).replace('/', '.');
80                 InputStream JavaDoc 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         // measures performances
141
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         // prints results
155
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 JavaDoc[] args) throws Exception JavaDoc {
251         // prepares zip files, if necessary
252
if (!(new File JavaDoc(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 JavaDoc(new FileOutputStream JavaDoc(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 JavaDoc(args[i]);
261                     Enumeration JavaDoc entries = zip.entries();
262                     while (entries.hasMoreElements()) {
263                         String JavaDoc s = ((ZipEntry JavaDoc) 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         // measures performances
278
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 JavaDoc(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 JavaDoc entries = zip.entries();
288                     double t = System.currentTimeMillis();
289                     while (entries.hasMoreElements()) {
290                         String JavaDoc s = ((ZipEntry JavaDoc) 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         // prints results
330
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 JavaDoc is) throws IOException JavaDoc {
357         if (is == null) {
358             throw new IOException JavaDoc("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 JavaDoc findClass(final String JavaDoc name) throws ClassNotFoundException JavaDoc {
383         try {
384             byte[] b;
385             String JavaDoc fileName = name.replace('.', '/') + ".class";
386             InputStream JavaDoc 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                 // case 4:
424
default:
425                     compute = false;
426                     skipDebug = true;
427                     b = nullAdaptClass(is, name);
428                     break;
429             }
430             if (dst != null) {
431                 dst.putNextEntry(new ZipEntry JavaDoc(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 JavaDoc e) {
439             e.printStackTrace();
440             throw new ClassNotFoundException JavaDoc(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 JavaDoc e) {
453         }
454     }
455
456     abstract ALLPerfTest newInstance();
457
458     abstract byte[] nullAdaptClass(final InputStream JavaDoc is, final String JavaDoc name)
459             throws Exception JavaDoc;
460
461     abstract byte[] counterAdaptClass(final InputStream JavaDoc is, final String JavaDoc name)
462             throws Exception JavaDoc;
463 }
464
Popular Tags