1 16 package org.apache.cocoon.xml; 17 18 import java.io.ByteArrayInputStream ; 19 20 import javax.xml.parsers.SAXParser ; 21 import javax.xml.parsers.SAXParserFactory ; 22 23 import org.apache.cocoon.xml.dom.DOMBuilder; 24 import org.xml.sax.ContentHandler ; 25 import org.xml.sax.helpers.DefaultHandler ; 26 27 33 34 public final class SaxBufferTestCase extends AbstractXMLTestCase { 35 public SaxBufferTestCase(String s) { 36 super(s); 37 } 38 39 public void testCompareDOM() throws Exception { 40 DOMBuilder in = new DOMBuilder(); 41 generateLargeSAX(in); 42 43 SaxBuffer sb = new SaxBuffer(); 44 generateLargeSAX(sb); 45 46 DOMBuilder out = new DOMBuilder(); 47 sb.toSAX(out); 48 49 assertXMLEqual(in.getDocument(), out.getDocument()); 50 } 51 52 public void testStressLoop() throws Exception { 53 SaxBuffer sb = new SaxBuffer(); 54 55 long loop = 10000; 56 57 long start = System.currentTimeMillis(); 59 for(int i=0;i<loop;i++) { 60 generateSmallSAX(sb); 61 sb.recycle(); 62 } 63 long stop = System.currentTimeMillis() + 1; 64 65 double r = 1000*loop/(stop-start); 66 System.out.println("consuming: "+ r + " documents per second"); 67 } 68 69 public void testCompareToParsing() throws Exception { 70 DOMBuilder in = new DOMBuilder(); 71 generateSmallSAX(in); 72 73 SAXParserFactory pfactory = SAXParserFactory.newInstance(); 74 SAXParser p = pfactory.newSAXParser(); 75 76 77 SaxBuffer b = new SaxBuffer(); 78 DefaultHandlerWrapper wrapper = new DefaultHandlerWrapper(b); 79 ByteArrayInputStream bis = new ByteArrayInputStream (generateByteArray()); 80 81 long loop = 10000; 82 83 long start = System.currentTimeMillis(); 84 for(int i=0;i<loop;i++) { 85 b.recycle(); 86 bis.reset(); 87 p.parse(bis,wrapper); 88 } 89 long stop = System.currentTimeMillis() + 1; 90 91 double r = 1000*loop/(stop-start); 92 System.out.println("parsed:" + r + " documents per second"); 93 94 95 ContentHandler ch = new DefaultHandler (); 96 97 start = System.currentTimeMillis(); 98 for(int i=0;i<loop;i++) { 99 b.toSAX(ch); 100 } 101 stop = System.currentTimeMillis() + 1; 102 103 r = 1000*loop/(stop-start); 104 System.out.println("recalling: " + r + " documents per second"); 105 } 106 107 108 } 109 | Popular Tags |