/ecat-checklistbank/src/test/java/org/gbif/manualtests/StringKeyedMemoryTest.java
Java | 220 lines | 175 code | 18 blank | 27 comment | 35 complexity | 79456b450973833404a900b515426a97 MD5 | raw file
- /**
- *
- */
- package org.gbif.manualtests;
- import org.gbif.checklistbank.cache.StringIndex;
- import org.gbif.utils.collection.CompactHashSet;
- import java.io.IOException;
- import java.io.UnsupportedEncodingException;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.Map;
- import java.util.Set;
- import java.util.TreeMap;
- import java.util.Vector;
- import gnu.trove.TByteArrayList;
- import gnu.trove.TObjectIntHashMap;
- import org.apache.commons.lang3.ArrayUtils;
- /**
- * A test class to determine how much memory is used for Strings versus various int primitive sets
- *
- * @author markus
- */
- public class StringKeyedMemoryTest {
- public static void main(String[] args) throws IOException {
- // testHashMap();
- // testTreeMap();
- // testTStringIntHashMap();
- // testTByteArrayIntHashMap();
- // testCompactHashSet();
- testHashSet();
- testStringIndex();
- testArrayList();
- testArray();
- testVector();
- testLinkedList();
- testByteArrayList();
- }
- // 640.000
- public static void testArray() {
- int batchSize = 10000;
- String[] cache = new String[0];
- int i = 0;
- while (true) {
- int x = 0;
- String[] batch = new String[batchSize];
- while (x < batchSize) {
- batch[x] = "scientificName " + (i + x);
- x++;
- }
- cache = (String[]) ArrayUtils.addAll(cache, batch);
- i += x;
- System.out.println((i) + " added... ");
- }
- }
- // 700.000
- public static void testArrayList() {
- ArrayList<String> cache = new ArrayList<String>(10000);
- int i = 0;
- while (true) {
- i++;
- cache.add("scientificName " + i);
- if ((i + 1) % 10000 == 0) {
- System.out.println((i + 1) + " added...");
- }
- }
- }
- // 1.500.000
- public static void testByteArrayList() {
- List cache = new ArrayList();
- int i = 0;
- try {
- while (true) {
- cache.add(("scientificName " + i).getBytes("utf-8"));
- if ((i + 1) % 10000 == 0) {
- System.out.println((i + 1) + " added...");
- }
- i++;
- }
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- }
- // 670.000
- public static void testCompactHashSet() {
- Set<String> cache = new CompactHashSet<String>(10000);
- int i = 0;
- while (true) {
- i++;
- cache.add("scientificName " + i);
- if ((i + 1) % 10000 == 0) {
- System.out.println((i + 1) + " added...");
- }
- }
- }
- // 390.000
- public static void testHashMap() {
- Map<String, Integer> cache = new HashMap<String, Integer>();
- int i = 0;
- while (true) {
- i++;
- cache.put("scientificName " + i, i / 2);
- if ((i + 1) % 10000 == 0) {
- System.out.println((i + 1) + " added...");
- }
- }
- }
- // 480.000
- public static void testHashSet() {
- Set<String> cache = new HashSet<String>(10000);
- int i = 0;
- while (true) {
- i++;
- cache.add("scientificName " + i);
- if ((i + 1) % 10000 == 0) {
- System.out.println((i + 1) + " added...");
- }
- }
- }
- // 560.000
- public static void testLinkedList() {
- List<String> cache = new LinkedList<String>();
- int i = 0;
- while (true) {
- i++;
- cache.add("scientificName " + i);
- if ((i + 1) % 10000 == 0) {
- System.out.println((i + 1) + " added...");
- }
- }
- }
- // 610.000
- public static void testStringIndex() {
- StringIndex<String> cache = new StringIndex<String>();
- int i = 0;
- while (true) {
- i++;
- cache.add(i + " scientificName");
- if ((i + 1) % 10000 == 0) {
- System.out.println((i + 1) + " added...");
- // this triggers cache updating to be fair when comparing memory footprint ;)
- cache.getFirst(i + " scientificName");
- System.out.println((i + 1) + " and updated index");
- }
- }
- }
- // SUPER SLOW !!!
- // > 800.000 (stopped as it was soooo slow)
- public static void testTByteArrayIntHashMap() {
- TObjectIntHashMap<TByteArrayList> cache = new TObjectIntHashMap<TByteArrayList>();
- int i = 0;
- try {
- while (true) {
- i++;
- cache.put(new TByteArrayList(("scientificName " + i).getBytes("utf-8")), i / 2);
- if ((i + 1) % 10000 == 0) {
- System.out.println((i + 1) + " added...");
- }
- }
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- }
- // 420.000
- public static void testTreeMap() {
- Map<String, Integer> cache = new TreeMap<String, Integer>();
- int i = 0;
- while (true) {
- i++;
- cache.put("scientificName " + i, i / 2);
- if ((i + 1) % 10000 == 0) {
- System.out.println((i + 1) + " added...");
- }
- }
- }
- // 580.000
- public static void testTStringIntHashMap() {
- TObjectIntHashMap<String> cache = new TObjectIntHashMap<String>();
- int i = 0;
- while (true) {
- i++;
- cache.put("scientificName " + i, i / 2);
- if ((i + 1) % 10000 == 0) {
- System.out.println((i + 1) + " added...");
- }
- }
- }
- // 640.000
- public static void testVector() {
- Vector<String> cache = new Vector<String>(10000);
- int i = 0;
- while (true) {
- i++;
- cache.add("scientificName " + i);
- if ((i + 1) % 10000 == 0) {
- System.out.println((i + 1) + " added...");
- }
- }
- }
- }