24 |
|
public static void main(String[] args) throws Exception { |
25 |
|
Class mapClass = java.util.concurrent.ConcurrentHashMap.class; |
26 |
|
int numTests = 50; |
27 |
< |
int size = 50000; |
27 |
> |
int size = 75000; |
28 |
|
|
29 |
|
if (args.length > 0) { |
30 |
|
try { |
46 |
|
System.out.println("Testing " + mapClass.getName() + " trials: " + numTests + " size: " + size); |
47 |
|
|
48 |
|
absentSize = 4; |
49 |
< |
while (absentSize <= size) absentSize <<= 1; |
49 |
> |
while (absentSize < size) absentSize <<= 1; |
50 |
|
absentMask = absentSize-1; |
51 |
|
absent = new Integer[absentSize]; |
52 |
< |
for (int i = 0; i < absentSize; ++i) |
53 |
< |
absent[i] = new Integer(2 * (i - 1) + 1); |
52 |
> |
for (int i = 0; i < absentSize/2; ++i) |
53 |
> |
absent[i] = Integer.valueOf(-i - 1); |
54 |
> |
for (int i = absentSize/2; i < absentSize; ++i) |
55 |
> |
absent[i] = Integer.valueOf(size + i + 1); |
56 |
|
|
57 |
|
Integer[] key = new Integer[size]; |
58 |
|
for (int i = 0; i < size; ++i) |
59 |
< |
key[i] = new Integer(2 * i); |
59 |
> |
key[i] = Integer.valueOf(i); |
60 |
|
|
61 |
|
for (int rep = 0; rep < numTests; ++rep) { |
60 |
– |
shuffle(absent); |
62 |
|
runTest(newMap(mapClass), key); |
63 |
+ |
if ((rep & 3) == 3 && rep < numTests - 1) { |
64 |
+ |
shuffle(key); |
65 |
+ |
// Thread.sleep(50); |
66 |
+ |
} |
67 |
|
} |
68 |
|
|
69 |
|
TestTimer.printStats(); |
84 |
|
|
85 |
|
|
86 |
|
static void runTest(Map<Integer,Integer> s, Integer[] key) { |
82 |
– |
shuffle(key); |
87 |
|
int size = key.length; |
88 |
|
long startTime = System.nanoTime(); |
89 |
|
test(s, key); |
90 |
|
long time = System.nanoTime() - startTime; |
91 |
|
} |
92 |
|
|
93 |
< |
|
90 |
< |
static void t1(String nm, int n, Map<Integer,Integer> s, Integer[] key, int expect) { |
93 |
> |
static void t1(String nm, int n, Map<Integer,Integer> s, Integer[] key, int expect, int iters) { |
94 |
|
int sum = 0; |
92 |
– |
int iters = 4; |
95 |
|
timer.start(nm, n * iters); |
96 |
|
for (int j = 0; j < iters; ++j) { |
97 |
|
for (int i = 0; i < n; i++) { |
102 |
|
reallyAssert (sum == expect * iters); |
103 |
|
} |
104 |
|
|
105 |
+ |
static void t1Boxed(String nm, int n, Map<Integer,Integer> s, Integer[] key, int expect) { |
106 |
+ |
int sum = 0; |
107 |
+ |
int iters = 8; |
108 |
+ |
timer.start(nm, n * iters); |
109 |
+ |
for (int j = 0; j < iters; ++j) { |
110 |
+ |
for (int i = 0; i < n; i++) { |
111 |
+ |
if ((Integer)(s.get(i)) != i) ++sum; |
112 |
+ |
} |
113 |
+ |
} |
114 |
+ |
timer.finish(); |
115 |
+ |
reallyAssert (sum == expect * iters); |
116 |
+ |
} |
117 |
+ |
|
118 |
+ |
|
119 |
|
static void t2(String nm, int n, Map<Integer,Integer> s, Integer[] key, int expect) { |
120 |
|
int sum = 0; |
121 |
|
timer.start(nm, n); |
223 |
|
++sum; |
224 |
|
} |
225 |
|
timer.finish(); |
226 |
+ |
// if (sum != size) |
227 |
+ |
// System.out.println("iters " + sum + " size " + size); |
228 |
|
reallyAssert (sum == size); |
229 |
|
} |
230 |
|
|
236 |
|
++sum; |
237 |
|
} |
238 |
|
timer.finish(); |
239 |
+ |
// if (sum != size) |
240 |
+ |
// System.out.println("iters " + sum + " size " + size); |
241 |
|
reallyAssert (sum == size); |
242 |
|
} |
243 |
|
static void ittest3(Map<Integer,Integer> s, int size) { |
288 |
|
|
289 |
|
|
290 |
|
static void ittest(Map<Integer,Integer> s, int size) { |
291 |
< |
ittest1(s, size); |
292 |
< |
ittest2(s, size); |
293 |
< |
ittest3(s, size); |
291 |
> |
for (int i = 0; i < 4; ++i) { |
292 |
> |
ittest1(s, size); |
293 |
> |
ittest2(s, size); |
294 |
> |
ittest3(s, size); |
295 |
> |
} |
296 |
|
// for (int i = 0; i < size-1; ++i) |
297 |
|
// ittest4(s, size, i); |
298 |
|
} |
366 |
|
it.next(); |
367 |
|
it.remove(); |
368 |
|
} |
369 |
+ |
reallyAssert(s.isEmpty()); |
370 |
|
timer.finish(); |
371 |
|
} |
372 |
|
|
373 |
< |
static void rvtest(Map<Integer,Integer> s, int size) { |
374 |
< |
timer.start("Remove (iterator) ", size); |
375 |
< |
for (Iterator it = s.values().iterator(); it.hasNext(); ) { |
376 |
< |
it.next(); |
377 |
< |
it.remove(); |
373 |
> |
static void stest(Map<Integer,Integer> s, int size) throws Exception { |
374 |
> |
if (!(s instanceof Serializable)) |
375 |
> |
return; |
376 |
> |
System.out.print("Serialize : "); |
377 |
> |
|
378 |
> |
for (int i = 0; i < size; i++) { |
379 |
> |
s.put(Integer.valueOf(i), Integer.valueOf(1)); |
380 |
|
} |
381 |
< |
timer.finish(); |
381 |
> |
|
382 |
> |
long startTime = System.nanoTime(); |
383 |
> |
|
384 |
> |
FileOutputStream fs = new FileOutputStream("IntMapCheck.dat"); |
385 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(fs)); |
386 |
> |
out.writeObject(s); |
387 |
> |
out.close(); |
388 |
> |
|
389 |
> |
FileInputStream is = new FileInputStream("IntMapCheck.dat"); |
390 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(is)); |
391 |
> |
Map<Integer,Integer> m = (Map<Integer,Integer>)in.readObject(); |
392 |
> |
|
393 |
> |
long endTime = System.nanoTime(); |
394 |
> |
long time = endTime - startTime; |
395 |
> |
|
396 |
> |
System.out.print(time + "ms"); |
397 |
> |
|
398 |
> |
if (s instanceof IdentityHashMap) return; |
399 |
> |
reallyAssert (s.equals(m)); |
400 |
|
} |
401 |
|
|
402 |
+ |
|
403 |
+ |
static void test(Map<Integer,Integer> s, Integer[] key) { |
404 |
+ |
int size = key.length; |
405 |
+ |
|
406 |
+ |
t3("Put (absent) ", size, s, key, size); |
407 |
+ |
reallyAssert(s.size() == size); |
408 |
+ |
t1("Get (present) ", size, s, key, size, 8); |
409 |
+ |
t1Boxed("Get boxed (present) ", size, s, key, size); |
410 |
+ |
ittest1(s, size); |
411 |
+ |
t3("Put (present) ", size, s, key, 0); |
412 |
+ |
reallyAssert(s.size() == size); |
413 |
+ |
t7("ContainsKey ", size, s, key, absent); |
414 |
+ |
t4("ContainsKey ", size, s, key, size); |
415 |
+ |
ktest(s, size, key); |
416 |
+ |
t4("ContainsKey ", absentSize, s, absent, 0); |
417 |
+ |
t6("Get ", size, s, key, absent); |
418 |
+ |
t1("Get (present) ", size, s, key, size, 8); |
419 |
+ |
t1("Get (absent) ", absentSize, s, absent, 0, 1); |
420 |
+ |
reallyAssert(s.size() == size); |
421 |
+ |
t2("Remove (absent) ", absentSize, s, absent, 0); |
422 |
+ |
reallyAssert(s.size() == size); |
423 |
+ |
t5("Remove (present) ", size, s, key, size / 2); |
424 |
+ |
reallyAssert(s.size() == size / 2); |
425 |
+ |
t1("Get ", size, s, key, size / 2, 8); |
426 |
+ |
ittest1(s, size / 2); |
427 |
+ |
t3("Put (half present) ", size, s, key, size / 2); |
428 |
+ |
reallyAssert(s.size() == size); |
429 |
+ |
t1("Get (present) ", size, s, key, size, 4); |
430 |
+ |
|
431 |
+ |
entest(s, size); |
432 |
+ |
t9(s); |
433 |
+ |
reallyAssert(s.size() == size); |
434 |
+ |
timer.start("Clear ", size); |
435 |
+ |
s.clear(); |
436 |
+ |
timer.finish(); |
437 |
+ |
t1("Get (absent) ", size, s, key, 0, 1); |
438 |
+ |
t4("ContainsKey ", size, s, key, 0); |
439 |
+ |
t2("Remove (absent) ", size, s, key, 0); |
440 |
+ |
t3("Put (presized) ", size, s, key, size); |
441 |
+ |
t1("Get (present) ", size, s, key, size, 4); |
442 |
+ |
reallyAssert(s.size() == size); |
443 |
+ |
ittest(s, size); |
444 |
+ |
rtest(s, size); |
445 |
+ |
reallyAssert(s.size() == 0); |
446 |
+ |
timer.start("Clear ", size); |
447 |
+ |
s.clear(); |
448 |
+ |
timer.finish(); |
449 |
+ |
t3("Put (presized) ", size, s, key, size); |
450 |
|
|
360 |
– |
static void dtest(Map<Integer,Integer> s, int size, Integer[] key) { |
451 |
|
timer.start("Put (putAll) ", size * 2); |
452 |
|
Map<Integer,Integer> s2 = null; |
453 |
|
try { |
468 |
|
reallyAssert (shc == s2hc); |
469 |
|
timer.finish(); |
470 |
|
|
471 |
< |
timer.start("Put (present) ", size); |
471 |
> |
timer.start("Put (present) ", size * 2); |
472 |
|
s2.putAll(s); |
473 |
|
timer.finish(); |
474 |
|
|
475 |
+ |
timer.start("Put (present) ", size); |
476 |
+ |
int ipsum = 0; |
477 |
+ |
for (Iterator i0 = s.entrySet().iterator(); i0.hasNext(); ) { |
478 |
+ |
Map.Entry<Integer,Integer> me = (Map.Entry<Integer,Integer>)(i0.next()); |
479 |
+ |
if (s2.put(me.getKey(), me.getValue()) != null) |
480 |
+ |
++ipsum; |
481 |
+ |
} |
482 |
+ |
reallyAssert (ipsum == s.size()); |
483 |
+ |
timer.finish(); |
484 |
+ |
|
485 |
|
timer.start("Iter EntrySet contains ", size * 2); |
486 |
|
Set es2 = s2.entrySet(); |
487 |
|
int sum = 0; |
492 |
|
timer.finish(); |
493 |
|
reallyAssert (sum == size); |
494 |
|
|
395 |
– |
t6("Get ", size, s2, key, absent); |
396 |
– |
|
495 |
|
Integer hold = s2.get(key[size-1]); |
496 |
|
s2.put(key[size-1], absent[0]); |
497 |
|
timer.start("Iter Equals ", size * 2); |
506 |
|
timer.finish(); |
507 |
|
|
508 |
|
s2.put(key[size-1], hold); |
509 |
< |
timer.start("Remove (iterator) ", size * 2); |
509 |
> |
timer.start("Remove (present) ", size * 2); |
510 |
|
Iterator s2i = s2.entrySet().iterator(); |
511 |
|
Set es = s.entrySet(); |
512 |
|
while (s2i.hasNext()) |
513 |
|
reallyAssert(es.remove(s2i.next())); |
514 |
|
timer.finish(); |
515 |
|
|
418 |
– |
if (!s.isEmpty()) System.out.println(s); |
516 |
|
reallyAssert (s.isEmpty()); |
517 |
|
|
518 |
|
timer.start("Clear ", size); |
521 |
|
reallyAssert (s2.isEmpty() && s.isEmpty()); |
522 |
|
} |
523 |
|
|
427 |
– |
static void stest(Map<Integer,Integer> s, int size) throws Exception { |
428 |
– |
if (!(s instanceof Serializable)) |
429 |
– |
return; |
430 |
– |
System.out.print("Serialize : "); |
431 |
– |
|
432 |
– |
for (int i = 0; i < size; i++) { |
433 |
– |
s.put(new Integer(i), new Integer(1)); |
434 |
– |
} |
435 |
– |
|
436 |
– |
long startTime = System.nanoTime(); |
437 |
– |
|
438 |
– |
FileOutputStream fs = new FileOutputStream("IntMapCheck.dat"); |
439 |
– |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(fs)); |
440 |
– |
out.writeObject(s); |
441 |
– |
out.close(); |
442 |
– |
|
443 |
– |
FileInputStream is = new FileInputStream("IntMapCheck.dat"); |
444 |
– |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(is)); |
445 |
– |
Map<Integer,Integer> m = (Map<Integer,Integer>)in.readObject(); |
446 |
– |
|
447 |
– |
long endTime = System.nanoTime(); |
448 |
– |
long time = endTime - startTime; |
449 |
– |
|
450 |
– |
System.out.print(time + "ms"); |
451 |
– |
|
452 |
– |
if (s instanceof IdentityHashMap) return; |
453 |
– |
reallyAssert (s.equals(m)); |
454 |
– |
} |
455 |
– |
|
456 |
– |
|
457 |
– |
static void test(Map<Integer,Integer> s, Integer[] key) { |
458 |
– |
int size = key.length; |
459 |
– |
|
460 |
– |
t3("Put (absent) ", size, s, key, size); |
461 |
– |
t3("Put (present) ", size, s, key, 0); |
462 |
– |
t7("ContainsKey ", size, s, key, absent); |
463 |
– |
t4("ContainsKey ", size, s, key, size); |
464 |
– |
ktest(s, size, key); |
465 |
– |
t4("ContainsKey ", absentSize, s, absent, 0); |
466 |
– |
t6("Get ", size, s, key, absent); |
467 |
– |
t1("Get (present) ", size, s, key, size); |
468 |
– |
t1("Get (absent) ", absentSize, s, absent, 0); |
469 |
– |
t2("Remove (absent) ", absentSize, s, absent, 0); |
470 |
– |
t5("Remove (present) ", size, s, key, size / 2); |
471 |
– |
t3("Put (half present) ", size, s, key, size / 2); |
472 |
– |
|
473 |
– |
ittest(s, size); |
474 |
– |
entest(s, size); |
475 |
– |
t9(s); |
476 |
– |
rtest(s, size); |
477 |
– |
|
478 |
– |
t4("ContainsKey ", size, s, key, 0); |
479 |
– |
t2("Remove (absent) ", size, s, key, 0); |
480 |
– |
t3("Put (presized) ", size, s, key, size); |
481 |
– |
dtest(s, size, key); |
482 |
– |
} |
483 |
– |
|
524 |
|
static class TestTimer { |
525 |
|
private String name; |
526 |
|
private long numOps; |
539 |
|
t = stats.sum / n; |
540 |
|
else |
541 |
|
t = stats.least; |
542 |
< |
long nano = Math.round(1000000.0 * t); |
542 |
> |
long nano = Math.round(t); |
543 |
|
System.out.println(e.getKey() + ": " + nano); |
544 |
|
} |
545 |
|
} |
568 |
|
void finish() { |
569 |
|
long endTime = System.nanoTime(); |
570 |
|
long time = endTime - startTime; |
571 |
< |
double timePerOp = (((double)time)/numOps) / 1000000; |
571 |
> |
double timePerOp = ((double)time)/numOps; |
572 |
|
|
573 |
|
Object st = accum.get(name); |
574 |
|
if (st == null) |
603 |
|
Stats(double t) { least = t; } |
604 |
|
} |
605 |
|
|
606 |
< |
static Random rng = new Random(); |
606 |
> |
static Random rng = new Random(3152688); |
607 |
|
|
608 |
|
static void shuffle(Integer[] keys) { |
609 |
|
int size = keys.length; |