KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dom4j > samples > performance > ParseLoop


1 /*
2  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
3  *
4  * This software is open source.
5  * See the bottom of this file for the licence.
6  *
7  * $Id: ParseLoop.java,v 1.4 2005/01/29 14:53:13 maartenc Exp $
8  */

9
10 package org.dom4j.samples.performance;
11
12 import java.io.BufferedReader JavaDoc;
13 import java.io.FileReader JavaDoc;
14 import java.io.StringReader JavaDoc;
15
16 import org.dom4j.Document;
17 import org.dom4j.io.SAXReader;
18
19 /**
20  * A simple parsing program that loops which makes it easier to profile
21  *
22  * @author <a HREF="mailto:jstrachan@apache.org">James Strachan </a>
23  * @version $Revision: 1.4 $
24  */

25 public class ParseLoop {
26
27     private static int bufferSize = 128 * 1024;
28
29     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
30         if (args.length <= 0) {
31             System.out.println("arguments: <XML file> [<loopCount>]");
32             return;
33         }
34         String JavaDoc xmlFile = args[0];
35         int loops = 40;
36         if (args.length > 1) {
37             loops = Integer.parseInt(args[1]);
38         }
39
40         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(64 * 1024);
41         BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new FileReader JavaDoc(xmlFile));
42         while (true) {
43             String JavaDoc text = reader.readLine();
44             if (text == null) {
45                 break;
46             }
47             buffer.append(text);
48             buffer.append("\n");
49         }
50         long start = System.currentTimeMillis();
51
52         parse(buffer.toString(), loops);
53
54         long elapsed = System.currentTimeMillis() - start;
55
56         System.out.println("Parsed: " + xmlFile + " " + loops + " times in: "
57                 + elapsed + " (ms)");
58     }
59
60     protected static void parse(String JavaDoc text, int loops) throws Exception JavaDoc {
61         SAXReader xmlReader = new SAXReader();
62         for (int i = 0; i < loops; i++) {
63             Document document = xmlReader.read(new StringReader JavaDoc(text));
64         }
65     }
66 }
67
68 /*
69  * Redistribution and use of this software and associated documentation
70  * ("Software"), with or without modification, are permitted provided that the
71  * following conditions are met:
72  *
73  * 1. Redistributions of source code must retain copyright statements and
74  * notices. Redistributions must also contain a copy of this document.
75  *
76  * 2. Redistributions in binary form must reproduce the above copyright notice,
77  * this list of conditions and the following disclaimer in the documentation
78  * and/or other materials provided with the distribution.
79  *
80  * 3. The name "DOM4J" must not be used to endorse or promote products derived
81  * from this Software without prior written permission of MetaStuff, Ltd. For
82  * written permission, please contact dom4j-info@metastuff.com.
83  *
84  * 4. Products derived from this Software may not be called "DOM4J" nor may
85  * "DOM4J" appear in their names without prior written permission of MetaStuff,
86  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
87  *
88  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
89  *
90  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
91  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
92  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
93  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
94  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
95  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
96  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
97  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
98  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
99  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
100  * POSSIBILITY OF SUCH DAMAGE.
101  *
102  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
103  *
104  * $Id: ParseLoop.java,v 1.4 2005/01/29 14:53:13 maartenc Exp $
105  */

106
Popular Tags