KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > asm > xml > XMLPerfTest


1 /***
2  * ASM XML Adapter
3  * Copyright (c) 2004, Eugene Kuleshov
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.xml;
31
32 import java.io.BufferedInputStream JavaDoc;
33 import java.io.File JavaDoc;
34 import java.io.FileInputStream JavaDoc;
35 import java.io.IOException JavaDoc;
36 import java.io.InputStream JavaDoc;
37 import java.io.OutputStream JavaDoc;
38 import java.net.URL JavaDoc;
39
40 import javax.xml.transform.Source JavaDoc;
41 import javax.xml.transform.stream.StreamSource JavaDoc;
42
43 /**
44  * Performance test suite for ASM XML
45  *
46  * @author Eugene Kuleshov
47  */

48 public class XMLPerfTest {
49
50     private static final String JavaDoc[] ENGINES = {
51         "jd.xml.xslt.trax.TransformerFactoryImpl",
52         "net.sf.saxon.TransformerFactoryImpl",
53         "org.apache.xalan.processor.TransformerFactoryImpl", };
54
55     private static final String JavaDoc[] TEMPLATES = {
56         "copy.xsl",
57         "linenumbers.xsl",
58         "profile.xsl", };
59
60     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
61         System.err.println("Comparing XSLT performance for ASM XSLT");
62         System.err.println("This may take 20 to 30 minutes\n");
63
64         File JavaDoc examplesDir = new File JavaDoc(args[0]);
65         if (!examplesDir.isDirectory()) {
66             System.err.println(args[0] + " must be directory");
67             return;
68         }
69
70         // File[] templates = examplesDir.listFiles(new FilenameFilter() {
71
// public boolean accept(File dir, String name) {
72
// return name.endsWith(".xsl");
73
// }
74
// });
75

76         for (int i = 0; i < ENGINES.length; i++) {
77             System.err.println(ENGINES[i]);
78             process(null, ENGINES[i]);
79             for (int j = 0; j < TEMPLATES.length; j++) {
80                 process(new File JavaDoc(examplesDir, TEMPLATES[j]).getAbsolutePath(),
81                         ENGINES[i]);
82             }
83             System.err.println();
84         }
85
86     }
87
88     private static void process(String JavaDoc name, String JavaDoc engine) throws Exception JavaDoc {
89         System.setProperty("javax.xml.transform.TransformerFactory", engine);
90         processRep(name, Processor.BYTECODE);
91         processRep(name, Processor.MULTI_XML);
92         processRep(name, Processor.SINGLE_XML);
93     }
94
95     // private static void processEntry(
96
// String className,
97
// TransformerHandler handler) throws Exception
98
// {
99
// byte[] classData = getCode(new URL(className).openStream());
100
// ByteArrayOutputStream bos = new ByteArrayOutputStream();
101
//
102
// handler.setResult(new SAXResult(new ASMContentHandler(bos, false)));
103
//
104
// ClassReader cr = new ClassReader(classData);
105
// cr.accept(new SAXClassAdapter(handler, cr.getVersion(), false), false);
106
// }
107
//
108
// private static byte[] getCode(InputStream is) throws IOException {
109
// ByteArrayOutputStream bos = new ByteArrayOutputStream();
110
// byte[] buff = new byte[1024];
111
// int n = -1;
112
// while ((n = is.read(buff)) > -1)
113
// bos.write(buff, 0, n);
114
// return bos.toByteArray();
115
// }
116

117     private static void processRep(String JavaDoc name, int outRep) {
118         long l1 = System.currentTimeMillis();
119         int n = 0;
120         try {
121             Class JavaDoc c = XMLPerfTest.class;
122             String JavaDoc u = c.getResource("/java/lang/String.class").toString();
123             final InputStream JavaDoc is = new BufferedInputStream JavaDoc(new URL JavaDoc(u.substring(4,
124                     u.indexOf('!'))).openStream());
125             final OutputStream JavaDoc os = new IgnoringOutputStream();
126             final StreamSource JavaDoc xslt = name == null
127                     ? null
128                     : new StreamSource JavaDoc(new FileInputStream JavaDoc(name));
129
130             Processor p = new DotObserver(Processor.BYTECODE,
131                     outRep,
132                     is,
133                     os,
134                     xslt);
135             n = p.process();
136
137         } catch (Exception JavaDoc ex) {
138             System.err.println();
139             // ex.printStackTrace();
140
System.err.println(ex);
141
142         }
143
144         long l2 = System.currentTimeMillis();
145
146         System.err.println();
147         System.err.println(" " + outRep + " " + name + " " + (l2 - l1)
148                 + "ms " + (1000f * n / (l2 - l1)));
149
150         // SAXTransformerFactory saxtf = (SAXTransformerFactory)
151
// TransformerFactory.newInstance();
152
// Templates templates = saxtf.newTemplates(xslt);
153
//
154
// ZipEntry ze = null;
155
// int max = 10000;
156
// while ((ze = zis.getNextEntry()) != null && max > 0) {
157
// if (ze.getName().endsWith(".class")) {
158
// processEntry(u.substring(0, n + 2).concat(ze.getName()),
159
// saxtf.newTransformerHandler(templates));
160
// max--;
161
// }
162
// }
163
}
164
165     private static final class DotObserver extends Processor {
166         private int n = 0;
167
168         public DotObserver(
169             int inRepresenation,
170             int outRepresentation,
171             InputStream JavaDoc input,
172             OutputStream JavaDoc output,
173             Source JavaDoc xslt)
174         {
175             super(inRepresenation, outRepresentation, input, output, xslt);
176         }
177
178         public void update(Object JavaDoc arg) {
179             n++;
180             if ((n % 1000) == 0) {
181                 System.err.print("" + (n / 1000));
182             } else if ((n % 100) == 0) {
183                 System.err.print(".");
184             }
185         }
186     }
187
188     private static final class IgnoringOutputStream extends OutputStream JavaDoc {
189
190         public final void write(int b) throws IOException JavaDoc {
191         }
192
193         public final void write(byte[] b) throws IOException JavaDoc {
194         }
195
196         public final void write(byte[] b, int off, int len) throws IOException JavaDoc {
197         }
198     }
199 }
200
Popular Tags