PageRenderTime 38ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/ecat-checklistbank/src/test/java/org/gbif/manualtests/StringKeyedMemoryTest.java

http://gbif-ecat.googlecode.com/
Java | 220 lines | 175 code | 18 blank | 27 comment | 35 complexity | 79456b450973833404a900b515426a97 MD5 | raw file
  1. /**
  2. *
  3. */
  4. package org.gbif.manualtests;
  5. import org.gbif.checklistbank.cache.StringIndex;
  6. import org.gbif.utils.collection.CompactHashSet;
  7. import java.io.IOException;
  8. import java.io.UnsupportedEncodingException;
  9. import java.util.ArrayList;
  10. import java.util.HashMap;
  11. import java.util.HashSet;
  12. import java.util.LinkedList;
  13. import java.util.List;
  14. import java.util.Map;
  15. import java.util.Set;
  16. import java.util.TreeMap;
  17. import java.util.Vector;
  18. import gnu.trove.TByteArrayList;
  19. import gnu.trove.TObjectIntHashMap;
  20. import org.apache.commons.lang3.ArrayUtils;
  21. /**
  22. * A test class to determine how much memory is used for Strings versus various int primitive sets
  23. *
  24. * @author markus
  25. */
  26. public class StringKeyedMemoryTest {
  27. public static void main(String[] args) throws IOException {
  28. // testHashMap();
  29. // testTreeMap();
  30. // testTStringIntHashMap();
  31. // testTByteArrayIntHashMap();
  32. // testCompactHashSet();
  33. testHashSet();
  34. testStringIndex();
  35. testArrayList();
  36. testArray();
  37. testVector();
  38. testLinkedList();
  39. testByteArrayList();
  40. }
  41. // 640.000
  42. public static void testArray() {
  43. int batchSize = 10000;
  44. String[] cache = new String[0];
  45. int i = 0;
  46. while (true) {
  47. int x = 0;
  48. String[] batch = new String[batchSize];
  49. while (x < batchSize) {
  50. batch[x] = "scientificName " + (i + x);
  51. x++;
  52. }
  53. cache = (String[]) ArrayUtils.addAll(cache, batch);
  54. i += x;
  55. System.out.println((i) + " added... ");
  56. }
  57. }
  58. // 700.000
  59. public static void testArrayList() {
  60. ArrayList<String> cache = new ArrayList<String>(10000);
  61. int i = 0;
  62. while (true) {
  63. i++;
  64. cache.add("scientificName " + i);
  65. if ((i + 1) % 10000 == 0) {
  66. System.out.println((i + 1) + " added...");
  67. }
  68. }
  69. }
  70. // 1.500.000
  71. public static void testByteArrayList() {
  72. List cache = new ArrayList();
  73. int i = 0;
  74. try {
  75. while (true) {
  76. cache.add(("scientificName " + i).getBytes("utf-8"));
  77. if ((i + 1) % 10000 == 0) {
  78. System.out.println((i + 1) + " added...");
  79. }
  80. i++;
  81. }
  82. } catch (UnsupportedEncodingException e) {
  83. e.printStackTrace();
  84. }
  85. }
  86. // 670.000
  87. public static void testCompactHashSet() {
  88. Set<String> cache = new CompactHashSet<String>(10000);
  89. int i = 0;
  90. while (true) {
  91. i++;
  92. cache.add("scientificName " + i);
  93. if ((i + 1) % 10000 == 0) {
  94. System.out.println((i + 1) + " added...");
  95. }
  96. }
  97. }
  98. // 390.000
  99. public static void testHashMap() {
  100. Map<String, Integer> cache = new HashMap<String, Integer>();
  101. int i = 0;
  102. while (true) {
  103. i++;
  104. cache.put("scientificName " + i, i / 2);
  105. if ((i + 1) % 10000 == 0) {
  106. System.out.println((i + 1) + " added...");
  107. }
  108. }
  109. }
  110. // 480.000
  111. public static void testHashSet() {
  112. Set<String> cache = new HashSet<String>(10000);
  113. int i = 0;
  114. while (true) {
  115. i++;
  116. cache.add("scientificName " + i);
  117. if ((i + 1) % 10000 == 0) {
  118. System.out.println((i + 1) + " added...");
  119. }
  120. }
  121. }
  122. // 560.000
  123. public static void testLinkedList() {
  124. List<String> cache = new LinkedList<String>();
  125. int i = 0;
  126. while (true) {
  127. i++;
  128. cache.add("scientificName " + i);
  129. if ((i + 1) % 10000 == 0) {
  130. System.out.println((i + 1) + " added...");
  131. }
  132. }
  133. }
  134. // 610.000
  135. public static void testStringIndex() {
  136. StringIndex<String> cache = new StringIndex<String>();
  137. int i = 0;
  138. while (true) {
  139. i++;
  140. cache.add(i + " scientificName");
  141. if ((i + 1) % 10000 == 0) {
  142. System.out.println((i + 1) + " added...");
  143. // this triggers cache updating to be fair when comparing memory footprint ;)
  144. cache.getFirst(i + " scientificName");
  145. System.out.println((i + 1) + " and updated index");
  146. }
  147. }
  148. }
  149. // SUPER SLOW !!!
  150. // > 800.000 (stopped as it was soooo slow)
  151. public static void testTByteArrayIntHashMap() {
  152. TObjectIntHashMap<TByteArrayList> cache = new TObjectIntHashMap<TByteArrayList>();
  153. int i = 0;
  154. try {
  155. while (true) {
  156. i++;
  157. cache.put(new TByteArrayList(("scientificName " + i).getBytes("utf-8")), i / 2);
  158. if ((i + 1) % 10000 == 0) {
  159. System.out.println((i + 1) + " added...");
  160. }
  161. }
  162. } catch (UnsupportedEncodingException e) {
  163. e.printStackTrace();
  164. }
  165. }
  166. // 420.000
  167. public static void testTreeMap() {
  168. Map<String, Integer> cache = new TreeMap<String, Integer>();
  169. int i = 0;
  170. while (true) {
  171. i++;
  172. cache.put("scientificName " + i, i / 2);
  173. if ((i + 1) % 10000 == 0) {
  174. System.out.println((i + 1) + " added...");
  175. }
  176. }
  177. }
  178. // 580.000
  179. public static void testTStringIntHashMap() {
  180. TObjectIntHashMap<String> cache = new TObjectIntHashMap<String>();
  181. int i = 0;
  182. while (true) {
  183. i++;
  184. cache.put("scientificName " + i, i / 2);
  185. if ((i + 1) % 10000 == 0) {
  186. System.out.println((i + 1) + " added...");
  187. }
  188. }
  189. }
  190. // 640.000
  191. public static void testVector() {
  192. Vector<String> cache = new Vector<String>(10000);
  193. int i = 0;
  194. while (true) {
  195. i++;
  196. cache.add("scientificName " + i);
  197. if ((i + 1) % 10000 == 0) {
  198. System.out.println((i + 1) + " added...");
  199. }
  200. }
  201. }
  202. }