BAEL-4686: Performance test

This commit is contained in:
Amitabh Tiwari 2020-11-23 14:54:44 +05:30
parent aaac4e6a95
commit 61f4eb62a7
4 changed files with 23 additions and 95 deletions

View File

@ -1,4 +1,4 @@
package com.baeldung.map.cuncurrenthashmap; package com.baeldung.map.concurrenthashmap;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
@ -11,14 +11,14 @@ import java.util.concurrent.TimeUnit;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
public class ConcurrentHashMapVsSynchronizedMapTest { public class ConcurrentHashMapVsSynchronizedMapPerformanceTest {
public final static int THREAD_POOL_SIZE = 5; public final static int THREAD_POOL_SIZE = 5;
public final static int TEST_ITERATIONS = 5; public final static int TEST_ITERATIONS = 5;
public final static int TEST_NO_ITEMS = 500000; public final static int TEST_NO_ITEMS = 500000;
@Test @Test
public void randomReadAndWritePerformaceTest_cuncurrentHashMap_faster() public void randomReadAndWritePerformaceTest_ConcurrentHashMap_faster()
throws InterruptedException { throws InterruptedException {
// For synchronizedMap // For synchronizedMap
Long totalTimeForSynchronizedMap = 0l; Long totalTimeForSynchronizedMap = 0l;
@ -30,18 +30,18 @@ public class ConcurrentHashMapVsSynchronizedMapTest {
Long avgTimeForSynchronizedMap = totalTimeForSynchronizedMap / TEST_ITERATIONS; Long avgTimeForSynchronizedMap = totalTimeForSynchronizedMap / TEST_ITERATIONS;
// For ConcurrentHashMap Object // For ConcurrentHashMap Object
Long totalTimeForCuncurrentHashMap = 0l; Long totalTimeForConcurrentHashMap = 0l;
Map<String, Integer> fasterMap = new ConcurrentHashMap<>(); Map<String, Integer> fasterMap = new ConcurrentHashMap<>();
for (int i = 0; i < TEST_ITERATIONS; i++) { 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 @Test
public void randomWritePerformaceTest_cuncurrentHashMap_faster() throws InterruptedException { public void randomWritePerformaceTest_ConcurrentHashMap_faster() throws InterruptedException {
// For synchronizedMap // For synchronizedMap
Long totalTimeForSynchronizedMap = 0l; Long totalTimeForSynchronizedMap = 0l;
Map<String, Integer> slowerMap = Collections Map<String, Integer> slowerMap = Collections
@ -52,18 +52,18 @@ public class ConcurrentHashMapVsSynchronizedMapTest {
Long avgTimeForSynchronizedMap = totalTimeForSynchronizedMap / TEST_ITERATIONS; Long avgTimeForSynchronizedMap = totalTimeForSynchronizedMap / TEST_ITERATIONS;
// For ConcurrentHashMap Object // For ConcurrentHashMap Object
Long totalTimeForCuncurrentHashMap = 0l; Long totalTimeForConcurrentHashMap = 0l;
Map<String, Integer> fasterMap = new ConcurrentHashMap<>(); Map<String, Integer> fasterMap = new ConcurrentHashMap<>();
for (int i = 0; i < TEST_ITERATIONS; i++) { 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 @Test
public void randomReadPerformaceTest_cuncurrentHashMap_faster() throws InterruptedException { public void randomReadPerformaceTest_ConcurrentHashMap_faster() throws InterruptedException {
Map<String, Integer> slowerMap = Collections Map<String, Integer> slowerMap = Collections
.synchronizedMap(addItems(new HashMap<String, Integer>())); .synchronizedMap(addItems(new HashMap<String, Integer>()));
@ -74,17 +74,16 @@ public class ConcurrentHashMapVsSynchronizedMapTest {
} }
Long avgTimeForSynchronizedMap = totalTimeForSynchronizedMap / TEST_ITERATIONS; Long avgTimeForSynchronizedMap = totalTimeForSynchronizedMap / TEST_ITERATIONS;
Map<String, Integer> fasterMap = Collections Map<String, Integer> fasterMap = addItems(new ConcurrentHashMap<String, Integer>());
.synchronizedMap(addItems(new ConcurrentHashMap<String, Integer>()));
// For ConcurrentHashMap Object // For ConcurrentHashMap Object
Long totalTimeForCuncurrentHashMap = 0l; Long totalTimeForConcurrentHashMap = 0l;
new ConcurrentHashMap<>(); new ConcurrentHashMap<>();
for (int i = 0; i < TEST_ITERATIONS; i++) { 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<String, Integer> addItems(Map<String, Integer> map) { private Map<String, Integer> addItems(Map<String, Integer> map) {
@ -125,7 +124,7 @@ public class ConcurrentHashMapVsSynchronizedMapTest {
public void run() { public void run() {
for (int i = 0; i < TEST_NO_ITEMS; i++) { for (int i = 0; i < TEST_NO_ITEMS; i++) {
Integer randNumber = (int) Math.ceil(Math.random() * TEST_NO_ITEMS); 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); map.put(String.valueOf(randNumber), randNumber);
} }
} }
@ -146,7 +145,7 @@ public class ConcurrentHashMapVsSynchronizedMapTest {
public void run() { public void run() {
for (int i = 0; i < TEST_NO_ITEMS; i++) { for (int i = 0; i < TEST_NO_ITEMS; i++) {
Integer randNumber = (int) Math.ceil(Math.random() * TEST_NO_ITEMS); Integer randNumber = (int) Math.ceil(Math.random() * TEST_NO_ITEMS);
Integer value = map.get(String.valueOf(randNumber)); map.get(String.valueOf(randNumber));
} }
} }
}); });

View File

@ -1,4 +1,4 @@
package com.baeldung.map.cuncurrenthashmap; package com.baeldung.map.concurrenthashmap;
import java.util.Collections; import java.util.Collections;
import java.util.ConcurrentModificationException; import java.util.ConcurrentModificationException;
@ -14,7 +14,7 @@ import org.junit.Test;
public class ConcurrentModificationErrorTest { public class ConcurrentModificationErrorTest {
@Test(expected = ConcurrentModificationException.class) @Test(expected = ConcurrentModificationException.class)
public void whenRemoveAndAddOnHashMap_thenCuncurrentModificationError() { public void whenRemoveAndAddOnHashMap_thenConcurrentModificationError() {
Map<Integer, String> map = new HashMap<>(); Map<Integer, String> map = new HashMap<>();
map.put(1, "baeldung"); map.put(1, "baeldung");
map.put(2, "HashMap"); map.put(2, "HashMap");
@ -26,7 +26,7 @@ public class ConcurrentModificationErrorTest {
} }
} }
public void whenRemoveAndAddOnCuncurrentHashMap_thenNoError() { public void whenRemoveAndAddOnConcurrentHashMap_thenNoError() {
Map<Integer, String> map = new ConcurrentHashMap<>(); Map<Integer, String> map = new ConcurrentHashMap<>();
map.put(1, "baeldung"); map.put(1, "baeldung");
map.put(2, "HashMap"); map.put(2, "HashMap");

View File

@ -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<UserId, String> map = new HashMap<>();
Map<UserId, String> 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<UserId, String> 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");
}
}

View File

@ -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;
}
}