fix ConcurrentMapTests failed test (#1070)

* add concurrentmap tests

* fix ConcurrentMapTests#givenConcurrentMap_whenKeyWithSameHashCode_thenPerformanceDegrades
This commit is contained in:
Tian Baoqiang 2017-01-29 21:32:10 +08:00 committed by Grzegorz Piwowarek
parent b852e2edcc
commit c565173601
1 changed files with 3 additions and 4 deletions

View File

@ -9,7 +9,6 @@ import java.util.Map;
import java.util.concurrent.*; import java.util.concurrent.*;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
public class ConcurrentMapPerformanceTest { public class ConcurrentMapPerformanceTest {
@ -24,11 +23,11 @@ public class ConcurrentMapPerformanceTest {
long syncHashMapAvgRuntime = timeElapseForGetPut(synchronizedHashMap); long syncHashMapAvgRuntime = timeElapseForGetPut(synchronizedHashMap);
long concurrentHashMapAvgRuntime = timeElapseForGetPut(concurrentHashMap); long concurrentHashMapAvgRuntime = timeElapseForGetPut(concurrentHashMap);
System.out.println(String.format("Hashtable: %s, syncHashMap: %s, ConcurrentHashMap: %s", hashtableAvgRuntime, syncHashMapAvgRuntime, concurrentHashMapAvgRuntime));
assertTrue(hashtableAvgRuntime > concurrentHashMapAvgRuntime); assertTrue(hashtableAvgRuntime > concurrentHashMapAvgRuntime);
assertTrue(syncHashMapAvgRuntime > concurrentHashMapAvgRuntime); assertTrue(syncHashMapAvgRuntime > concurrentHashMapAvgRuntime);
System.out.println(String.format("Hashtable: %s, syncHashMap: %s, ConcurrentHashMap: %s", hashtableAvgRuntime, syncHashMapAvgRuntime, concurrentHashMapAvgRuntime));
} }
private long timeElapseForGetPut(Map<String, Object> map) throws InterruptedException { private long timeElapseForGetPut(Map<String, Object> map) throws InterruptedException {
@ -90,7 +89,7 @@ public class ConcurrentMapPerformanceTest {
long mapOfDefaultHashDuration = System.currentTimeMillis() - defaultHashStartTime; long mapOfDefaultHashDuration = System.currentTimeMillis() - defaultHashStartTime;
assertEquals(executeTimes * 2, mapOfDefaultHash.size()); assertEquals(executeTimes * 2, mapOfDefaultHash.size());
assertNotEquals(executeTimes * 2, mapOfSameHash.size()); assertEquals(executeTimes * 2, mapOfSameHash.size());
System.out.println(String.format("same-hash: %s, default-hash: %s", mapOfSameHashDuration, mapOfDefaultHashDuration)); System.out.println(String.format("same-hash: %s, default-hash: %s", mapOfSameHashDuration, mapOfDefaultHashDuration));
assertTrue("same hashCode() should greatly degrade performance", mapOfSameHashDuration > mapOfDefaultHashDuration * 10); assertTrue("same hashCode() should greatly degrade performance", mapOfSameHashDuration > mapOfDefaultHashDuration * 10);
} }