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