001: package org.jgroups.tests;
002:
003: import org.jgroups.JChannel;
004: import org.jgroups.JChannelFactory;
005: import org.jgroups.blocks.DistributedHashtable;
006: import org.jgroups.blocks.ReplicatedHashtable;
007: import org.jgroups.debug.Debugger;
008:
009: import java.util.Hashtable;
010: import java.util.Map;
011: import java.util.Vector;
012:
013: /**
014: * Add 1000 1k items to a hashtable (Distributed- or Replicated Hashtable) simultaneously
015: * @author Bela Ban
016: */
017: public class HashtableTest {
018: static int NUM_ITEMS = 1000;
019: static long start, stop;
020:
021: static class Notifier implements DistributedHashtable.Notification,
022: ReplicatedHashtable.Notification {
023: int num_items = 0;
024: int tmp;
025:
026: Notifier(int n) {
027: num_items = n;
028: }
029:
030: public void entrySet(Object key, Object value) {
031: tmp = ((Integer) key).intValue();
032: if (tmp % 100 == 0)
033: System.out.println("** entrySet(" + key + ')');
034: if (tmp >= num_items) {
035: stop = System.currentTimeMillis();
036: System.out.println(num_items + " elements took "
037: + (stop - start)
038: + " msecs to receive and insert");
039: }
040: }
041:
042: public void entryRemoved(Object key) {
043:
044: }
045:
046: public void viewChange(Vector new_mbrs, Vector old_mbrs) {
047: System.out.println("** viewChange(" + new_mbrs + ", "
048: + old_mbrs + ')');
049: }
050:
051: public void contentsSet(Map m) {
052: System.out.println("** contentsSet ("
053: + (m != null ? m.size() + "" : "0") + " items");
054: }
055:
056: public void contentsCleared() {
057: System.out.println("** contentsCleared()");
058: }
059:
060: }
061:
062: public static void main(String[] args) {
063: Hashtable ht;
064: int i;
065: byte[] buf;
066: boolean use_replicated_hashtable = false, debug = false, cummulative = false;
067: Debugger debugger = null;
068:
069: /*
070: String props="UDP(mcast_addr=224.0.0.36;mcast_port=55566;ip_ttl=32;" +
071: "mcast_send_buf_size=150000;mcast_recv_buf_size=80000):" +
072: "PING(timeout=2000;num_initial_members=3):" +
073: "MERGE2(min_interval=5000;max_interval=10000):" +
074: "FD_SOCK:" +
075: "VERIFY_SUSPECT(timeout=1500):" +
076: "pbcast.STABLE(desired_avg_gossip=20000):" +
077: "pbcast.NAKACK(gc_lag=50;retransmit_timeout=300,600,1200,2400,4800):" +
078: "UNICAST(timeout=5000):" +
079: "FRAG(frag_size=16000;down_thread=false;up_thread=false):" +
080: "pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;" +
081: "shun=false;print_local_addr=true):" +
082: "pbcast.STATE_TRANSFER";
083: // "PERF(details=true)";
084: */
085:
086: String props = "UDP(mcast_addr=224.0.0.36;mcast_port=55566;ip_ttl=32;"
087: + "mcast_send_buf_size=150000;mcast_recv_buf_size=80000):"
088: + "PING(timeout=2000;num_initial_members=3):"
089: + "MERGE2(min_interval=5000;max_interval=10000):"
090: + "FD_SOCK:"
091: + "VERIFY_SUSPECT(timeout=1500):"
092: + "pbcast.NAKACK(gc_lag=50;retransmit_timeout=600,1200,2400,4800):"
093: + "UNICAST(timeout=5000):"
094: + "pbcast.STABLE(desired_avg_gossip=20000):"
095: + "FRAG(frag_size=16000;down_thread=false;up_thread=false):"
096: + "pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;"
097: + "shun=false;print_local_addr=true):"
098: + "pbcast.STATE_TRANSFER";
099: // "PERF(details=true)";
100:
101: /*
102: String props="UDP(mcast_addr=224.0.0.35;mcast_port=45566;ip_ttl=32;" +
103: "mcast_send_buf_size=150000;mcast_recv_buf_size=80000):" +
104: //"PIGGYBACK:" +
105: "PING(timeout=2000;num_initial_members=5):" +
106: "FD_SOCK:" +
107: "VERIFY_SUSPECT(timeout=1500):" +
108: "UNICAST(timeout=5000):" +
109: "FRAG(frag_size=4096;down_thread=false;up_thread=false):" +
110: "TOTAL_TOKEN(block_sending=1000;unblock_sending=200):" +
111: "pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;" +
112: "shun=false;print_local_addr=true)";
113: */
114:
115: for (i = 0; i < args.length; i++) {
116: if ("-help".equals(args[i])) {
117: help();
118: return;
119: }
120: if ("-use_rht".equals(args[i])) {
121: use_replicated_hashtable = true;
122: continue;
123: }
124: if ("-props".equals(args[i])) {
125: props = args[++i];
126: continue;
127: }
128: if ("-debug".equals(args[i])) {
129: debug = true;
130: continue;
131: }
132: if ("-cummulative".equals(args[i])) {
133: cummulative = true;
134: continue;
135: }
136: }
137:
138: try {
139: if (use_replicated_hashtable) {
140: ht = new ReplicatedHashtable("HashtableTest",
141: new JChannelFactory(), props, 1000);
142: if (debug) {
143: debugger = new Debugger(
144: (JChannel) ((ReplicatedHashtable) ht)
145: .getChannel(), cummulative);
146: }
147: ((ReplicatedHashtable) ht).addNotifier(new Notifier(
148: NUM_ITEMS));
149: } else {
150: ht = new DistributedHashtable("HashtableTest",
151: new JChannelFactory(), props, 1000);
152: if (debug) {
153: debugger = new Debugger(
154: (JChannel) ((DistributedHashtable) ht)
155: .getChannel(), cummulative);
156: }
157: // ((DistributedHashtable)ht).addNotifier(new MyNotifier());
158: }
159: if (debugger != null)
160: debugger.start();
161:
162: System.out.println("Hashtable already has " + ht.size()
163: + " items");
164:
165: System.out.print("Press key to insert " + NUM_ITEMS
166: + " 1k items into DistributedHashtable");
167: System.in.read();
168:
169: buf = new byte[1024];
170: for (i = 0; i < buf.length; i++)
171: buf[i] = (byte) 'x';
172:
173: start = System.currentTimeMillis();
174: for (i = 1; i <= NUM_ITEMS; i++) {
175: ht.put(new Integer(i), buf);
176: if (i % 100 == 0)
177: System.out.println(i); // will slow down insertion
178: }
179: stop = System.currentTimeMillis();
180: System.out.println(i + " elements took " + (stop - start)
181: + " msecs to "
182: + (use_replicated_hashtable ? "send" : "insert"));
183: while (true) {
184: System.out.println("Hashtable has " + ht.size()
185: + " entries");
186: System.in.read();
187: }
188: } catch (Exception ex) {
189: System.err.println(ex);
190: }
191: }
192:
193: static void help() {
194: System.out
195: .println("HashtableTest [-help] [-use_rht] [-props <properties>] [-debug] [-cummulative]");
196: }
197:
198: }
|