From 964cde9dbc40517e37638098ba08a144d83f264e Mon Sep 17 00:00:00 2001 From: Sampada <46674082+sampada07@users.noreply.github.com> Date: Fri, 30 Oct 2020 23:07:24 +0530 Subject: [PATCH] BAEL-3894: Added Performance Tests (#10213) --- .../apache/commons/CollectionsUnitTest.java | 73 +++++++++++++++++++ .../com/baeldung/guava/GuavaUnitTest.java | 25 +++++++ 2 files changed, 98 insertions(+) diff --git a/libraries-6/src/test/java/com/baeldung/apache/commons/CollectionsUnitTest.java b/libraries-6/src/test/java/com/baeldung/apache/commons/CollectionsUnitTest.java index aa5e09b443..3de0c64fb9 100644 --- a/libraries-6/src/test/java/com/baeldung/apache/commons/CollectionsUnitTest.java +++ b/libraries-6/src/test/java/com/baeldung/apache/commons/CollectionsUnitTest.java @@ -7,9 +7,12 @@ import static org.junit.Assert.assertFalse; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.concurrent.TimeUnit; import org.apache.commons.collections4.BidiMap; import org.apache.commons.collections4.MultiValuedMap; +import org.apache.commons.collections4.bidimap.DualHashBidiMap; +import org.apache.commons.collections4.bidimap.DualTreeBidiMap; import org.apache.commons.collections4.bidimap.TreeBidiMap; import org.apache.commons.collections4.map.MultiKeyMap; import org.apache.commons.collections4.multimap.ArrayListValuedHashMap; @@ -20,6 +23,7 @@ public class CollectionsUnitTest { private final static MultiValuedMap groceryCart = new ArrayListValuedHashMap<>(); private final static MultiKeyMap days = new MultiKeyMap(); private final static MultiKeyMap cityCoordinates = new MultiKeyMap(); + private long start; static { daysOfWeek.put(1, "Monday"); @@ -106,4 +110,73 @@ public class CollectionsUnitTest { } + @Test + public void givenTreeBidiMap_whenHundredThousandKeys_thenPerformanceNoted() { + System.out.println("**TreeBidiMap**"); + BidiMap map = new TreeBidiMap<>(); + start = System.nanoTime(); + for (int i = 0; i < 100000; i++) { + Integer key = new Integer(i); + Integer value = new Integer(i + 1); + map.put(key, value); + } + System.out.println("Insertion time:" + TimeUnit.MILLISECONDS.convert(System.nanoTime() - start, TimeUnit.NANOSECONDS)); + + start = System.nanoTime(); + Integer value = (Integer) map.get(new Integer(500)); + System.out.println("Value:" + value); + System.out.println("Fetch time key:" + TimeUnit.MICROSECONDS.convert(System.nanoTime() - start, TimeUnit.NANOSECONDS)); + + start = System.nanoTime(); + Integer key = (Integer) map.getKey(new Integer(501)); + System.out.println("Key:" + key); + System.out.println("Fetch time value:" + TimeUnit.MICROSECONDS.convert(System.nanoTime() - start, TimeUnit.NANOSECONDS)); + } + + @Test + public void givenDualTreeBidiMap_whenHundredThousandKeys_thenPerformanceNoted() { + System.out.println("**DualTreeBidiMap**"); + BidiMap map = new DualTreeBidiMap<>(); + start = System.nanoTime(); + for (int i = 0; i < 100000; i++) { + Integer key = new Integer(i); + Integer value = new Integer(i + 1); + map.put(key, value); + } + System.out.println("Insertion time:" + TimeUnit.MILLISECONDS.convert(System.nanoTime() - start, TimeUnit.NANOSECONDS)); + + start = System.nanoTime(); + Integer value = (Integer) map.get(new Integer(500)); + System.out.println("Value:" + value); + System.out.println("Fetch time key:" + TimeUnit.MICROSECONDS.convert(System.nanoTime() - start, TimeUnit.NANOSECONDS)); + + start = System.nanoTime(); + Integer key = (Integer) map.getKey(new Integer(501)); + System.out.println("Key:" + key); + System.out.println("Fetch time value:" + TimeUnit.MICROSECONDS.convert(System.nanoTime() - start, TimeUnit.NANOSECONDS)); + } + + @Test + public void givenDualHashBidiMap_whenHundredThousandKeys_thenPerformanceNoted() { + System.out.println("**DualHashBidiMap**"); + BidiMap map = new DualHashBidiMap<>(); + start = System.nanoTime(); + for (int i = 0; i < 100000; i++) { + Integer key = new Integer(i); + Integer value = new Integer(i + 1); + map.put(key, value); + } + System.out.println("Insertion time:" + TimeUnit.MILLISECONDS.convert(System.nanoTime() - start, TimeUnit.NANOSECONDS)); + + start = System.nanoTime(); + Integer value = (Integer) map.get(new Integer(500)); + System.out.println("Value:" + value); + System.out.println("Fetch time key:" + TimeUnit.MICROSECONDS.convert(System.nanoTime() - start, TimeUnit.NANOSECONDS)); + + start = System.nanoTime(); + Integer key = (Integer) map.getKey(new Integer(501)); + System.out.println("Key:" + key); + System.out.println("Fetch time value:" + TimeUnit.MICROSECONDS.convert(System.nanoTime() - start, TimeUnit.NANOSECONDS)); + } + } \ No newline at end of file diff --git a/libraries-6/src/test/java/com/baeldung/guava/GuavaUnitTest.java b/libraries-6/src/test/java/com/baeldung/guava/GuavaUnitTest.java index 13a676e709..c0501f761e 100644 --- a/libraries-6/src/test/java/com/baeldung/guava/GuavaUnitTest.java +++ b/libraries-6/src/test/java/com/baeldung/guava/GuavaUnitTest.java @@ -6,6 +6,7 @@ import static org.junit.Assert.assertTrue; import java.util.Arrays; import java.util.List; +import java.util.concurrent.TimeUnit; import org.junit.Test; @@ -21,6 +22,7 @@ public class GuavaUnitTest { private final static Multimap groceryCart = ArrayListMultimap.create(); private final static Table cityCoordinates = HashBasedTable.create(); private final static Table movies = HashBasedTable.create(); + private long start; static { daysOfWeek.put(1, "Monday"); @@ -115,4 +117,27 @@ public class GuavaUnitTest { assertTrue(movies.containsValue("Speed")); } + + @Test + public void givenHashBiMap_whenHundredThousandKeys_thenPerformanceNoted() { + BiMap map = HashBiMap.create(); + start = System.nanoTime(); + for (int i = 0; i < 100000; i++) { + Integer key = new Integer(i); + Integer value = new Integer(i + 1); + map.put(key, value); + } + System.out.println("Insertion time:" + TimeUnit.MILLISECONDS.convert(System.nanoTime() - start, TimeUnit.NANOSECONDS)); + + start = System.nanoTime(); + Integer value = map.get(new Integer(500)); + System.out.println("Value:" + value); + System.out.println("Fetch time key:" + TimeUnit.MICROSECONDS.convert(System.nanoTime() - start, TimeUnit.NANOSECONDS)); + + start = System.nanoTime(); + Integer key = map.inverse() + .get(new Integer(501)); + System.out.println("Key:" + key); + System.out.println("Fetch time value:" + TimeUnit.MICROSECONDS.convert(System.nanoTime() - start, TimeUnit.NANOSECONDS)); + } } \ No newline at end of file