KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > xdm > perf > XDMPerfNumbers


1 /*
2  * XDMPerfNumbers.java
3  *
4  * Created on November 10, 2005, 11:17 AM
5  *
6  * To change this template, choose Tools | Template Manager
7  * and open the template in the editor.
8  */

9
10 package org.netbeans.modules.xml.xdm.perf;
11 import junit.framework.Test;
12 import junit.framework.TestCase;
13 import junit.framework.TestSuite;
14
15 import java.io.BufferedReader JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.io.InputStream JavaDoc;
18 import java.io.InputStreamReader JavaDoc;
19 import javax.xml.parsers.DocumentBuilder JavaDoc;
20 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
21 import org.netbeans.editor.BaseDocument;
22 import org.netbeans.editor.TokenItem;
23 import org.netbeans.editor.ext.ExtSyntaxSupport;
24 import org.netbeans.modules.xml.text.syntax.XMLKit;
25 import org.netbeans.modules.xml.xam.ModelSource;
26 import org.netbeans.modules.xml.xdm.XDMModel;
27 import org.openide.util.Lookup;
28 import org.openide.util.lookup.Lookups;
29
30 /**
31  * This test class is meant to be used for obtaining performance numbers on XDM.
32  * NOTE: The numbers may vary on each run and on each machine. Also please note
33  * the following before running these tests.
34  *
35  * 1. Do not run testReadUsingSyntaxParser and testReadUsingXDM together.
36  * 2. Avoid insertString for each line. This is BAD. See usage for 'readLine'
37  * and 'insertEachLine'.
38  *
39  * @author Samaresh
40  */

41 public class XDMPerfNumbers extends TestCase {
42     
43     /**
44      * Performance numbers are run on this giant schema.
45      */

46     static final String JavaDoc SCHEMA_FILE = "J1_TravelItinerary.xsd";
47         
48     /**
49      * Line separator.
50      */

51     static String JavaDoc lineSeparator = System.getProperty("line.separator");
52     
53     /**
54      * boolean flag, to indicate whether to call insertString() for each line
55      * or not. Applicable only when 'readLine' is true. When true, the performance
56      * is really BAD.
57      */

58     boolean insertEachLine = false;
59     
60     /**
61      * boolean flag, to indicate whether to read line-by-line or
62      * character-by-character. This flag does NOT make any major
63      * difference.
64      */

65     boolean readLine = false;
66     
67     public XDMPerfNumbers(String JavaDoc testName) {
68         super(testName);
69     }
70
71     protected void setUp() throws Exception JavaDoc {
72     }
73
74     protected void tearDown() throws Exception JavaDoc {
75     }
76     
77     public static Test suite() {
78         TestSuite suite = new TestSuite(XDMPerfNumbers.class);
79         
80         return suite;
81     }
82             
83     public void testReadUsingDOM() {
84         System.out.println("testReadUsingDOM");
85         long start = System.currentTimeMillis();
86         DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
87         try {
88             DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
89             InputStream JavaDoc is = getClass().getResourceAsStream(SCHEMA_FILE);
90             org.w3c.dom.Document JavaDoc document = builder.parse(is);
91             this.assertNotNull("DOM model didn't get created!!!", document);
92         } catch (Exception JavaDoc ex) {
93             ex.printStackTrace();
94         }
95         long end = System.currentTimeMillis();
96         System.out.println("Time taken to parse using DOM: " + (end-start) + "ms.\n");
97     }
98         
99     public void testReadUsingSyntaxParser() {
100         System.out.println("testReadUsingSyntaxParser");
101         try {
102             java.net.URL JavaDoc url = getClass().getResource(SCHEMA_FILE);
103             // prepare document
104
BaseDocument basedoc = new BaseDocument(XMLKit.class, false);
105             insertStringInDocument(new InputStreamReader JavaDoc(url.openStream(),"UTF-8"), basedoc);
106
107             long start = System.currentTimeMillis();
108             //org.w3c.dom.Document doc = new XMLSyntaxParser(basedoc).parse();
109
ExtSyntaxSupport sup = (ExtSyntaxSupport)basedoc.getSyntaxSupport();
110             TokenItem token = sup.getTokenChain(0, basedoc.getLength());
111             long end = System.currentTimeMillis();
112             System.out.println("Time taken to parse using Syntax Parser: " + (end-start) + "ms.\n");
113             this.assertNotNull("Syntax parser model didn't get created!!!", sup);
114         } catch (Exception JavaDoc ex) {
115             ex.printStackTrace();
116         }
117     }
118     
119     public void testReadUsingXDM() {
120         long start = System.currentTimeMillis();
121         try {
122             javax.swing.text.Document JavaDoc sd = new BaseDocument(XMLKit.class, false);
123             XDMModel model = null;
124
125             InputStreamReader JavaDoc reader = new InputStreamReader JavaDoc(getClass().getResourceAsStream(SCHEMA_FILE),"UTF-8");
126             insertStringInDocument(reader, sd);
127             
128             start = System.currentTimeMillis();
129         Lookup lookup = Lookups.singleton(sd);
130         ModelSource ms = new ModelSource(lookup, true);
131             model = new XDMModel(ms);
132             model.sync();
133             this.assertNotNull("XDM model didn't get created!!!", model);
134         } catch (IOException JavaDoc ex) {
135             ex.printStackTrace();
136         }
137         long end = System.currentTimeMillis();
138         System.out.println("Time taken to parse using XDM: " + (end-start) + "ms.\n");
139     }
140     
141     
142     /**
143      * Reads data in a buffer reader and then accumulates them in a
144      * string buffer
145      */

146     private void insertStringInDocument(InputStreamReader JavaDoc inputStreamReader,
147             javax.swing.text.Document JavaDoc document) {
148         
149         BufferedReader JavaDoc reader = null;
150         System.out.println("insertStringInDocument() called...");
151         long start = System.currentTimeMillis();
152         try {
153             reader = new BufferedReader JavaDoc(inputStreamReader);
154             StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc();
155             if(readLine) {
156                 insertLines(reader, document);
157             } else {
158                 insertCharacters(reader, document);
159             }
160         } catch (Exception JavaDoc ex) {
161             ex.printStackTrace();
162         } finally {
163             try {
164                 reader.close();
165             } catch (IOException JavaDoc ex) {
166                 ex.printStackTrace();
167             }
168         }
169         long end = System.currentTimeMillis();
170         System.out.println("Time taken in insertStringInDocument(): " + (end-start) + "ms.");
171     }
172     
173     
174     /**
175      * Inserts text into the swing document line by line.
176      * If 'insertEachLine' is true, inserts them directly, otherwise
177      * keeps them in a buffer and inserts at the end.
178      * The former is BAD. Do NOT insert each line.
179      */

180     private void insertLines(BufferedReader JavaDoc reader,
181             javax.swing.text.Document JavaDoc document) throws Exception JavaDoc {
182         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
183         String JavaDoc line = null;
184         while ((line = reader.readLine()) != null) {
185             //do not ever insert each line into the doc. This is a killer.
186
if(insertEachLine)
187                 document.insertString(document.getLength(), line+lineSeparator, null);
188             else
189                 buffer.append(line+lineSeparator);
190         }
191         if(!insertEachLine) {
192             document.insertString(0, buffer.toString(), null);
193         }
194     }
195     
196     /**
197      * Inserts text into the swing document character by character.
198      */

199     private void insertCharacters(BufferedReader JavaDoc reader,
200             javax.swing.text.Document JavaDoc document) throws Exception JavaDoc {
201         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
202         int c = 0;
203         while((c = reader.read()) != -1) {
204             buffer.append((char)c);
205         }
206         
207         //finally one insertString
208
document.insertString(0, buffer.toString(), null);
209     }
210 }
211
Popular Tags