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: TestNamespaceCache.java,v 1.2 2004/06/25 08:03:50 maartenc Exp $
8    */
9   
10  package org.dom4j.tree;
11  
12  import junit.framework.Test;
13  import junit.framework.TestSuite;
14  import junit.textui.TestRunner;
15  
16  import org.dom4j.AbstractTestCase;
17  import org.dom4j.Namespace;
18  
19  /*** A test harness to test the performance of the NamespaceCache
20    *
21    * @author <a href="mailto:bfinnell@users.sourceforge.net">Brett Finnell</a>
22    */
23  public class TestNamespaceCache extends AbstractTestCase {
24  
25      private int THREADCOUNT = 50;
26      private int ITERATIONCOUNT = 10000;
27  
28      public static void main( String[] args ) {
29          TestRunner.run( suite() );
30      }
31      
32      public static Test suite() {
33          return new TestSuite( TestNamespaceCache.class );
34      }
35      
36      public TestNamespaceCache(String name) {
37          super(name);
38      }
39  
40      // Test case(s)
41      //-------------------------------------------------------------------------
42      public void testGetSameNamespaceSingleThread() {
43      	long start = System.currentTimeMillis();
44          SameNSTest test = new SameNSTest();
45          test.run();
46  	long end = System.currentTimeMillis();
47  	System.out.println("Same NS Single took " + (end - start) + " ms");
48      }
49  
50      public void testGetSameNamespaceMultiThread() throws Exception {
51      	long start = System.currentTimeMillis();
52          runMultiThreadedTest(new SameNSTest());
53  	long end = System.currentTimeMillis();
54  	System.out.println("Different NS Single took " + (end - start) + " ms");
55      }
56  
57      public void testGetNewNamespaceSingleThread() {
58      	long start = System.currentTimeMillis();
59          DifferentNSTest test = new DifferentNSTest();
60          test.run();
61  	long end = System.currentTimeMillis();
62  	System.out.println("Same NS Multi took " + (end - start) + " ms");
63      }
64  
65      public void testGetNewNamespaceMultiThread() throws Exception {
66      	long start = System.currentTimeMillis();
67          runMultiThreadedTest(new DifferentNSTest());
68  	long end = System.currentTimeMillis();
69  	System.out.println("Different NS Multi took " + (end - start) + " ms");
70      }
71  
72      private void runMultiThreadedTest(Runnable test) throws Exception {
73  
74          //Make the threads
75          Thread[] threads = new Thread[THREADCOUNT];
76          for (int i = 0; i < THREADCOUNT; i++)
77              threads[i] = new Thread( new SameNSTest() );
78  
79          //Start the threads
80          for (int j = 0; j < THREADCOUNT; j++)
81              threads[j].start();
82  
83          //Join with the threads
84          for (int k = 0; k < THREADCOUNT; k++)
85              threads[k].join();
86      }
87  
88      private class SameNSTest implements Runnable {
89  
90          public void run() {
91              NamespaceCache cache = new NamespaceCache();
92              for (int i = 0; i < ITERATIONCOUNT; i++) {
93                  Namespace ns = cache.get("prefix", "uri");
94              }
95          }
96  
97      }
98  
99      private class DifferentNSTest implements Runnable {
100 
101         public void run() {
102             NamespaceCache cache = new NamespaceCache();
103             for (int i = 0; i < ITERATIONCOUNT; i++) {
104                 Namespace ns = cache.get("prefix", Integer.toString(i));
105             }
106         }
107 
108     }
109 
110 }
111 
112 
113 
114 
115 /*
116  * Redistribution and use of this software and associated documentation
117  * ("Software"), with or without modification, are permitted provided
118  * that the following conditions are met:
119  *
120  * 1. Redistributions of source code must retain copyright
121  *    statements and notices.  Redistributions must also contain a
122  *    copy of this document.
123  *
124  * 2. Redistributions in binary form must reproduce the
125  *    above copyright notice, this list of conditions and the
126  *    following disclaimer in the documentation and/or other
127  *    materials provided with the distribution.
128  *
129  * 3. The name "DOM4J" must not be used to endorse or promote
130  *    products derived from this Software without prior written
131  *    permission of MetaStuff, Ltd.  For written permission,
132  *    please contact dom4j-info@metastuff.com.
133  *
134  * 4. Products derived from this Software may not be called "DOM4J"
135  *    nor may "DOM4J" appear in their names without prior written
136  *    permission of MetaStuff, Ltd. DOM4J is a registered
137  *    trademark of MetaStuff, Ltd.
138  *
139  * 5. Due credit should be given to the DOM4J Project - 
140  *    http://www.dom4j.org
141  *
142  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
143  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
144  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
145  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
146  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
147  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
148  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
149  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
150  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
151  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
152  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
153  * OF THE POSSIBILITY OF SUCH DAMAGE.
154  *
155  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
156  *
157  * $Id: TestNamespaceCache.java,v 1.2 2004/06/25 08:03:50 maartenc Exp $
158  */