From bd6bc03c93a926c357ee22006a75082d606c4731 Mon Sep 17 00:00:00 2001 From: Amitabh Tiwari Date: Mon, 23 Nov 2020 17:32:18 +0530 Subject: [PATCH] BAEL-4686: Corrected the test --- ...shMapVsSynchronizedMapPerformanceTest.java | 86 +++++++++---------- 1 file changed, 43 insertions(+), 43 deletions(-) diff --git a/core-java-modules/core-java-collections-maps-3/src/test/java/com/baeldung/map/concurrenthashmap/ConcurrentHashMapVsSynchronizedMapPerformanceTest.java b/core-java-modules/core-java-collections-maps-3/src/test/java/com/baeldung/map/concurrenthashmap/ConcurrentHashMapVsSynchronizedMapPerformanceTest.java index eb0c58a8b0..573087d5a5 100644 --- a/core-java-modules/core-java-collections-maps-3/src/test/java/com/baeldung/map/concurrenthashmap/ConcurrentHashMapVsSynchronizedMapPerformanceTest.java +++ b/core-java-modules/core-java-collections-maps-3/src/test/java/com/baeldung/map/concurrenthashmap/ConcurrentHashMapVsSynchronizedMapPerformanceTest.java @@ -15,7 +15,7 @@ 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; + public final static int TEST_NO_ITEMS = 10000; @Test public void randomReadAndWritePerformaceTest_ConcurrentHashMap_faster() @@ -40,6 +40,28 @@ public class ConcurrentHashMapVsSynchronizedMapPerformanceTest { Assert.assertTrue(avgTimeForSynchronizedMap > avgTimeForConcurrentHashMap); } + private long performReadAndWriteTest(final Map map) + throws InterruptedException { + long startTime = System.nanoTime(); + ExecutorService exectures = Executors.newFixedThreadPool(THREAD_POOL_SIZE); + for (int j = 0; j < THREAD_POOL_SIZE; j++) { + exectures.execute(new Runnable() { + @Override + public void run() { + for (int i = 0; i < TEST_NO_ITEMS; i++) { + Integer randNumber = (int) Math.ceil(Math.random() * TEST_NO_ITEMS); + map.get(String.valueOf(randNumber)); + map.put(String.valueOf(randNumber), randNumber); + } + } + }); + } + exectures.shutdown(); + exectures.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS); + long entTime = System.nanoTime(); + return (entTime - startTime) / 1000000L; + } + @Test public void randomWritePerformaceTest_ConcurrentHashMap_faster() throws InterruptedException { // For synchronizedMap @@ -62,6 +84,26 @@ public class ConcurrentHashMapVsSynchronizedMapPerformanceTest { Assert.assertTrue(avgTimeForSynchronizedMap > avgTimeForConcurrentHashMap); } + private long performWriteTest(final Map map) throws InterruptedException { + long startTime = System.nanoTime(); + ExecutorService exectures = Executors.newFixedThreadPool(THREAD_POOL_SIZE); + for (int j = 0; j < THREAD_POOL_SIZE; j++) { + exectures.execute(new Runnable() { + @Override + public void run() { + for (int i = 0; i < TEST_NO_ITEMS; i++) { + Integer randNumber = (int) Math.ceil(Math.random() * TEST_NO_ITEMS); + map.put(String.valueOf(randNumber), randNumber); + } + } + }); + } + exectures.shutdown(); + exectures.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS); + long entTime = System.nanoTime(); + return (entTime - startTime) / 1000000L; + } + @Test public void randomReadPerformaceTest_ConcurrentHashMap_faster() throws InterruptedException { @@ -94,48 +136,6 @@ public class ConcurrentHashMapVsSynchronizedMapPerformanceTest { return map; } - private long performWriteTest(final Map map) throws InterruptedException { - long startTime = System.nanoTime(); - ExecutorService exectures = Executors.newFixedThreadPool(THREAD_POOL_SIZE); - for (int j = 0; j < THREAD_POOL_SIZE; j++) { - exectures.execute(new Runnable() { - @Override - public void run() { - for (int i = 0; i < TEST_NO_ITEMS; i++) { - Integer randNumber = (int) Math.ceil(Math.random() * TEST_NO_ITEMS); - map.put(String.valueOf(randNumber), randNumber); - } - } - }); - } - exectures.shutdown(); - exectures.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS); - long entTime = System.nanoTime(); - return (entTime - startTime) / 1000000L; - } - - private long performReadAndWriteTest(final Map map) - throws InterruptedException { - long startTime = System.nanoTime(); - ExecutorService exectures = Executors.newFixedThreadPool(THREAD_POOL_SIZE); - for (int j = 0; j < THREAD_POOL_SIZE; j++) { - exectures.execute(new Runnable() { - @Override - public void run() { - for (int i = 0; i < TEST_NO_ITEMS; i++) { - Integer randNumber = (int) Math.ceil(Math.random() * TEST_NO_ITEMS); - map.get(String.valueOf(randNumber)); - map.put(String.valueOf(randNumber), randNumber); - } - } - }); - } - exectures.shutdown(); - exectures.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS); - long entTime = System.nanoTime(); - return (entTime - startTime) / 1000000L; - } - private long performReadTest(final Map map) throws InterruptedException { long startTime = System.nanoTime(); ExecutorService exectures = Executors.newFixedThreadPool(THREAD_POOL_SIZE);