001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.openide.filesystems.xmlfs;
043:
044: import java.io.*;
045: import java.net.URL;
046:
047: import org.xml.sax.*;
048:
049: import org.openide.filesystems.XMLFileSystem;
050:
051: /** A test that should measure and compare the speed of parsing an
052: * XML document to creation of new XMLFileSystem
053: *
054: * @author Jaroslav Tulach
055: */
056: public class XMLFSvsParserTest extends
057: org.netbeans.performance.Benchmark implements EntityResolver {
058: private static final String PUBLIC_ID = "-//NetBeans//DTD Filesystem 1.0//EN";// NOI18N
059: private static final String DTD_PATH = "/org/openide/filesystems/filesystem.dtd";// NOI18N
060: /** URL of a document to parse */
061: private URL url;
062:
063: /** Constructor
064: */
065: public XMLFSvsParserTest(String name) {
066: super (name, new Object[] { new Integer(100), new Integer(1000),
067: new Integer(10000) });
068: }
069:
070: /** A main method.
071: */
072: public static void main(java.lang.String[] args) {
073: junit.textui.TestRunner.run(new junit.framework.TestSuite(
074: XMLFSvsParserTest.class));
075: }
076:
077: /** Setups the test.
078: */
079: protected void setUp() throws Exception {
080: File file = File.createTempFile("afile", null);
081: file.deleteOnExit();
082:
083: Integer count = (Integer) getArgument();
084:
085: prepareXmlFilesystem(file, count.intValue(),
086: "anyfilewithanyvalues");
087:
088: url = file.toURL();
089: }
090:
091: /** Tests how quickly a parser can parse the file.
092: */
093: public void testXMLParser() throws Exception {
094: int cnt = getIterationCount();
095:
096: while (cnt-- > 0) {
097: org.xml.sax.HandlerBase base = new org.xml.sax.HandlerBase();
098: javax.xml.parsers.SAXParserFactory factory = javax.xml.parsers.SAXParserFactory
099: .newInstance();
100: factory.setValidating(false);
101: org.xml.sax.Parser parser = factory.newSAXParser()
102: .getParser();
103: parser.setEntityResolver(this );
104: parser.setDocumentHandler(base);
105:
106: parser.parse(new InputSource(url.openStream()));
107: }
108: }
109:
110: /** Tests how quickly an XML fs can be created.
111: */
112: public void testXMLFS() throws Exception {
113: int cnt = getIterationCount();
114: while (cnt-- > 0) {
115: XMLFileSystem fs = new XMLFileSystem(url);
116: }
117: }
118:
119: /** Implements entity resolver.
120: */
121: public InputSource resolveEntity(java.lang.String pid,
122: java.lang.String sid) throws SAXException {
123: if (pid != null && pid.equals(PUBLIC_ID))
124: return new InputSource(getClass().getResourceAsStream(
125: DTD_PATH));
126:
127: return new InputSource(sid);
128: }
129:
130: /** Writes the content of a file
131: */
132: private static void prepareXmlFilesystem(File jar, int count,
133: String prefix) throws IOException {
134: FileOutputStream stream = new FileOutputStream(jar);
135: OutputStreamWriter writer = new OutputStreamWriter(stream,
136: "UTF8");
137: PrintWriter print = new PrintWriter(writer);
138: print
139: .println("<?xml version=\"1.0\"?>"
140: + "<!DOCTYPE filesystem PUBLIC "
141: + "\"-//NetBeans//DTD Filesystem 1.0//EN\" "
142: + "\"http://www.netbeans.org/dtds/filesystem-1_0.dtd\">\n"
143: + "<filesystem>");
144:
145: for (int i = 0; i < count; i++) {
146: print.println("<file name=\"" + prefix + i + "\"/>");
147: }
148: print.println("</filesystem>");
149: print.flush();
150: print.close();
151: }
152:
153: }
|