BAEL-3894: Added Performance Tests (#10213)

This commit is contained in:
Sampada 2020-10-30 23:07:24 +05:30 committed by GitHub
parent bfcbaf886a
commit 964cde9dbc
2 changed files with 98 additions and 0 deletions

View File

@ -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<String, String> groceryCart = new ArrayListValuedHashMap<>();
private final static MultiKeyMap<String, String> days = new MultiKeyMap<String, String>();
private final static MultiKeyMap<String, String> cityCoordinates = new MultiKeyMap<String, String>();
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<Integer, Integer> 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<Integer, Integer> 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<Integer, Integer> 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));
}
}

View File

@ -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<String, String> groceryCart = ArrayListMultimap.create();
private final static Table<String, String, String> cityCoordinates = HashBasedTable.create();
private final static Table<String, String, String> 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<Integer, Integer> 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));
}
}