001: package org.jgroups.tests;
002:
003: //import gnu.trove.TLongObjectHashMap;
004: //import gnu.trove.THashMap;
005:
006: import EDU.oswego.cs.dl.util.concurrent.ConcurrentReaderHashMap;
007: import EDU.oswego.cs.dl.util.concurrent.ConcurrentHashMap;
008:
009: import java.util.HashMap;
010: import java.util.Map;
011: import java.util.TreeMap;
012:
013: /**
014: * @author Bela Ban
015: * @version $Id: HashMapTest.java,v 1.5 2005/09/02 14:27:38 belaban Exp $
016: */
017: public class HashMapTest {
018:
019: public static void main(String[] args) throws Exception {
020: int num = 10000;
021: for (int i = 0; i < args.length; i++) {
022: if (args[i].equals("-num")) {
023: num = Integer.parseInt(args[++i]);
024: continue;
025: }
026: System.out.println("HashMapTest [-num <num>] [-help]");
027: return;
028: }
029: HashMapTest t = new HashMapTest();
030: Class[] classes = new Class[] { HashMap.class, TreeMap.class,
031: ConcurrentReaderHashMap.class, ConcurrentHashMap.class };
032: Map[] maps = new Map[classes.length];
033:
034: System.out.println("\nTesting creation times");
035: for (int i = 0; i < classes.length; i++) {
036: t.testCreation(classes[i], num);
037: }
038:
039: for (int i = 0; i < classes.length; i++)
040: maps[i] = (Map) classes[i].newInstance();
041:
042: System.out.println("\nTesting puts and gets");
043: for (int i = 0; i < maps.length; i++) {
044: t.testPutAndGet(maps[i], num);
045: }
046: }
047:
048: /* private void start3(THashMap m, int num) {
049: long start, stop;
050:
051: start=System.currentTimeMillis();
052: for(int i=0; i < num; i++) {
053: m.put(new Long(i), "bla");
054: }
055:
056: stop=System.currentTimeMillis();
057: System.out.println("Took " + (stop-start) + "ms to insert " + m.size() + " elements into " + m.getClass().getName());
058: m.clear();
059: }
060:
061: private void start2(TLongObjectHashMap m, int num) {
062: long start, stop;
063:
064: start=System.currentTimeMillis();
065: for(int i=0; i < num; i++) {
066: m.put(i, "bla");
067: }
068:
069: stop=System.currentTimeMillis();
070: System.out.println("Took " + (stop-start) + "ms to insert " + m.size() + " elements into " + m.getClass().getName());
071: m.clear();
072: }*/
073:
074: private void testCreation(Class cl, int num)
075: throws IllegalAccessException, InstantiationException {
076: long start, stop;
077:
078: start = System.currentTimeMillis();
079: for (int i = 0; i < num; i++) {
080: cl.newInstance();
081: }
082:
083: stop = System.currentTimeMillis();
084: System.out.println("Took " + (stop - start) + "ms to create "
085: + num + " instances of " + cl.getName());
086: }
087:
088: private void testPutAndGet(Map m, int num) throws Exception {
089: long start, stop;
090: Object retval;
091:
092: start = System.currentTimeMillis();
093: for (int i = 0; i < num; i++) {
094: m.put(new Long(i), "bla");
095: }
096:
097: stop = System.currentTimeMillis();
098: System.out
099: .println("Took " + (stop - start) + "ms to insert "
100: + m.size() + " elements into "
101: + m.getClass().getName());
102:
103: start = System.currentTimeMillis();
104: for (int i = 0; i < num; i++) {
105: retval = m.get(new Long(i));
106: if (retval == null)
107: throw new Exception("retval for " + i + " is null");
108: }
109:
110: stop = System.currentTimeMillis();
111: System.out
112: .println("Took " + (stop - start) + "ms to fetch "
113: + m.size() + " elements from "
114: + m.getClass().getName());
115:
116: start = System.currentTimeMillis();
117: m.clear();
118: stop = System.currentTimeMillis();
119: System.out.println("Took " + (stop - start) + "ms to clear "
120: + m.getClass().getName() + "\n");
121: }
122:
123: }
|