1
2
3
4
5
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
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
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
74 for (int j = 0; j < THREADCOUNT; j++) {
75 threads[j].start();
76 }
77
78
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
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