001: package org.drools.util;
002:
003: import junit.framework.TestCase;
004: import org.drools.util.ObjectHashMap;
005:
006: public class ObjectHashMapTest2 extends TestCase {
007:
008: public ObjectHashMapTest2() {
009: super ();
010: }
011:
012: public void testJUHashmap() {
013: final java.util.HashMap map = new java.util.HashMap();
014: assertNotNull(map);
015: final int count = 1000;
016: for (int idx = 0; idx < count; idx++) {
017: final String key = "key" + idx;
018: final String val = "value" + idx;
019: map.put(key, val);
020: assertEquals(val, map.get(key));
021: }
022: }
023:
024: public void testStringData() {
025: final ObjectHashMap map = new ObjectHashMap();
026: assertNotNull(map);
027: final int count = 1000;
028: for (int idx = 0; idx < count; idx++) {
029: final String key = "key" + idx;
030: final String val = "value" + idx;
031: map.put(key, val);
032: assertEquals(val, map.get(key));
033: }
034: }
035:
036: public void testStringDataDupFalse() {
037: final ObjectHashMap map = new ObjectHashMap();
038: assertNotNull(map);
039: final int count = 10000;
040: for (int idx = 0; idx < count; idx++) {
041: final String key = "key" + idx;
042: final String val = "value" + idx;
043: map.put(key, val, false);
044: assertEquals(val, map.get(key));
045: }
046: }
047:
048: public void testIntegerData() {
049: final ObjectHashMap map = new ObjectHashMap();
050: assertNotNull(map);
051: final int count = 1000;
052: for (int idx = 0; idx < count; idx++) {
053: final Integer key = new Integer(idx);
054: final Integer val = new Integer(idx);
055: map.put(key, val);
056: assertEquals(val, map.get(key));
057: }
058: }
059:
060: public void testJUHashMap1() {
061: final int count = 100000;
062: final java.util.HashMap map = new java.util.HashMap();
063: assertNotNull(map);
064: final long start = System.currentTimeMillis();
065: for (int idx = 0; idx < count; idx++) {
066: final String key = "key" + idx;
067: final String strval = "value" + idx;
068: map.put(key, strval);
069: }
070: final long end = System.currentTimeMillis();
071: System.out.println("java.util.HashMap put(key,value) ET - "
072: + ((end - start)));
073: }
074:
075: public void testStringData2() {
076: final int count = 100000;
077: final ObjectHashMap map = new ObjectHashMap();
078: assertNotNull(map);
079: final long start = System.currentTimeMillis();
080: for (int idx = 0; idx < count; idx++) {
081: final String key = "key" + idx;
082: final String strval = "value" + idx;
083: map.put(key, strval);
084: }
085: final long end = System.currentTimeMillis();
086: System.out.println("Custom ObjectHashMap put(key,value) ET - "
087: + ((end - start)));
088: }
089:
090: public void testStringData3() {
091: final int count = 100000;
092: final ObjectHashMap map = new ObjectHashMap();
093: assertNotNull(map);
094: for (int idx = 0; idx < count; idx++) {
095: final String key = "key" + idx;
096: final String strval = "value" + idx;
097: map.put(key, strval);
098: }
099: final long start = System.currentTimeMillis();
100: for (int idx = 0; idx < count; idx++) {
101: final String key = "key" + idx;
102: map.get(key);
103: }
104: final long end = System.currentTimeMillis();
105: System.out.println("Custom ObjectHashMap get(key) ET - "
106: + ((end - start)));
107: }
108:
109: public void testJUHashMap2() {
110: final int count = 100000;
111: final java.util.HashMap map = new java.util.HashMap();
112: assertNotNull(map);
113: for (int idx = 0; idx < count; idx++) {
114: final String key = "key" + idx;
115: final String strval = "value" + idx;
116: map.put(key, strval);
117: }
118: final long start = System.currentTimeMillis();
119: for (int idx = 0; idx < count; idx++) {
120: final String key = "key" + idx;
121: map.get(key);
122: }
123: final long end = System.currentTimeMillis();
124: System.out.println("java.util.HashMap get(key) ET - "
125: + ((end - start)));
126: }
127:
128: public void testStringData4() {
129: final int count = 100000;
130: final ObjectHashMap map = new ObjectHashMap();
131: assertNotNull(map);
132: for (int idx = 0; idx < count; idx++) {
133: final String key = "key" + idx;
134: final String strval = "value" + idx;
135: map.put(key, strval);
136: }
137: final long start = System.currentTimeMillis();
138: final org.drools.util.Iterator itr = map.iterator();
139: Object val = null;
140: while ((val = itr.next()) != null) {
141: val.hashCode();
142: }
143: final long end = System.currentTimeMillis();
144: System.out.println("Custom ObjectHashMap iterate ET - "
145: + ((end - start)));
146: }
147:
148: public void testJUHashMap3() {
149: final int count = 100000;
150: final java.util.HashMap map = new java.util.HashMap();
151: assertNotNull(map);
152: for (int idx = 0; idx < count; idx++) {
153: final String key = "key" + idx;
154: final String strval = "value" + idx;
155: map.put(key, strval);
156: }
157: final long start = System.currentTimeMillis();
158: final java.util.Iterator itr = map.values().iterator();
159: while (itr.hasNext()) {
160: itr.next().hashCode();
161: }
162: final long end = System.currentTimeMillis();
163: System.out.println("java.util.HashMap iterate ET - "
164: + ((end - start)));
165: }
166:
167: public void testStringData5() {
168: final int count = 100000;
169: final ObjectHashMap map = new ObjectHashMap();
170: assertNotNull(map);
171: final long start = System.currentTimeMillis();
172: for (int idx = 0; idx < count; idx++) {
173: final String key = "key" + idx;
174: final String strval = "value" + idx;
175: map.put(key, strval, false);
176: }
177: final long end = System.currentTimeMillis();
178: System.out.println("Custom ObjectHashMap dup false ET - "
179: + ((end - start)));
180: }
181:
182: public static void main(final String[] args) {
183: final ObjectHashMapTest2 test = new ObjectHashMapTest2();
184: final int loop = 5;
185: for (int idx = 0; idx < loop; idx++) {
186: test.testIntegerData();
187: test.testStringData();
188: test.testJUHashmap();
189: test.testStringData2();
190: test.testJUHashMap1();
191: test.testStringData3();
192: test.testJUHashMap2();
193: test.testStringData4();
194: test.testJUHashMap3();
195: test.testStringData5();
196: test.testStringDataDupFalse();
197: System.out.println(" --------------- ");
198: }
199: }
200: }
|