1 |
dl |
1.2 |
/* |
2 |
|
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
|
* Expert Group and released to the public domain, as explained at |
4 |
jsr166 |
1.8 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
dl |
1.2 |
*/ |
6 |
dl |
1.1 |
/** |
7 |
|
|
* @test |
8 |
|
|
* @synopsis Times and checks basic set operations |
9 |
|
|
*/ |
10 |
jsr166 |
1.11 |
import java.io.*; |
11 |
dl |
1.1 |
import java.util.*; |
12 |
|
|
|
13 |
|
|
public class NavigableSetCheck { |
14 |
|
|
|
15 |
|
|
static int absentSize; |
16 |
|
|
static int absentMask; |
17 |
|
|
static Integer[] absent; |
18 |
|
|
|
19 |
|
|
static final Integer MISSING = new Integer(Integer.MIN_VALUE); |
20 |
|
|
|
21 |
|
|
static TestTimer timer = new TestTimer(); |
22 |
|
|
|
23 |
|
|
static void reallyAssert(boolean b) { |
24 |
|
|
if (!b) throw new Error("Failed Assertion"); |
25 |
|
|
} |
26 |
|
|
|
27 |
|
|
public static void main(String[] args) throws Exception { |
28 |
jsr166 |
1.10 |
Class<?> setClass = null; |
29 |
dl |
1.1 |
int numTests = 50; |
30 |
|
|
int size = 50000; |
31 |
|
|
|
32 |
|
|
if (args.length > 0) { |
33 |
|
|
try { |
34 |
|
|
setClass = Class.forName(args[0]); |
35 |
jsr166 |
1.4 |
} catch (ClassNotFoundException e) { |
36 |
dl |
1.1 |
throw new RuntimeException("Class " + args[0] + " not found."); |
37 |
|
|
} |
38 |
|
|
} |
39 |
|
|
|
40 |
jsr166 |
1.3 |
if (args.length > 1) |
41 |
dl |
1.1 |
numTests = Integer.parseInt(args[1]); |
42 |
|
|
|
43 |
jsr166 |
1.3 |
if (args.length > 2) |
44 |
dl |
1.1 |
size = Integer.parseInt(args[2]); |
45 |
|
|
|
46 |
|
|
System.out.println("Testing " + setClass.getName() + " trials: " + numTests + " size: " + size); |
47 |
|
|
|
48 |
|
|
absentSize = 8; |
49 |
|
|
while (absentSize < size) absentSize <<= 1; |
50 |
|
|
absentMask = absentSize - 1; |
51 |
|
|
absent = new Integer[absentSize]; |
52 |
|
|
|
53 |
jsr166 |
1.3 |
for (int i = 0; i < absentSize; ++i) |
54 |
dl |
1.1 |
absent[i] = new Integer(i * 2); |
55 |
|
|
|
56 |
|
|
Integer[] key = new Integer[size]; |
57 |
jsr166 |
1.3 |
for (int i = 0; i < size; ++i) |
58 |
dl |
1.1 |
key[i] = new Integer(i * 2 + 1); |
59 |
|
|
|
60 |
|
|
for (int rep = 0; rep < numTests; ++rep) { |
61 |
|
|
runTest(newSet(setClass), key); |
62 |
|
|
} |
63 |
|
|
|
64 |
|
|
TestTimer.printStats(); |
65 |
|
|
} |
66 |
|
|
|
67 |
jsr166 |
1.10 |
static NavigableSet newSet(Class<?> cl) { |
68 |
dl |
1.1 |
try { |
69 |
jsr166 |
1.14 |
return (NavigableSet) cl.getConstructor().newInstance(); |
70 |
jsr166 |
1.4 |
} catch (Exception e) { |
71 |
dl |
1.1 |
throw new RuntimeException("Can't instantiate " + cl + ": " + e); |
72 |
|
|
} |
73 |
|
|
} |
74 |
|
|
|
75 |
|
|
static void runTest(NavigableSet s, Integer[] key) { |
76 |
|
|
shuffle(key); |
77 |
|
|
int size = key.length; |
78 |
|
|
long startTime = System.currentTimeMillis(); |
79 |
|
|
test(s, key); |
80 |
|
|
long time = System.currentTimeMillis() - startTime; |
81 |
|
|
} |
82 |
|
|
|
83 |
|
|
static void t1(String nm, int n, NavigableSet s, Integer[] key, int expect) { |
84 |
|
|
int sum = 0; |
85 |
|
|
int iters = 4; |
86 |
|
|
timer.start(nm, n * iters); |
87 |
|
|
for (int j = 0; j < iters; ++j) { |
88 |
|
|
for (int i = 0; i < n; i++) { |
89 |
|
|
if (s.contains(key[i])) ++sum; |
90 |
|
|
} |
91 |
|
|
} |
92 |
jsr166 |
1.3 |
timer.finish(); |
93 |
jsr166 |
1.7 |
reallyAssert(sum == expect * iters); |
94 |
dl |
1.1 |
} |
95 |
|
|
|
96 |
|
|
static void t2(String nm, int n, NavigableSet s, Integer[] key, int expect) { |
97 |
|
|
int sum = 0; |
98 |
|
|
timer.start(nm, n); |
99 |
|
|
for (int i = 0; i < n; i++) { |
100 |
|
|
if (s.remove(key[i])) ++sum; |
101 |
|
|
} |
102 |
jsr166 |
1.3 |
timer.finish(); |
103 |
jsr166 |
1.7 |
reallyAssert(sum == expect); |
104 |
dl |
1.1 |
} |
105 |
|
|
|
106 |
|
|
static void t3(String nm, int n, NavigableSet s, Integer[] key, int expect) { |
107 |
|
|
int sum = 0; |
108 |
|
|
timer.start(nm, n); |
109 |
|
|
for (int i = 0; i < n; i++) { |
110 |
|
|
if (s.add(key[i])) ++sum; |
111 |
|
|
} |
112 |
jsr166 |
1.3 |
timer.finish(); |
113 |
jsr166 |
1.7 |
reallyAssert(sum == expect); |
114 |
dl |
1.1 |
} |
115 |
|
|
|
116 |
|
|
static void t4(String nm, int n, NavigableSet s, Integer[] key, int expect) { |
117 |
|
|
int sum = 0; |
118 |
|
|
timer.start(nm, n); |
119 |
|
|
for (int i = 0; i < n; i++) { |
120 |
|
|
if (s.contains(key[i])) ++sum; |
121 |
|
|
} |
122 |
jsr166 |
1.3 |
timer.finish(); |
123 |
jsr166 |
1.7 |
reallyAssert(sum == expect); |
124 |
dl |
1.1 |
} |
125 |
|
|
|
126 |
|
|
static void t5(String nm, int n, NavigableSet s, Integer[] key, int expect) { |
127 |
|
|
int sum = 0; |
128 |
|
|
timer.start(nm, n/2); |
129 |
|
|
for (int i = n-2; i >= 0; i-=2) { |
130 |
|
|
if (s.remove(key[i])) ++sum; |
131 |
|
|
} |
132 |
jsr166 |
1.3 |
timer.finish(); |
133 |
jsr166 |
1.7 |
reallyAssert(sum == expect); |
134 |
dl |
1.1 |
} |
135 |
|
|
|
136 |
|
|
static void t6(String nm, int n, NavigableSet s, Integer[] k1, Integer[] k2) { |
137 |
|
|
int sum = 0; |
138 |
|
|
timer.start(nm, n * 2); |
139 |
|
|
for (int i = 0; i < n; i++) { |
140 |
|
|
if (s.contains(k1[i])) ++sum; |
141 |
|
|
if (s.contains(k2[i & absentMask])) ++sum; |
142 |
|
|
} |
143 |
jsr166 |
1.3 |
timer.finish(); |
144 |
jsr166 |
1.7 |
reallyAssert(sum == n); |
145 |
dl |
1.1 |
} |
146 |
|
|
|
147 |
|
|
static void t7(String nm, int n, NavigableSet s, Integer[] k1, Integer[] k2) { |
148 |
|
|
int sum = 0; |
149 |
|
|
timer.start(nm, n * 2); |
150 |
|
|
for (int i = 0; i < n; i++) { |
151 |
|
|
if (s.contains(k1[i])) ++sum; |
152 |
|
|
if (s.contains(k2[i & absentMask])) ++sum; |
153 |
|
|
} |
154 |
jsr166 |
1.3 |
timer.finish(); |
155 |
jsr166 |
1.7 |
reallyAssert(sum == n); |
156 |
dl |
1.1 |
} |
157 |
|
|
|
158 |
|
|
static void t8(String nm, int n, NavigableSet s, Integer[] key, int expect) { |
159 |
|
|
int sum = 0; |
160 |
|
|
timer.start(nm, n); |
161 |
|
|
for (int i = 0; i < n; i++) { |
162 |
|
|
if (s.contains(key[i])) ++sum; |
163 |
|
|
} |
164 |
jsr166 |
1.3 |
timer.finish(); |
165 |
jsr166 |
1.7 |
reallyAssert(sum == expect); |
166 |
dl |
1.1 |
} |
167 |
|
|
|
168 |
|
|
static void higherTest(NavigableSet s) { |
169 |
|
|
int sum = 0; |
170 |
|
|
int iters = s.size(); |
171 |
|
|
timer.start("Higher ", iters); |
172 |
|
|
Object e = s.first(); |
173 |
|
|
while (e != null) { |
174 |
|
|
++sum; |
175 |
|
|
e = s.higher(e); |
176 |
|
|
} |
177 |
jsr166 |
1.3 |
timer.finish(); |
178 |
jsr166 |
1.7 |
reallyAssert(sum == iters); |
179 |
dl |
1.1 |
} |
180 |
|
|
|
181 |
|
|
static void lowerTest(NavigableSet s) { |
182 |
|
|
int sum = 0; |
183 |
|
|
int iters = s.size(); |
184 |
|
|
timer.start("Lower ", iters); |
185 |
|
|
Object e = s.first(); |
186 |
|
|
while (e != null) { |
187 |
|
|
++sum; |
188 |
|
|
e = s.higher(e); |
189 |
|
|
} |
190 |
jsr166 |
1.3 |
timer.finish(); |
191 |
jsr166 |
1.7 |
reallyAssert(sum == iters); |
192 |
dl |
1.1 |
} |
193 |
|
|
|
194 |
|
|
static void ceilingTest(NavigableSet s) { |
195 |
|
|
int sum = 0; |
196 |
|
|
int iters = s.size(); |
197 |
|
|
if (iters > absentSize) iters = absentSize; |
198 |
|
|
timer.start("Ceiling ", iters); |
199 |
|
|
for (int i = 0; i < iters; ++i) { |
200 |
|
|
Object e = s.ceiling(absent[i]); |
201 |
|
|
if (e != null) |
202 |
|
|
++sum; |
203 |
|
|
} |
204 |
jsr166 |
1.3 |
timer.finish(); |
205 |
jsr166 |
1.7 |
reallyAssert(sum == iters); |
206 |
dl |
1.1 |
} |
207 |
|
|
|
208 |
|
|
static void floorTest(NavigableSet s) { |
209 |
|
|
int sum = 0; |
210 |
|
|
int iters = s.size(); |
211 |
|
|
if (iters > absentSize) iters = absentSize; |
212 |
|
|
timer.start("Floor ", iters); |
213 |
|
|
for (int i = 1; i < iters; ++i) { |
214 |
|
|
Object e = s.floor(absent[i]); |
215 |
|
|
if (e != null) |
216 |
|
|
++sum; |
217 |
|
|
} |
218 |
jsr166 |
1.3 |
timer.finish(); |
219 |
jsr166 |
1.7 |
reallyAssert(sum == iters-1); |
220 |
dl |
1.1 |
} |
221 |
|
|
|
222 |
|
|
static void ktest(NavigableSet s, int size, Integer[] key) { |
223 |
|
|
timer.start("Contains ", size); |
224 |
|
|
int sum = 0; |
225 |
|
|
for (int i = 0; i < size; i++) { |
226 |
|
|
if (s.contains(key[i])) ++sum; |
227 |
|
|
} |
228 |
jsr166 |
1.3 |
timer.finish(); |
229 |
jsr166 |
1.7 |
reallyAssert(sum == size); |
230 |
dl |
1.1 |
} |
231 |
|
|
|
232 |
|
|
static void ittest1(NavigableSet s, int size) { |
233 |
|
|
int sum = 0; |
234 |
|
|
timer.start("Iter Key ", size); |
235 |
|
|
for (Iterator it = s.iterator(); it.hasNext(); ) { |
236 |
jsr166 |
1.4 |
if (it.next() != MISSING) |
237 |
dl |
1.1 |
++sum; |
238 |
|
|
} |
239 |
jsr166 |
1.3 |
timer.finish(); |
240 |
jsr166 |
1.7 |
reallyAssert(sum == size); |
241 |
dl |
1.1 |
} |
242 |
|
|
|
243 |
|
|
static void ittest(NavigableSet s, int size) { |
244 |
|
|
ittest1(s, size); |
245 |
|
|
} |
246 |
|
|
|
247 |
|
|
static void rittest1(NavigableSet s, int size) { |
248 |
|
|
int sum = 0; |
249 |
|
|
timer.start("Desc Iter Key ", size); |
250 |
|
|
for (Iterator it = s.descendingIterator(); it.hasNext(); ) { |
251 |
jsr166 |
1.4 |
if (it.next() != MISSING) |
252 |
dl |
1.1 |
++sum; |
253 |
|
|
} |
254 |
jsr166 |
1.3 |
timer.finish(); |
255 |
jsr166 |
1.7 |
reallyAssert(sum == size); |
256 |
dl |
1.1 |
} |
257 |
|
|
|
258 |
|
|
static void rittest(NavigableSet s, int size) { |
259 |
|
|
rittest1(s, size); |
260 |
|
|
} |
261 |
|
|
|
262 |
|
|
static void rtest(NavigableSet s, int size) { |
263 |
|
|
timer.start("Remove (iterator) ", size); |
264 |
|
|
for (Iterator it = s.iterator(); it.hasNext(); ) { |
265 |
|
|
it.next(); |
266 |
|
|
it.remove(); |
267 |
|
|
} |
268 |
jsr166 |
1.3 |
timer.finish(); |
269 |
dl |
1.1 |
} |
270 |
|
|
|
271 |
|
|
static void dtest(NavigableSet s, int size, Integer[] key) { |
272 |
|
|
timer.start("Add (addAll) ", size * 2); |
273 |
jsr166 |
1.14 |
final NavigableSet s2; |
274 |
dl |
1.1 |
try { |
275 |
jsr166 |
1.14 |
s2 = (NavigableSet) s.getClass().getConstructor().newInstance(); |
276 |
dl |
1.1 |
s2.addAll(s); |
277 |
|
|
} |
278 |
|
|
catch (Exception e) { e.printStackTrace(); return; } |
279 |
jsr166 |
1.3 |
timer.finish(); |
280 |
|
|
|
281 |
dl |
1.1 |
timer.start("Iter Equals ", size * 2); |
282 |
|
|
boolean eqt = s2.equals(s) && s.equals(s2); |
283 |
jsr166 |
1.7 |
reallyAssert(eqt); |
284 |
jsr166 |
1.3 |
timer.finish(); |
285 |
dl |
1.1 |
|
286 |
|
|
timer.start("Iter HashCode ", size * 2); |
287 |
|
|
int shc = s.hashCode(); |
288 |
|
|
int s2hc = s2.hashCode(); |
289 |
jsr166 |
1.7 |
reallyAssert(shc == s2hc); |
290 |
jsr166 |
1.3 |
timer.finish(); |
291 |
dl |
1.1 |
|
292 |
|
|
timer.start("Add (present) ", size); |
293 |
|
|
s2.addAll(s); |
294 |
jsr166 |
1.3 |
timer.finish(); |
295 |
dl |
1.1 |
|
296 |
|
|
t6("Contains ", size, s2, key, absent); |
297 |
|
|
|
298 |
|
|
boolean as2 = s2.add(absent[absentSize-1]); |
299 |
|
|
reallyAssert(as2); |
300 |
|
|
timer.start("Iter Equals ", size * 2); |
301 |
|
|
eqt = s2.equals(s) && s.equals(s2); |
302 |
|
|
if (as2) |
303 |
jsr166 |
1.7 |
reallyAssert(!eqt); |
304 |
jsr166 |
1.3 |
timer.finish(); |
305 |
dl |
1.1 |
|
306 |
|
|
timer.start("Iter HashCode ", size * 2); |
307 |
|
|
int s1h = s.hashCode(); |
308 |
|
|
int s2h = s2.hashCode(); |
309 |
|
|
if (as2) |
310 |
jsr166 |
1.7 |
reallyAssert(s1h != s2h); |
311 |
jsr166 |
1.3 |
timer.finish(); |
312 |
dl |
1.1 |
|
313 |
|
|
timer.start("Clear ", size); |
314 |
|
|
s.clear(); |
315 |
|
|
s2.clear(); |
316 |
jsr166 |
1.3 |
timer.finish(); |
317 |
jsr166 |
1.7 |
reallyAssert(s2.isEmpty() && s.isEmpty()); |
318 |
dl |
1.1 |
} |
319 |
|
|
|
320 |
|
|
static void test(NavigableSet s, Integer[] key) { |
321 |
|
|
int size = key.length; |
322 |
|
|
|
323 |
|
|
t3("Add (absent) ", size, s, key, size); |
324 |
|
|
t3("Add (present) ", size, s, key, 0); |
325 |
|
|
t7("ContainsKey ", size, s, key, absent); |
326 |
|
|
t4("ContainsKey ", size, s, key, size); |
327 |
|
|
ktest(s, size, key); |
328 |
|
|
t4("Contains ", absentSize, s, absent, 0); |
329 |
|
|
t6("Contains ", size, s, key, absent); |
330 |
|
|
t1("Contains (present) ", size, s, key, size); |
331 |
|
|
t1("Contains (absent) ", absentSize, s, absent, 0); |
332 |
|
|
t2("Remove (absent) ", absentSize, s, absent, 0); |
333 |
|
|
t5("Remove (present) ", size, s, key, size / 2); |
334 |
|
|
t3("Add (half present) ", size, s, key, size / 2); |
335 |
|
|
|
336 |
|
|
ittest(s, size); |
337 |
|
|
rittest(s, size); |
338 |
|
|
higherTest(s); |
339 |
|
|
ceilingTest(s); |
340 |
|
|
floorTest(s); |
341 |
|
|
lowerTest(s); |
342 |
|
|
rtest(s, size); |
343 |
|
|
|
344 |
|
|
t4("Contains ", size, s, key, 0); |
345 |
|
|
t2("Remove (absent) ", size, s, key, 0); |
346 |
|
|
t3("Add (presized) ", size, s, key, size); |
347 |
|
|
dtest(s, size, key); |
348 |
|
|
} |
349 |
|
|
|
350 |
|
|
static class TestTimer { |
351 |
|
|
private String name; |
352 |
|
|
private long numOps; |
353 |
|
|
private long startTime; |
354 |
|
|
private String cname; |
355 |
|
|
|
356 |
|
|
static final java.util.TreeMap accum = new java.util.TreeMap(); |
357 |
jsr166 |
1.3 |
|
358 |
dl |
1.1 |
static void printStats() { |
359 |
|
|
for (Iterator it = accum.entrySet().iterator(); it.hasNext(); ) { |
360 |
jsr166 |
1.5 |
Map.Entry e = (Map.Entry) it.next(); |
361 |
|
|
Stats stats = (Stats) e.getValue(); |
362 |
dl |
1.1 |
int n = stats.number; |
363 |
|
|
double t; |
364 |
jsr166 |
1.3 |
if (n > 0) |
365 |
dl |
1.1 |
t = stats.sum / n; |
366 |
|
|
else |
367 |
|
|
t = stats.least; |
368 |
|
|
long nano = Math.round(1000000.0 * t); |
369 |
|
|
System.out.println(e.getKey() + ": " + nano); |
370 |
|
|
} |
371 |
|
|
} |
372 |
jsr166 |
1.3 |
|
373 |
dl |
1.1 |
void start(String name, long numOps) { |
374 |
|
|
this.name = name; |
375 |
|
|
this.cname = classify(); |
376 |
|
|
this.numOps = numOps; |
377 |
|
|
startTime = System.currentTimeMillis(); |
378 |
|
|
} |
379 |
jsr166 |
1.3 |
|
380 |
dl |
1.1 |
String classify() { |
381 |
|
|
if (name.startsWith("Contains")) |
382 |
|
|
return "Contains "; |
383 |
|
|
else if (name.startsWith("Add")) |
384 |
|
|
return "Add "; |
385 |
|
|
else if (name.startsWith("Remove")) |
386 |
|
|
return "Remove "; |
387 |
|
|
else if (name.startsWith("Iter")) |
388 |
|
|
return "Iter "; |
389 |
jsr166 |
1.3 |
else |
390 |
dl |
1.1 |
return null; |
391 |
|
|
} |
392 |
|
|
|
393 |
|
|
void finish() { |
394 |
|
|
long endTime = System.currentTimeMillis(); |
395 |
|
|
long time = endTime - startTime; |
396 |
jsr166 |
1.6 |
double timePerOp = (double) time / numOps; |
397 |
dl |
1.1 |
|
398 |
|
|
Object st = accum.get(name); |
399 |
|
|
if (st == null) |
400 |
|
|
accum.put(name, new Stats(timePerOp)); |
401 |
|
|
else { |
402 |
|
|
Stats stats = (Stats) st; |
403 |
|
|
stats.sum += timePerOp; |
404 |
|
|
stats.number++; |
405 |
|
|
if (timePerOp < stats.least) stats.least = timePerOp; |
406 |
|
|
} |
407 |
|
|
|
408 |
|
|
if (cname != null) { |
409 |
|
|
st = accum.get(cname); |
410 |
|
|
if (st == null) |
411 |
|
|
accum.put(cname, new Stats(timePerOp)); |
412 |
|
|
else { |
413 |
|
|
Stats stats = (Stats) st; |
414 |
|
|
stats.sum += timePerOp; |
415 |
|
|
stats.number++; |
416 |
|
|
if (timePerOp < stats.least) stats.least = timePerOp; |
417 |
|
|
} |
418 |
|
|
} |
419 |
|
|
|
420 |
|
|
} |
421 |
|
|
|
422 |
|
|
} |
423 |
|
|
|
424 |
|
|
static class Stats { |
425 |
|
|
double sum = 0; |
426 |
|
|
double least; |
427 |
|
|
int number = 0; |
428 |
|
|
Stats(double t) { least = t; } |
429 |
|
|
} |
430 |
|
|
|
431 |
|
|
static Random rng = new Random(); |
432 |
|
|
|
433 |
|
|
static void shuffle(Integer[] keys) { |
434 |
|
|
int size = keys.length; |
435 |
jsr166 |
1.9 |
for (int i = size; i > 1; i--) { |
436 |
dl |
1.1 |
int r = rng.nextInt(i); |
437 |
|
|
Integer t = keys[i-1]; |
438 |
|
|
keys[i-1] = keys[r]; |
439 |
|
|
keys[r] = t; |
440 |
|
|
} |
441 |
|
|
} |
442 |
|
|
|
443 |
|
|
} |