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 |
|
*/ |
5 |
– |
import java.util.*; |
10 |
|
import java.io.*; |
11 |
+ |
import java.util.*; |
12 |
|
|
13 |
|
public class NavigableMapCheck { |
14 |
|
|
25 |
|
} |
26 |
|
|
27 |
|
public static void main(String[] args) throws Exception { |
28 |
< |
Class mapClass = null; |
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) { |
35 |
> |
} catch (ClassNotFoundException e) { |
36 |
|
throw new RuntimeException("Class " + args[0] + " not found."); |
37 |
|
} |
38 |
|
} |
39 |
|
|
40 |
< |
|
36 |
< |
if (args.length > 1) |
40 |
> |
if (args.length > 1) |
41 |
|
numTests = Integer.parseInt(args[1]); |
42 |
|
|
43 |
< |
if (args.length > 2) |
43 |
> |
if (args.length > 2) |
44 |
|
size = Integer.parseInt(args[2]); |
45 |
|
|
46 |
|
System.out.println("Testing " + mapClass.getName() + " trials: " + numTests + " size: " + size); |
47 |
|
|
44 |
– |
|
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) |
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) |
57 |
> |
for (int i = 0; i < size; ++i) |
58 |
|
key[i] = new Integer(i * 2 + 1); |
59 |
|
|
57 |
– |
|
60 |
|
for (int rep = 0; rep < numTests; ++rep) { |
61 |
|
runTest(newMap(mapClass), key); |
62 |
|
} |
63 |
|
|
64 |
|
TestTimer.printStats(); |
63 |
– |
|
65 |
|
} |
66 |
|
|
67 |
< |
static NavigableMap newMap(Class cl) { |
67 |
> |
static NavigableMap newMap(Class<?> cl) { |
68 |
|
try { |
69 |
< |
NavigableMap m = (NavigableMap)cl.newInstance(); |
70 |
< |
return m; |
70 |
< |
} catch(Exception e) { |
69 |
> |
return (NavigableMap) cl.getConstructor().newInstance(); |
70 |
> |
} catch (Exception e) { |
71 |
|
throw new RuntimeException("Can't instantiate " + cl + ": " + e); |
72 |
|
} |
73 |
|
} |
74 |
|
|
75 |
– |
|
75 |
|
static void runTest(NavigableMap s, Integer[] key) { |
76 |
|
shuffle(key); |
77 |
|
int size = key.length; |
89 |
|
if (s.get(key[i]) != null) ++sum; |
90 |
|
} |
91 |
|
} |
92 |
< |
timer.finish(); |
93 |
< |
reallyAssert (sum == expect * iters); |
92 |
> |
timer.finish(); |
93 |
> |
reallyAssert(sum == expect * iters); |
94 |
|
} |
95 |
|
|
96 |
|
static void t2(String nm, int n, NavigableMap s, Integer[] key, int expect) { |
99 |
|
for (int i = 0; i < n; i++) { |
100 |
|
if (s.remove(key[i]) != null) ++sum; |
101 |
|
} |
102 |
< |
timer.finish(); |
103 |
< |
reallyAssert (sum == expect); |
102 |
> |
timer.finish(); |
103 |
> |
reallyAssert(sum == expect); |
104 |
|
} |
105 |
|
|
106 |
|
static void t3(String nm, int n, NavigableMap s, Integer[] key, int expect) { |
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); |
112 |
> |
timer.finish(); |
113 |
> |
reallyAssert(sum == expect); |
114 |
|
} |
115 |
|
|
116 |
|
static void t4(String nm, int n, NavigableMap s, Integer[] key, int expect) { |
119 |
|
for (int i = 0; i < n; i++) { |
120 |
|
if (s.containsKey(key[i])) ++sum; |
121 |
|
} |
122 |
< |
timer.finish(); |
123 |
< |
reallyAssert (sum == expect); |
122 |
> |
timer.finish(); |
123 |
> |
reallyAssert(sum == expect); |
124 |
|
} |
125 |
|
|
126 |
|
static void t5(String nm, int n, NavigableMap s, Integer[] key, int expect) { |
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); |
132 |
> |
timer.finish(); |
133 |
> |
reallyAssert(sum == expect); |
134 |
|
} |
135 |
|
|
136 |
|
static void t6(String nm, int n, NavigableMap s, Integer[] k1, Integer[] k2) { |
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); |
143 |
> |
timer.finish(); |
144 |
> |
reallyAssert(sum == n); |
145 |
|
} |
146 |
|
|
147 |
|
static void t7(String nm, int n, NavigableMap s, Integer[] k1, Integer[] k2) { |
151 |
|
if (s.containsKey(k1[i])) ++sum; |
152 |
|
if (s.containsKey(k2[i & absentMask])) ++sum; |
153 |
|
} |
154 |
< |
timer.finish(); |
155 |
< |
reallyAssert (sum == n); |
154 |
> |
timer.finish(); |
155 |
> |
reallyAssert(sum == n); |
156 |
|
} |
157 |
|
|
158 |
|
static void t8(String nm, int n, NavigableMap s, Integer[] key, int expect) { |
161 |
|
for (int i = 0; i < n; i++) { |
162 |
|
if (s.get(key[i]) != null) ++sum; |
163 |
|
} |
164 |
< |
timer.finish(); |
165 |
< |
reallyAssert (sum == expect); |
164 |
> |
timer.finish(); |
165 |
> |
reallyAssert(sum == expect); |
166 |
|
} |
167 |
|
|
169 |
– |
|
168 |
|
static void t9(NavigableMap s) { |
169 |
|
int sum = 0; |
170 |
|
int iters = 20; |
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); |
175 |
> |
timer.finish(); |
176 |
> |
reallyAssert(sum != 0); |
177 |
|
} |
178 |
|
|
179 |
|
static void higherTest(NavigableMap s) { |
185 |
|
++sum; |
186 |
|
e = s.higherEntry(e.getKey()); |
187 |
|
} |
188 |
< |
timer.finish(); |
189 |
< |
reallyAssert (sum == iters); |
188 |
> |
timer.finish(); |
189 |
> |
reallyAssert(sum == iters); |
190 |
|
} |
191 |
|
|
192 |
|
static void lowerTest(NavigableMap s) { |
198 |
|
++sum; |
199 |
|
e = s.higherEntry(e.getKey()); |
200 |
|
} |
201 |
< |
timer.finish(); |
202 |
< |
reallyAssert (sum == iters); |
201 |
> |
timer.finish(); |
202 |
> |
reallyAssert(sum == iters); |
203 |
|
} |
204 |
|
|
205 |
|
static void ceilingTest(NavigableMap s) { |
212 |
|
if (e != null) |
213 |
|
++sum; |
214 |
|
} |
215 |
< |
timer.finish(); |
216 |
< |
reallyAssert (sum == iters); |
215 |
> |
timer.finish(); |
216 |
> |
reallyAssert(sum == iters); |
217 |
|
} |
218 |
|
|
219 |
|
static void floorTest(NavigableMap s) { |
226 |
|
if (e != null) |
227 |
|
++sum; |
228 |
|
} |
229 |
< |
timer.finish(); |
230 |
< |
reallyAssert (sum == iters-1); |
229 |
> |
timer.finish(); |
230 |
> |
reallyAssert(sum == iters-1); |
231 |
|
} |
232 |
|
|
235 |
– |
|
233 |
|
static void ktest(NavigableMap s, int size, Integer[] key) { |
234 |
|
timer.start("ContainsKey ", size); |
235 |
|
Set ks = s.keySet(); |
237 |
|
for (int i = 0; i < size; i++) { |
238 |
|
if (ks.contains(key[i])) ++sum; |
239 |
|
} |
240 |
< |
timer.finish(); |
241 |
< |
reallyAssert (sum == size); |
240 |
> |
timer.finish(); |
241 |
> |
reallyAssert(sum == size); |
242 |
|
} |
243 |
|
|
247 |
– |
|
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) |
248 |
> |
if (it.next() != MISSING) |
249 |
|
++sum; |
250 |
|
} |
251 |
< |
timer.finish(); |
252 |
< |
reallyAssert (sum == size); |
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) |
259 |
> |
if (it.next() != MISSING) |
260 |
|
++sum; |
261 |
|
} |
262 |
< |
timer.finish(); |
263 |
< |
reallyAssert (sum == size); |
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) |
269 |
> |
if (it.next() != MISSING) |
270 |
|
++sum; |
271 |
|
} |
272 |
< |
timer.finish(); |
273 |
< |
reallyAssert (sum == size); |
272 |
> |
timer.finish(); |
273 |
> |
reallyAssert(sum == size); |
274 |
|
} |
275 |
|
|
276 |
|
static void ittest(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) |
286 |
> |
if (it.next() != MISSING) |
287 |
|
++sum; |
288 |
|
} |
289 |
< |
timer.finish(); |
290 |
< |
reallyAssert (sum == size); |
295 |
< |
} |
296 |
< |
|
297 |
< |
static void rittest2(NavigableMap s, int size) { |
298 |
< |
int sum = 0; |
299 |
< |
timer.start("Desc Iter Entry ", size); |
300 |
< |
for (Iterator it = s.descendingEntrySet().iterator(); it.hasNext(); ) { |
301 |
< |
if(it.next() != MISSING) |
302 |
< |
++sum; |
303 |
< |
} |
304 |
< |
timer.finish(); |
305 |
< |
reallyAssert (sum == size); |
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); |
295 |
> |
// rittest2(s, size); |
296 |
|
} |
297 |
|
|
313 |
– |
|
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(); |
304 |
> |
timer.finish(); |
305 |
|
} |
306 |
|
|
307 |
|
static void rvtest(NavigableMap s, int size) { |
310 |
|
it.next(); |
311 |
|
it.remove(); |
312 |
|
} |
313 |
< |
timer.finish(); |
313 |
> |
timer.finish(); |
314 |
|
} |
315 |
|
|
332 |
– |
|
316 |
|
static void dtest(NavigableMap s, int size, Integer[] key) { |
317 |
|
timer.start("Put (putAll) ", size * 2); |
318 |
< |
NavigableMap s2 = null; |
318 |
> |
final NavigableMap s2; |
319 |
|
try { |
320 |
< |
s2 = (NavigableMap) (s.getClass().newInstance()); |
320 |
> |
s2 = (NavigableMap) s.getClass().getConstructor().newInstance(); |
321 |
|
s2.putAll(s); |
322 |
|
} |
323 |
|
catch (Exception e) { e.printStackTrace(); return; } |
324 |
< |
timer.finish(); |
325 |
< |
|
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(); |
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(); |
334 |
> |
reallyAssert(shc == s2hc); |
335 |
> |
timer.finish(); |
336 |
|
|
337 |
|
timer.start("Put (present) ", size); |
338 |
|
s2.putAll(s); |
339 |
< |
timer.finish(); |
339 |
> |
timer.finish(); |
340 |
|
|
341 |
|
timer.start("Iter EntrySet contains ", size * 2); |
342 |
|
Set es2 = s2.entrySet(); |
345 |
|
Object entry = i1.next(); |
346 |
|
if (es2.contains(entry)) ++sum; |
347 |
|
} |
348 |
< |
timer.finish(); |
349 |
< |
reallyAssert (sum == size); |
348 |
> |
timer.finish(); |
349 |
> |
reallyAssert(sum == size); |
350 |
|
|
351 |
|
t6("Get ", size, s2, key, absent); |
352 |
|
|
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(); |
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(); |
363 |
> |
reallyAssert(s1h != s2h); |
364 |
> |
timer.finish(); |
365 |
|
|
366 |
|
s2.put(key[size-1], hold); |
367 |
|
timer.start("Remove (iterator) ", size * 2); |
369 |
|
Set es = s.entrySet(); |
370 |
|
while (s2i.hasNext()) |
371 |
|
es.remove(s2i.next()); |
372 |
< |
timer.finish(); |
372 |
> |
timer.finish(); |
373 |
|
|
374 |
< |
reallyAssert (s.isEmpty()); |
374 |
> |
reallyAssert(s.isEmpty()); |
375 |
|
|
376 |
|
timer.start("Clear ", size); |
377 |
|
s2.clear(); |
378 |
< |
timer.finish(); |
379 |
< |
reallyAssert (s2.isEmpty() && s.isEmpty()); |
378 |
> |
timer.finish(); |
379 |
> |
reallyAssert(s2.isEmpty() && s.isEmpty()); |
380 |
|
} |
381 |
|
|
399 |
– |
|
400 |
– |
|
382 |
|
static void test(NavigableMap s, Integer[] key) { |
383 |
|
int size = key.length; |
384 |
|
|
417 |
|
private String cname; |
418 |
|
|
419 |
|
static final java.util.TreeMap accum = new java.util.TreeMap(); |
420 |
< |
|
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())); |
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) |
427 |
> |
if (n > 0) |
428 |
|
t = stats.sum / n; |
429 |
|
else |
430 |
|
t = stats.least; |
432 |
|
System.out.println(e.getKey() + ": " + nano); |
433 |
|
} |
434 |
|
} |
435 |
< |
|
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 |
|
} |
461 |
– |
|
442 |
|
|
443 |
|
String classify() { |
444 |
|
if (name.startsWith("Get")) |
449 |
|
return "Remove "; |
450 |
|
else if (name.startsWith("Iter")) |
451 |
|
return "Iter "; |
452 |
< |
else |
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; |
459 |
> |
double timePerOp = (double) time /numOps; |
460 |
|
|
461 |
|
Object st = accum.get(name); |
462 |
|
if (st == null) |
479 |
|
if (timePerOp < stats.least) stats.least = timePerOp; |
480 |
|
} |
481 |
|
} |
502 |
– |
|
482 |
|
} |
504 |
– |
|
483 |
|
} |
484 |
|
|
485 |
|
static class Stats { |
489 |
|
Stats(double t) { least = t; } |
490 |
|
} |
491 |
|
|
492 |
< |
static Random rng = new Random(); |
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--) { |
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]; |
502 |
|
} |
503 |
|
|
504 |
|
} |
527 |
– |
|