1 16 package org.apache.cocoon.components.sax; 17 18 import org.xml.sax.helpers.DefaultHandler ; 19 import org.xml.sax.ContentHandler ; 20 import org.apache.cocoon.xml.dom.DOMBuilder; 21 import org.apache.cocoon.xml.AbstractXMLTestCase; 22 import org.apache.cocoon.xml.DefaultHandlerWrapper; 23 import javax.xml.parsers.SAXParser ; 24 import javax.xml.parsers.SAXParserFactory ; 25 import java.io.ByteArrayInputStream ; 26 27 33 34 public final class XMLByteStreamCompilerInterpreterTestCase extends AbstractXMLTestCase { 35 public XMLByteStreamCompilerInterpreterTestCase(String s) { 36 super(s); 37 } 38 39 public void testCompareDOM() throws Exception { 40 DOMBuilder in = new DOMBuilder(); 42 generateLargeSAX(in); 43 44 XMLByteStreamCompiler xmlc = new XMLByteStreamCompiler(); 46 generateLargeSAX(xmlc); 47 48 XMLByteStreamInterpreter xmli = new XMLByteStreamInterpreter(); 50 DOMBuilder out = new DOMBuilder(); 51 xmli.setConsumer(out); 52 xmli.deserialize(xmlc.getSAXFragment()); 53 54 assertXMLEqual(in.getDocument(), out.getDocument()); 56 } 57 58 public void testCompareByteArray() throws Exception { 59 XMLByteStreamCompiler sa = new XMLByteStreamCompiler(); 61 generateLargeSAX(sa); 62 63 byte[] aa = (byte[]) sa.getSAXFragment(); 65 66 XMLByteStreamCompiler sb = new XMLByteStreamCompiler(); 68 XMLByteStreamInterpreter xmli = new XMLByteStreamInterpreter(); 69 xmli.setConsumer(sb); 70 xmli.deserialize(aa); 71 72 byte[] ab = (byte[]) sb.getSAXFragment(); 74 75 assertTrue(aa.length == ab.length); 76 77 for (int i=0;i<aa.length;i++) { 78 assertEquals(aa[i],ab[i]); 79 } 80 } 81 82 public void testStressLoop() throws Exception { 83 XMLByteStreamCompiler xmlc = new XMLByteStreamCompiler(); 84 85 long loop = 10000; 86 87 long start = System.currentTimeMillis(); 89 for(int i=0;i<loop;i++) { 90 generateSmallSAX(xmlc); 91 xmlc.recycle(); 92 } 93 long stop = System.currentTimeMillis(); 94 95 double r = 1000*loop/(stop-start); 96 System.out.println("consuming: "+ r + " documents per second"); 97 } 98 99 public void testCompareToParsing() throws Exception { 100 DOMBuilder in = new DOMBuilder(); 101 generateSmallSAX(in); 102 103 SAXParserFactory pfactory = SAXParserFactory.newInstance(); 104 SAXParser p = pfactory.newSAXParser(); 105 106 XMLByteStreamCompiler xmlc = new XMLByteStreamCompiler(); 107 DefaultHandlerWrapper wrapper = new DefaultHandlerWrapper(xmlc); 108 109 ByteArrayInputStream bis = new ByteArrayInputStream (generateByteArray()); 110 111 long loop = 10000; 112 113 long start = System.currentTimeMillis(); 115 for(int i=0;i<loop;i++) { 116 xmlc.recycle(); 117 bis.reset(); 118 p.parse(bis,wrapper); 119 } 120 long stop = System.currentTimeMillis(); 121 122 double r = 1000*loop/(stop-start); 123 System.out.println("parsed: " + r + " documents per second"); 124 125 126 XMLByteStreamInterpreter xmli = new XMLByteStreamInterpreter(); 127 ContentHandler ch = new DefaultHandler (); 128 129 start = System.currentTimeMillis(); 131 for(int i=0;i<loop;i++) { 132 xmli.setContentHandler(ch); 133 xmli.deserialize(xmlc.getSAXFragment()); 134 } 135 stop = System.currentTimeMillis(); 136 137 r = 1000*loop/(stop-start); 138 System.out.println("recalling: " + r + " documents per second"); 139 } 140 } 141 | Popular Tags |