From 61f4eb62a77c2a70679a96a81d5370388424832c Mon Sep 17 00:00:00 2001 From: Amitabh Tiwari Date: Mon, 23 Nov 2020 14:54:44 +0530 Subject: [PATCH] BAEL-4686: Performance test --- ...hMapVsSynchronizedMapPerformanceTest.java} | 41 +++++++------- .../ConcurrentModificationErrorTest.java | 6 +-- .../cuncurrenthashmap/PerformanceTest.java | 54 ------------------- .../map/cuncurrenthashmap/UserId.java | 17 ------ 4 files changed, 23 insertions(+), 95 deletions(-) rename core-java-modules/core-java-collections-maps-3/src/test/java/com/baeldung/map/{cuncurrenthashmap/ConcurrentHashMapVsSynchronizedMapTest.java => concurrenthashmap/ConcurrentHashMapVsSynchronizedMapPerformanceTest.java} (82%) rename core-java-modules/core-java-collections-maps-3/src/test/java/com/baeldung/map/{cuncurrenthashmap => concurrenthashmap}/ConcurrentModificationErrorTest.java (87%) delete mode 100644 core-java-modules/core-java-collections-maps-3/src/test/java/com/baeldung/map/cuncurrenthashmap/PerformanceTest.java delete mode 100644 core-java-modules/core-java-collections-maps-3/src/test/java/com/baeldung/map/cuncurrenthashmap/UserId.java diff --git a/core-java-modules/core-java-collections-maps-3/src/test/java/com/baeldung/map/cuncurrenthashmap/ConcurrentHashMapVsSynchronizedMapTest.java b/core-java-modules/core-java-collections-maps-3/src/test/java/com/baeldung/map/concurrenthashmap/ConcurrentHashMapVsSynchronizedMapPerformanceTest.java similarity index 82% rename from core-java-modules/core-java-collections-maps-3/src/test/java/com/baeldung/map/cuncurrenthashmap/ConcurrentHashMapVsSynchronizedMapTest.java rename to core-java-modules/core-java-collections-maps-3/src/test/java/com/baeldung/map/concurrenthashmap/ConcurrentHashMapVsSynchronizedMapPerformanceTest.java index 6a7716d38a..eb0c58a8b0 100644 --- a/core-java-modules/core-java-collections-maps-3/src/test/java/com/baeldung/map/cuncurrenthashmap/ConcurrentHashMapVsSynchronizedMapTest.java +++ b/core-java-modules/core-java-collections-maps-3/src/test/java/com/baeldung/map/concurrenthashmap/ConcurrentHashMapVsSynchronizedMapPerformanceTest.java @@ -1,4 +1,4 @@ -package com.baeldung.map.cuncurrenthashmap; +package com.baeldung.map.concurrenthashmap; import java.util.Collections; import java.util.HashMap; @@ -11,14 +11,14 @@ import java.util.concurrent.TimeUnit; import org.junit.Assert; import org.junit.Test; -public class ConcurrentHashMapVsSynchronizedMapTest { +public class ConcurrentHashMapVsSynchronizedMapPerformanceTest { public final static int THREAD_POOL_SIZE = 5; public final static int TEST_ITERATIONS = 5; public final static int TEST_NO_ITEMS = 500000; @Test - public void randomReadAndWritePerformaceTest_cuncurrentHashMap_faster() + public void randomReadAndWritePerformaceTest_ConcurrentHashMap_faster() throws InterruptedException { // For synchronizedMap Long totalTimeForSynchronizedMap = 0l; @@ -30,18 +30,18 @@ public class ConcurrentHashMapVsSynchronizedMapTest { Long avgTimeForSynchronizedMap = totalTimeForSynchronizedMap / TEST_ITERATIONS; // For ConcurrentHashMap Object - Long totalTimeForCuncurrentHashMap = 0l; + Long totalTimeForConcurrentHashMap = 0l; Map fasterMap = new ConcurrentHashMap<>(); for (int i = 0; i < TEST_ITERATIONS; i++) { - totalTimeForCuncurrentHashMap += performReadAndWriteTest(fasterMap); + totalTimeForConcurrentHashMap += performReadAndWriteTest(fasterMap); } - Long avgTimeForCuncurrentHashMap = totalTimeForCuncurrentHashMap / TEST_ITERATIONS; + Long avgTimeForConcurrentHashMap = totalTimeForConcurrentHashMap / TEST_ITERATIONS; - Assert.assertTrue(avgTimeForSynchronizedMap > avgTimeForCuncurrentHashMap); + Assert.assertTrue(avgTimeForSynchronizedMap > avgTimeForConcurrentHashMap); } @Test - public void randomWritePerformaceTest_cuncurrentHashMap_faster() throws InterruptedException { + public void randomWritePerformaceTest_ConcurrentHashMap_faster() throws InterruptedException { // For synchronizedMap Long totalTimeForSynchronizedMap = 0l; Map slowerMap = Collections @@ -52,18 +52,18 @@ public class ConcurrentHashMapVsSynchronizedMapTest { Long avgTimeForSynchronizedMap = totalTimeForSynchronizedMap / TEST_ITERATIONS; // For ConcurrentHashMap Object - Long totalTimeForCuncurrentHashMap = 0l; + Long totalTimeForConcurrentHashMap = 0l; Map fasterMap = new ConcurrentHashMap<>(); for (int i = 0; i < TEST_ITERATIONS; i++) { - totalTimeForCuncurrentHashMap += performWriteTest(fasterMap); + totalTimeForConcurrentHashMap += performWriteTest(fasterMap); } - Long avgTimeForCuncurrentHashMap = totalTimeForCuncurrentHashMap / TEST_ITERATIONS; + Long avgTimeForConcurrentHashMap = totalTimeForConcurrentHashMap / TEST_ITERATIONS; - Assert.assertTrue(avgTimeForSynchronizedMap > avgTimeForCuncurrentHashMap); + Assert.assertTrue(avgTimeForSynchronizedMap > avgTimeForConcurrentHashMap); } @Test - public void randomReadPerformaceTest_cuncurrentHashMap_faster() throws InterruptedException { + public void randomReadPerformaceTest_ConcurrentHashMap_faster() throws InterruptedException { Map slowerMap = Collections .synchronizedMap(addItems(new HashMap())); @@ -74,17 +74,16 @@ public class ConcurrentHashMapVsSynchronizedMapTest { } Long avgTimeForSynchronizedMap = totalTimeForSynchronizedMap / TEST_ITERATIONS; - Map fasterMap = Collections - .synchronizedMap(addItems(new ConcurrentHashMap())); + Map fasterMap = addItems(new ConcurrentHashMap()); // For ConcurrentHashMap Object - Long totalTimeForCuncurrentHashMap = 0l; + Long totalTimeForConcurrentHashMap = 0l; new ConcurrentHashMap<>(); for (int i = 0; i < TEST_ITERATIONS; i++) { - totalTimeForCuncurrentHashMap += performReadTest(fasterMap); + totalTimeForConcurrentHashMap += performReadTest(fasterMap); } - Long avgTimeForCuncurrentHashMap = totalTimeForCuncurrentHashMap / TEST_ITERATIONS; + Long avgTimeForConcurrentHashMap = totalTimeForConcurrentHashMap / TEST_ITERATIONS; - Assert.assertTrue(avgTimeForSynchronizedMap > avgTimeForCuncurrentHashMap); + Assert.assertTrue(avgTimeForSynchronizedMap > avgTimeForConcurrentHashMap); } private Map addItems(Map map) { @@ -125,7 +124,7 @@ public class ConcurrentHashMapVsSynchronizedMapTest { public void run() { for (int i = 0; i < TEST_NO_ITEMS; i++) { Integer randNumber = (int) Math.ceil(Math.random() * TEST_NO_ITEMS); - Integer value = map.get(String.valueOf(randNumber)); + map.get(String.valueOf(randNumber)); map.put(String.valueOf(randNumber), randNumber); } } @@ -146,7 +145,7 @@ public class ConcurrentHashMapVsSynchronizedMapTest { public void run() { for (int i = 0; i < TEST_NO_ITEMS; i++) { Integer randNumber = (int) Math.ceil(Math.random() * TEST_NO_ITEMS); - Integer value = map.get(String.valueOf(randNumber)); + map.get(String.valueOf(randNumber)); } } }); diff --git a/core-java-modules/core-java-collections-maps-3/src/test/java/com/baeldung/map/cuncurrenthashmap/ConcurrentModificationErrorTest.java b/core-java-modules/core-java-collections-maps-3/src/test/java/com/baeldung/map/concurrenthashmap/ConcurrentModificationErrorTest.java similarity index 87% rename from core-java-modules/core-java-collections-maps-3/src/test/java/com/baeldung/map/cuncurrenthashmap/ConcurrentModificationErrorTest.java rename to core-java-modules/core-java-collections-maps-3/src/test/java/com/baeldung/map/concurrenthashmap/ConcurrentModificationErrorTest.java index 993354d6d0..998496720e 100644 --- a/core-java-modules/core-java-collections-maps-3/src/test/java/com/baeldung/map/cuncurrenthashmap/ConcurrentModificationErrorTest.java +++ b/core-java-modules/core-java-collections-maps-3/src/test/java/com/baeldung/map/concurrenthashmap/ConcurrentModificationErrorTest.java @@ -1,4 +1,4 @@ -package com.baeldung.map.cuncurrenthashmap; +package com.baeldung.map.concurrenthashmap; import java.util.Collections; import java.util.ConcurrentModificationException; @@ -14,7 +14,7 @@ import org.junit.Test; public class ConcurrentModificationErrorTest { @Test(expected = ConcurrentModificationException.class) - public void whenRemoveAndAddOnHashMap_thenCuncurrentModificationError() { + public void whenRemoveAndAddOnHashMap_thenConcurrentModificationError() { Map map = new HashMap<>(); map.put(1, "baeldung"); map.put(2, "HashMap"); @@ -26,7 +26,7 @@ public class ConcurrentModificationErrorTest { } } - public void whenRemoveAndAddOnCuncurrentHashMap_thenNoError() { + public void whenRemoveAndAddOnConcurrentHashMap_thenNoError() { Map map = new ConcurrentHashMap<>(); map.put(1, "baeldung"); map.put(2, "HashMap"); diff --git a/core-java-modules/core-java-collections-maps-3/src/test/java/com/baeldung/map/cuncurrenthashmap/PerformanceTest.java b/core-java-modules/core-java-collections-maps-3/src/test/java/com/baeldung/map/cuncurrenthashmap/PerformanceTest.java deleted file mode 100644 index 9751decf53..0000000000 --- a/core-java-modules/core-java-collections-maps-3/src/test/java/com/baeldung/map/cuncurrenthashmap/PerformanceTest.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.baeldung.map.cuncurrenthashmap; - -import java.util.Collections; -import java.util.ConcurrentModificationException; -import java.util.HashMap; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -import org.junit.Test; - -public class PerformanceTest { - - @Test(expected = ConcurrentModificationException.class) - public void whenRemoveAndAddOnHashMap_thenCuncurrentModificationError() { - Map map = new HashMap<>(); - Map synchronizedMap = Collections.synchronizedMap(map); - long startTime = System.currentTimeMillis(); - for(int i=0; i<100000; i++) { - UserId userId = new UserId(1); - synchronizedMap.put(userId, userId.toString()); - } - long endTime = System.currentTimeMillis(); - long addTimeForSynchronized = endTime-startTime; - - startTime = System.currentTimeMillis(); - for(int i=0; i<100000; i++) { - UserId userId = new UserId(1); - synchronizedMap.get(userId); - } - endTime = System.currentTimeMillis(); - long fetchTimeForSynchronized = endTime-startTime; - - Map map1 = new ConcurrentHashMap<>(); - startTime = System.currentTimeMillis(); - for(int i=0; i<100000; i++) { - UserId userId = new UserId(1); - map1.put(userId, userId.toString()); - } - endTime = System.currentTimeMillis(); - long addTimeForConcurrent = endTime-startTime; - - startTime = System.currentTimeMillis(); - for(int i=0; i<100000; i++) { - UserId userId = new UserId(1); - map1.get(userId); - } - endTime = System.currentTimeMillis(); - long fetchTimeForConcurrent = endTime-startTime; - - System.out.println("ABC"); - - } - -} diff --git a/core-java-modules/core-java-collections-maps-3/src/test/java/com/baeldung/map/cuncurrenthashmap/UserId.java b/core-java-modules/core-java-collections-maps-3/src/test/java/com/baeldung/map/cuncurrenthashmap/UserId.java deleted file mode 100644 index a69976180d..0000000000 --- a/core-java-modules/core-java-collections-maps-3/src/test/java/com/baeldung/map/cuncurrenthashmap/UserId.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.baeldung.map.cuncurrenthashmap; - -public class UserId { - - private int id; - - public UserId(int id) { - super(); - this.id = id; - } - - @Override - public int hashCode() { - return this.id%10; - } - -}