1 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 ; 16 import java.io.IOException ; 17 import java.io.InputStream ; 18 import java.io.InputStreamReader ; 19 import javax.xml.parsers.DocumentBuilder ; 20 import javax.xml.parsers.DocumentBuilderFactory ; 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 41 public class XDMPerfNumbers extends TestCase { 42 43 46 static final String SCHEMA_FILE = "J1_TravelItinerary.xsd"; 47 48 51 static String lineSeparator = System.getProperty("line.separator"); 52 53 58 boolean insertEachLine = false; 59 60 65 boolean readLine = false; 66 67 public XDMPerfNumbers(String testName) { 68 super(testName); 69 } 70 71 protected void setUp() throws Exception { 72 } 73 74 protected void tearDown() throws Exception { 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 factory = DocumentBuilderFactory.newInstance(); 87 try { 88 DocumentBuilder builder = factory.newDocumentBuilder(); 89 InputStream is = getClass().getResourceAsStream(SCHEMA_FILE); 90 org.w3c.dom.Document document = builder.parse(is); 91 this.assertNotNull("DOM model didn't get created!!!", document); 92 } catch (Exception 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 url = getClass().getResource(SCHEMA_FILE); 103 BaseDocument basedoc = new BaseDocument(XMLKit.class, false); 105 insertStringInDocument(new InputStreamReader (url.openStream(),"UTF-8"), basedoc); 106 107 long start = System.currentTimeMillis(); 108 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 ex) { 115 ex.printStackTrace(); 116 } 117 } 118 119 public void testReadUsingXDM() { 120 long start = System.currentTimeMillis(); 121 try { 122 javax.swing.text.Document sd = new BaseDocument(XMLKit.class, false); 123 XDMModel model = null; 124 125 InputStreamReader reader = new InputStreamReader (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 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 146 private void insertStringInDocument(InputStreamReader inputStreamReader, 147 javax.swing.text.Document document) { 148 149 BufferedReader reader = null; 150 System.out.println("insertStringInDocument() called..."); 151 long start = System.currentTimeMillis(); 152 try { 153 reader = new BufferedReader (inputStreamReader); 154 StringBuffer sbuf = new StringBuffer (); 155 if(readLine) { 156 insertLines(reader, document); 157 } else { 158 insertCharacters(reader, document); 159 } 160 } catch (Exception ex) { 161 ex.printStackTrace(); 162 } finally { 163 try { 164 reader.close(); 165 } catch (IOException 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 180 private void insertLines(BufferedReader reader, 181 javax.swing.text.Document document) throws Exception { 182 StringBuffer buffer = new StringBuffer (); 183 String line = null; 184 while ((line = reader.readLine()) != null) { 185 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 199 private void insertCharacters(BufferedReader reader, 200 javax.swing.text.Document document) throws Exception { 201 StringBuffer buffer = new StringBuffer (); 202 int c = 0; 203 while((c = reader.read()) != -1) { 204 buffer.append((char)c); 205 } 206 207 document.insertString(0, buffer.toString(), null); 209 } 210 } 211 | Popular Tags |