1
2
3
4
5
6
7
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
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
75 Thread[] threads = new Thread[THREADCOUNT];
76 for (int i = 0; i < THREADCOUNT; i++)
77 threads[i] = new Thread( new SameNSTest() );
78
79
80 for (int j = 0; j < THREADCOUNT; j++)
81 threads[j].start();
82
83
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158