001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.xml;
018:
019: import java.io.ByteArrayInputStream;
020:
021: import javax.xml.parsers.SAXParser;
022: import javax.xml.parsers.SAXParserFactory;
023:
024: import org.apache.cocoon.xml.dom.DOMBuilder;
025: import org.xml.sax.ContentHandler;
026: import org.xml.sax.helpers.DefaultHandler;
027:
028: /**
029: * Testcase for SaxBuffer
030: *
031: * @author <a href="mailto:tcurdt@apache.org">Torsten Curdt</a>
032: * @version
033: */
034:
035: public final class SaxBufferTestCase extends AbstractXMLTestCase {
036: public SaxBufferTestCase(String s) {
037: super (s);
038: }
039:
040: public void testCompareDOM() throws Exception {
041: DOMBuilder in = new DOMBuilder();
042: generateLargeSAX(in);
043:
044: SaxBuffer sb = new SaxBuffer();
045: generateLargeSAX(sb);
046:
047: DOMBuilder out = new DOMBuilder();
048: sb.toSAX(out);
049:
050: assertXMLEqual(in.getDocument(), out.getDocument());
051: }
052:
053: public void testStressLoop() throws Exception {
054: SaxBuffer sb = new SaxBuffer();
055:
056: long loop = 10000;
057:
058: // simply consume documents
059: long start = System.currentTimeMillis();
060: for (int i = 0; i < loop; i++) {
061: generateSmallSAX(sb);
062: sb.recycle();
063: }
064: long stop = System.currentTimeMillis() + 1;
065:
066: double r = 1000 * loop / (stop - start);
067: System.out.println("consuming: " + r + " documents per second");
068: }
069:
070: public void testCompareToParsing() throws Exception {
071: DOMBuilder in = new DOMBuilder();
072: generateSmallSAX(in);
073:
074: SAXParserFactory pfactory = SAXParserFactory.newInstance();
075: SAXParser p = pfactory.newSAXParser();
076:
077: SaxBuffer b = new SaxBuffer();
078: DefaultHandlerWrapper wrapper = new DefaultHandlerWrapper(b);
079: ByteArrayInputStream bis = new ByteArrayInputStream(
080: generateByteArray());
081:
082: long loop = 10000;
083:
084: long start = System.currentTimeMillis();
085: for (int i = 0; i < loop; i++) {
086: b.recycle();
087: bis.reset();
088: p.parse(bis, wrapper);
089: }
090: long stop = System.currentTimeMillis() + 1;
091:
092: double r = 1000 * loop / (stop - start);
093: System.out.println("parsed:" + r + " documents per second");
094:
095: ContentHandler ch = new DefaultHandler();
096:
097: start = System.currentTimeMillis();
098: for (int i = 0; i < loop; i++) {
099: 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: }
|