001: /***
002: * ASM XML Adapter
003: * Copyright (c) 2004, Eugene Kuleshov
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: * 1. Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: * 2. Redistributions in binary form must reproduce the above copyright
012: * notice, this list of conditions and the following disclaimer in the
013: * documentation and/or other materials provided with the distribution.
014: * 3. Neither the name of the copyright holders nor the names of its
015: * contributors may be used to endorse or promote products derived from
016: * this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
028: * THE POSSIBILITY OF SUCH DAMAGE.
029: */package org.objectweb.asm.xml;
030:
031: import java.io.BufferedInputStream;
032: import java.io.File;
033: import java.io.FileInputStream;
034: import java.io.IOException;
035: import java.io.InputStream;
036: import java.io.OutputStream;
037: import java.net.URL;
038:
039: import javax.xml.transform.Source;
040: import javax.xml.transform.stream.StreamSource;
041:
042: /**
043: * Performance test suite for ASM XML
044: *
045: * @author Eugene Kuleshov
046: */
047: public class XMLPerfTest {
048:
049: private static final String[] ENGINES = {
050: "jd.xml.xslt.trax.TransformerFactoryImpl",
051: "net.sf.saxon.TransformerFactoryImpl",
052: "org.apache.xalan.processor.TransformerFactoryImpl", };
053:
054: private static final String[] TEMPLATES = { "copy.xsl",
055: "linenumbers.xsl", "profile.xsl", };
056:
057: public static void main(final String[] args) throws Exception {
058: System.err.println("Comparing XSLT performance for ASM XSLT");
059: System.err.println("This may take 20 to 30 minutes\n");
060:
061: File examplesDir = new File(args[0]);
062: if (!examplesDir.isDirectory()) {
063: System.err.println(args[0] + " must be directory");
064: return;
065: }
066:
067: // File[] templates = examplesDir.listFiles(new FilenameFilter() {
068: // public boolean accept(File dir, String name) {
069: // return name.endsWith(".xsl");
070: // }
071: // });
072:
073: for (int i = 0; i < ENGINES.length; i++) {
074: System.err.println(ENGINES[i]);
075: process(null, ENGINES[i]);
076: for (int j = 0; j < TEMPLATES.length; j++) {
077: process(new File(examplesDir, TEMPLATES[j])
078: .getAbsolutePath(), ENGINES[i]);
079: }
080: System.err.println();
081: }
082:
083: }
084:
085: private static void process(final String name, final String engine)
086: throws Exception {
087: System.setProperty("javax.xml.transform.TransformerFactory",
088: engine);
089: processRep(name, Processor.BYTECODE);
090: processRep(name, Processor.MULTI_XML);
091: processRep(name, Processor.SINGLE_XML);
092: }
093:
094: // private static void processEntry(
095: // String className,
096: // TransformerHandler handler) throws Exception
097: // {
098: // byte[] classData = getCode(new URL(className).openStream());
099: // ByteArrayOutputStream bos = new ByteArrayOutputStream();
100: //
101: // handler.setResult(new SAXResult(new ASMContentHandler(bos, false)));
102: //
103: // ClassReader cr = new ClassReader(classData);
104: // cr.accept(new SAXClassAdapter(handler, cr.getVersion(), false), false);
105: // }
106: //
107: // private static byte[] getCode(InputStream is) throws IOException {
108: // ByteArrayOutputStream bos = new ByteArrayOutputStream();
109: // byte[] buff = new byte[1024];
110: // int n = -1;
111: // while ((n = is.read(buff)) > -1)
112: // bos.write(buff, 0, n);
113: // return bos.toByteArray();
114: // }
115:
116: private static void processRep(final String name, final int outRep) {
117: long l1 = System.currentTimeMillis();
118: int n = 0;
119: try {
120: Class c = XMLPerfTest.class;
121: String u = c.getResource("/java/lang/String.class")
122: .toString();
123: final InputStream is = new BufferedInputStream(new URL(u
124: .substring(4, u.indexOf('!'))).openStream());
125: final OutputStream os = new IgnoringOutputStream();
126: final StreamSource xslt = name == null ? null
127: : new StreamSource(new FileInputStream(name));
128:
129: Processor p = new DotObserver(Processor.BYTECODE, outRep,
130: is, os, xslt);
131: n = p.process();
132:
133: } catch (Exception ex) {
134: System.err.println();
135: // ex.printStackTrace();
136: System.err.println(ex);
137:
138: }
139:
140: long l2 = System.currentTimeMillis();
141:
142: System.err.println();
143: System.err.println(" " + outRep + " " + name + " "
144: + (l2 - l1) + "ms " + 1000f * n / (l2 - l1));
145:
146: // SAXTransformerFactory saxtf = (SAXTransformerFactory)
147: // TransformerFactory.newInstance();
148: // Templates templates = saxtf.newTemplates(xslt);
149: //
150: // ZipEntry ze = null;
151: // int max = 10000;
152: // while ((ze = zis.getNextEntry()) != null && max > 0) {
153: // if (ze.getName().endsWith(".class")) {
154: // processEntry(u.substring(0, n + 2).concat(ze.getName()),
155: // saxtf.newTransformerHandler(templates));
156: // max--;
157: // }
158: // }
159: }
160:
161: private static final class DotObserver extends Processor {
162: private int n = 0;
163:
164: public DotObserver(final int inRepresenation,
165: final int outRepresentation, final InputStream input,
166: final OutputStream output, final Source xslt) {
167: super (inRepresenation, outRepresentation, input, output,
168: xslt);
169: }
170:
171: public void update(final Object arg) {
172: n++;
173: if (n % 1000 == 0) {
174: System.err.print("" + n / 1000);
175: } else if (n % 100 == 0) {
176: System.err.print(".");
177: }
178: }
179: }
180:
181: private static final class IgnoringOutputStream extends
182: OutputStream {
183:
184: public final void write(final int b) throws IOException {
185: }
186:
187: public final void write(final byte[] b) throws IOException {
188: }
189:
190: public final void write(final byte[] b, final int off,
191: final int len) throws IOException {
192: }
193: }
194: }
|