KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > xml > SaxBufferTestCase


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.xml;
17
18 import java.io.ByteArrayInputStream JavaDoc;
19
20 import javax.xml.parsers.SAXParser JavaDoc;
21 import javax.xml.parsers.SAXParserFactory JavaDoc;
22
23 import org.apache.cocoon.xml.dom.DOMBuilder;
24 import org.xml.sax.ContentHandler JavaDoc;
25 import org.xml.sax.helpers.DefaultHandler JavaDoc;
26
27 /**
28  * Testcase for SaxBuffer
29  *
30  * @author <a HREF="mailto:tcurdt@apache.org">Torsten Curdt</a>
31  * @version
32  */

33
34 public final class SaxBufferTestCase extends AbstractXMLTestCase {
35     public SaxBufferTestCase(String JavaDoc s) {
36         super(s);
37     }
38
39     public void testCompareDOM() throws Exception JavaDoc {
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 JavaDoc {
53         SaxBuffer sb = new SaxBuffer();
54
55         long loop = 10000;
56
57         // simply consume documents
58
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 JavaDoc {
70         DOMBuilder in = new DOMBuilder();
71         generateSmallSAX(in);
72
73         SAXParserFactory JavaDoc pfactory = SAXParserFactory.newInstance();
74         SAXParser JavaDoc p = pfactory.newSAXParser();
75
76
77         SaxBuffer b = new SaxBuffer();
78         DefaultHandlerWrapper wrapper = new DefaultHandlerWrapper(b);
79         ByteArrayInputStream JavaDoc bis = new ByteArrayInputStream JavaDoc(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 JavaDoc ch = new DefaultHandler JavaDoc();
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