1 |
/* |
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 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
*/ |
6 |
/** |
7 |
* @test |
8 |
* @synopsis Times and checks basic map operations |
9 |
*/ |
10 |
import java.io.*; |
11 |
import java.util.*; |
12 |
|
13 |
public class NavigableMapCheck { |
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 |
Class<?> mapClass = null; |
29 |
int numTests = 50; |
30 |
int size = 50000; |
31 |
|
32 |
if (args.length > 0) { |
33 |
try { |
34 |
mapClass = Class.forName(args[0]); |
35 |
} catch (ClassNotFoundException e) { |
36 |
throw new RuntimeException("Class " + args[0] + " not found."); |
37 |
} |
38 |
} |
39 |
|
40 |
if (args.length > 1) |
41 |
numTests = Integer.parseInt(args[1]); |
42 |
|
43 |
if (args.length > 2) |
44 |
size = Integer.parseInt(args[2]); |
45 |
|
46 |
System.out.println("Testing " + mapClass.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 |
for (int i = 0; i < absentSize; ++i) |
54 |
absent[i] = new Integer(i * 2); |
55 |
|
56 |
Integer[] key = new Integer[size]; |
57 |
for (int i = 0; i < size; ++i) |
58 |
key[i] = new Integer(i * 2 + 1); |
59 |
|
60 |
for (int rep = 0; rep < numTests; ++rep) { |
61 |
runTest(newMap(mapClass), key); |
62 |
} |
63 |
|
64 |
TestTimer.printStats(); |
65 |
} |
66 |
|
67 |
static NavigableMap newMap(Class<?> cl) { |
68 |
try { |
69 |
return (NavigableMap) cl.getConstructor().newInstance(); |
70 |
} catch (Exception e) { |
71 |
throw new RuntimeException("Can't instantiate " + cl + ": " + e); |
72 |
} |
73 |
} |
74 |
|
75 |
static void runTest(NavigableMap 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, NavigableMap 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.get(key[i]) != null) ++sum; |
90 |
} |
91 |
} |
92 |
timer.finish(); |
93 |
reallyAssert(sum == expect * iters); |
94 |
} |
95 |
|
96 |
static void t2(String nm, int n, NavigableMap 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]) != null) ++sum; |
101 |
} |
102 |
timer.finish(); |
103 |
reallyAssert(sum == expect); |
104 |
} |
105 |
|
106 |
static void t3(String nm, int n, NavigableMap 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.put(key[i], absent[i & absentMask]) == null) ++sum; |
111 |
} |
112 |
timer.finish(); |
113 |
reallyAssert(sum == expect); |
114 |
} |
115 |
|
116 |
static void t4(String nm, int n, NavigableMap 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.containsKey(key[i])) ++sum; |
121 |
} |
122 |
timer.finish(); |
123 |
reallyAssert(sum == expect); |
124 |
} |
125 |
|
126 |
static void t5(String nm, int n, NavigableMap 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]) != null) ++sum; |
131 |
} |
132 |
timer.finish(); |
133 |
reallyAssert(sum == expect); |
134 |
} |
135 |
|
136 |
static void t6(String nm, int n, NavigableMap 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.get(k1[i]) != null) ++sum; |
141 |
if (s.get(k2[i & absentMask]) != null) ++sum; |
142 |
} |
143 |
timer.finish(); |
144 |
reallyAssert(sum == n); |
145 |
} |
146 |
|
147 |
static void t7(String nm, int n, NavigableMap 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.containsKey(k1[i])) ++sum; |
152 |
if (s.containsKey(k2[i & absentMask])) ++sum; |
153 |
} |
154 |
timer.finish(); |
155 |
reallyAssert(sum == n); |
156 |
} |
157 |
|
158 |
static void t8(String nm, int n, NavigableMap 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.get(key[i]) != null) ++sum; |
163 |
} |
164 |
timer.finish(); |
165 |
reallyAssert(sum == expect); |
166 |
} |
167 |
|
168 |
static void t9(NavigableMap s) { |
169 |
int sum = 0; |
170 |
int iters = 20; |
171 |
timer.start("ContainsValue (/n) ", iters * s.size()); |
172 |
int step = absentSize / iters; |
173 |
for (int i = 0; i < absentSize; i += step) |
174 |
if (s.containsValue(absent[i])) ++sum; |
175 |
timer.finish(); |
176 |
reallyAssert(sum != 0); |
177 |
} |
178 |
|
179 |
static void higherTest(NavigableMap s) { |
180 |
int sum = 0; |
181 |
int iters = s.size(); |
182 |
timer.start("Higher ", iters); |
183 |
Map.Entry e = s.firstEntry(); |
184 |
while (e != null) { |
185 |
++sum; |
186 |
e = s.higherEntry(e.getKey()); |
187 |
} |
188 |
timer.finish(); |
189 |
reallyAssert(sum == iters); |
190 |
} |
191 |
|
192 |
static void lowerTest(NavigableMap s) { |
193 |
int sum = 0; |
194 |
int iters = s.size(); |
195 |
timer.start("Lower ", iters); |
196 |
Map.Entry e = s.firstEntry(); |
197 |
while (e != null) { |
198 |
++sum; |
199 |
e = s.higherEntry(e.getKey()); |
200 |
} |
201 |
timer.finish(); |
202 |
reallyAssert(sum == iters); |
203 |
} |
204 |
|
205 |
static void ceilingTest(NavigableMap s) { |
206 |
int sum = 0; |
207 |
int iters = s.size(); |
208 |
if (iters > absentSize) iters = absentSize; |
209 |
timer.start("Ceiling ", iters); |
210 |
for (int i = 0; i < iters; ++i) { |
211 |
Map.Entry e = s.ceilingEntry(absent[i]); |
212 |
if (e != null) |
213 |
++sum; |
214 |
} |
215 |
timer.finish(); |
216 |
reallyAssert(sum == iters); |
217 |
} |
218 |
|
219 |
static void floorTest(NavigableMap s) { |
220 |
int sum = 0; |
221 |
int iters = s.size(); |
222 |
if (iters > absentSize) iters = absentSize; |
223 |
timer.start("Floor ", iters); |
224 |
for (int i = 1; i < iters; ++i) { |
225 |
Map.Entry e = s.floorEntry(absent[i]); |
226 |
if (e != null) |
227 |
++sum; |
228 |
} |
229 |
timer.finish(); |
230 |
reallyAssert(sum == iters-1); |
231 |
} |
232 |
|
233 |
static void ktest(NavigableMap s, int size, Integer[] key) { |
234 |
timer.start("ContainsKey ", size); |
235 |
Set ks = s.keySet(); |
236 |
int sum = 0; |
237 |
for (int i = 0; i < size; i++) { |
238 |
if (ks.contains(key[i])) ++sum; |
239 |
} |
240 |
timer.finish(); |
241 |
reallyAssert(sum == size); |
242 |
} |
243 |
|
244 |
static void ittest1(NavigableMap s, int size) { |
245 |
int sum = 0; |
246 |
timer.start("Iter Key ", size); |
247 |
for (Iterator it = s.keySet().iterator(); it.hasNext(); ) { |
248 |
if (it.next() != MISSING) |
249 |
++sum; |
250 |
} |
251 |
timer.finish(); |
252 |
reallyAssert(sum == size); |
253 |
} |
254 |
|
255 |
static void ittest2(NavigableMap s, int size) { |
256 |
int sum = 0; |
257 |
timer.start("Iter Value ", size); |
258 |
for (Iterator it = s.values().iterator(); it.hasNext(); ) { |
259 |
if (it.next() != MISSING) |
260 |
++sum; |
261 |
} |
262 |
timer.finish(); |
263 |
reallyAssert(sum == size); |
264 |
} |
265 |
static void ittest3(NavigableMap s, int size) { |
266 |
int sum = 0; |
267 |
timer.start("Iter Entry ", size); |
268 |
for (Iterator it = s.entrySet().iterator(); it.hasNext(); ) { |
269 |
if (it.next() != MISSING) |
270 |
++sum; |
271 |
} |
272 |
timer.finish(); |
273 |
reallyAssert(sum == size); |
274 |
} |
275 |
|
276 |
static void ittest(NavigableMap s, int size) { |
277 |
ittest1(s, size); |
278 |
ittest2(s, size); |
279 |
ittest3(s, size); |
280 |
} |
281 |
|
282 |
static void rittest1(NavigableMap s, int size) { |
283 |
int sum = 0; |
284 |
timer.start("Desc Iter Key ", size); |
285 |
for (Iterator it = s.descendingKeySet().iterator(); it.hasNext(); ) { |
286 |
if (it.next() != MISSING) |
287 |
++sum; |
288 |
} |
289 |
timer.finish(); |
290 |
reallyAssert(sum == size); |
291 |
} |
292 |
|
293 |
static void rittest(NavigableMap s, int size) { |
294 |
rittest1(s, size); |
295 |
// rittest2(s, size); |
296 |
} |
297 |
|
298 |
static void rtest(NavigableMap s, int size) { |
299 |
timer.start("Remove (iterator) ", size); |
300 |
for (Iterator it = s.keySet().iterator(); it.hasNext(); ) { |
301 |
it.next(); |
302 |
it.remove(); |
303 |
} |
304 |
timer.finish(); |
305 |
} |
306 |
|
307 |
static void rvtest(NavigableMap s, int size) { |
308 |
timer.start("Remove (iterator) ", size); |
309 |
for (Iterator it = s.values().iterator(); it.hasNext(); ) { |
310 |
it.next(); |
311 |
it.remove(); |
312 |
} |
313 |
timer.finish(); |
314 |
} |
315 |
|
316 |
static void dtest(NavigableMap s, int size, Integer[] key) { |
317 |
timer.start("Put (putAll) ", size * 2); |
318 |
final NavigableMap s2; |
319 |
try { |
320 |
s2 = (NavigableMap) s.getClass().getConstructor().newInstance(); |
321 |
s2.putAll(s); |
322 |
} |
323 |
catch (Exception e) { e.printStackTrace(); return; } |
324 |
timer.finish(); |
325 |
|
326 |
timer.start("Iter Equals ", size * 2); |
327 |
boolean eqt = s2.equals(s) && s.equals(s2); |
328 |
reallyAssert(eqt); |
329 |
timer.finish(); |
330 |
|
331 |
timer.start("Iter HashCode ", size * 2); |
332 |
int shc = s.hashCode(); |
333 |
int s2hc = s2.hashCode(); |
334 |
reallyAssert(shc == s2hc); |
335 |
timer.finish(); |
336 |
|
337 |
timer.start("Put (present) ", size); |
338 |
s2.putAll(s); |
339 |
timer.finish(); |
340 |
|
341 |
timer.start("Iter EntrySet contains ", size * 2); |
342 |
Set es2 = s2.entrySet(); |
343 |
int sum = 0; |
344 |
for (Iterator i1 = s.entrySet().iterator(); i1.hasNext(); ) { |
345 |
Object entry = i1.next(); |
346 |
if (es2.contains(entry)) ++sum; |
347 |
} |
348 |
timer.finish(); |
349 |
reallyAssert(sum == size); |
350 |
|
351 |
t6("Get ", size, s2, key, absent); |
352 |
|
353 |
Object hold = s2.get(key[size-1]); |
354 |
s2.put(key[size-1], absent[0]); |
355 |
timer.start("Iter Equals ", size * 2); |
356 |
eqt = s2.equals(s) && s.equals(s2); |
357 |
reallyAssert(!eqt); |
358 |
timer.finish(); |
359 |
|
360 |
timer.start("Iter HashCode ", size * 2); |
361 |
int s1h = s.hashCode(); |
362 |
int s2h = s2.hashCode(); |
363 |
reallyAssert(s1h != s2h); |
364 |
timer.finish(); |
365 |
|
366 |
s2.put(key[size-1], hold); |
367 |
timer.start("Remove (iterator) ", size * 2); |
368 |
Iterator s2i = s2.entrySet().iterator(); |
369 |
Set es = s.entrySet(); |
370 |
while (s2i.hasNext()) |
371 |
es.remove(s2i.next()); |
372 |
timer.finish(); |
373 |
|
374 |
reallyAssert(s.isEmpty()); |
375 |
|
376 |
timer.start("Clear ", size); |
377 |
s2.clear(); |
378 |
timer.finish(); |
379 |
reallyAssert(s2.isEmpty() && s.isEmpty()); |
380 |
} |
381 |
|
382 |
static void test(NavigableMap s, Integer[] key) { |
383 |
int size = key.length; |
384 |
|
385 |
t3("Put (absent) ", size, s, key, size); |
386 |
t3("Put (present) ", size, s, key, 0); |
387 |
t7("ContainsKey ", size, s, key, absent); |
388 |
t4("ContainsKey ", size, s, key, size); |
389 |
ktest(s, size, key); |
390 |
t4("ContainsKey ", absentSize, s, absent, 0); |
391 |
t6("Get ", size, s, key, absent); |
392 |
t1("Get (present) ", size, s, key, size); |
393 |
t1("Get (absent) ", absentSize, s, absent, 0); |
394 |
t2("Remove (absent) ", absentSize, s, absent, 0); |
395 |
t5("Remove (present) ", size, s, key, size / 2); |
396 |
t3("Put (half present) ", size, s, key, size / 2); |
397 |
|
398 |
ittest(s, size); |
399 |
rittest(s, size); |
400 |
higherTest(s); |
401 |
ceilingTest(s); |
402 |
floorTest(s); |
403 |
lowerTest(s); |
404 |
t9(s); |
405 |
rtest(s, size); |
406 |
|
407 |
t4("ContainsKey ", size, s, key, 0); |
408 |
t2("Remove (absent) ", size, s, key, 0); |
409 |
t3("Put (presized) ", size, s, key, size); |
410 |
dtest(s, size, key); |
411 |
} |
412 |
|
413 |
static class TestTimer { |
414 |
private String name; |
415 |
private long numOps; |
416 |
private long startTime; |
417 |
private String cname; |
418 |
|
419 |
static final java.util.TreeMap accum = new java.util.TreeMap(); |
420 |
|
421 |
static void printStats() { |
422 |
for (Iterator it = accum.entrySet().iterator(); it.hasNext(); ) { |
423 |
Map.Entry e = (Map.Entry) it.next(); |
424 |
Stats stats = (Stats) e.getValue(); |
425 |
int n = stats.number; |
426 |
double t; |
427 |
if (n > 0) |
428 |
t = stats.sum / n; |
429 |
else |
430 |
t = stats.least; |
431 |
long nano = Math.round(1000000.0 * t); |
432 |
System.out.println(e.getKey() + ": " + nano); |
433 |
} |
434 |
} |
435 |
|
436 |
void start(String name, long numOps) { |
437 |
this.name = name; |
438 |
this.cname = classify(); |
439 |
this.numOps = numOps; |
440 |
startTime = System.currentTimeMillis(); |
441 |
} |
442 |
|
443 |
String classify() { |
444 |
if (name.startsWith("Get")) |
445 |
return "Get "; |
446 |
else if (name.startsWith("Put")) |
447 |
return "Put "; |
448 |
else if (name.startsWith("Remove")) |
449 |
return "Remove "; |
450 |
else if (name.startsWith("Iter")) |
451 |
return "Iter "; |
452 |
else |
453 |
return null; |
454 |
} |
455 |
|
456 |
void finish() { |
457 |
long endTime = System.currentTimeMillis(); |
458 |
long time = endTime - startTime; |
459 |
double timePerOp = (double) time /numOps; |
460 |
|
461 |
Object st = accum.get(name); |
462 |
if (st == null) |
463 |
accum.put(name, new Stats(timePerOp)); |
464 |
else { |
465 |
Stats stats = (Stats) st; |
466 |
stats.sum += timePerOp; |
467 |
stats.number++; |
468 |
if (timePerOp < stats.least) stats.least = timePerOp; |
469 |
} |
470 |
|
471 |
if (cname != null) { |
472 |
st = accum.get(cname); |
473 |
if (st == null) |
474 |
accum.put(cname, new Stats(timePerOp)); |
475 |
else { |
476 |
Stats stats = (Stats) st; |
477 |
stats.sum += timePerOp; |
478 |
stats.number++; |
479 |
if (timePerOp < stats.least) stats.least = timePerOp; |
480 |
} |
481 |
} |
482 |
} |
483 |
} |
484 |
|
485 |
static class Stats { |
486 |
double sum = 0; |
487 |
double least; |
488 |
int number = 0; |
489 |
Stats(double t) { least = t; } |
490 |
} |
491 |
|
492 |
static Random rng = new Random(111); |
493 |
|
494 |
static void shuffle(Integer[] keys) { |
495 |
int size = keys.length; |
496 |
for (int i = size; i > 1; i--) { |
497 |
int r = rng.nextInt(i); |
498 |
Integer t = keys[i-1]; |
499 |
keys[i-1] = keys[r]; |
500 |
keys[r] = t; |
501 |
} |
502 |
} |
503 |
|
504 |
} |