001: // $Id: DistributedHashtableTest.java,v 1.7 2005/05/30 16:15:11 belaban Exp $
002:
003: package org.jgroups.tests;
004:
005: import org.jgroups.Address;
006: import org.jgroups.JChannelFactory;
007: import org.jgroups.blocks.DistributedHashtable;
008: import org.jgroups.util.Util;
009:
010: import java.io.DataInputStream;
011: import java.util.Iterator;
012: import java.util.Set;
013: import java.util.TreeSet;
014:
015: /**
016: * Tests the DistributedHashtable interactively
017: * @author Bela Ban
018: */
019: public class DistributedHashtableTest implements Runnable {
020: DistributedHashtable ht;
021: long timeout = 500;
022: Thread t = null;
023:
024: public void start(String props, long timeout) throws Exception {
025: this .timeout = timeout;
026: ht = new DistributedHashtable("HashtableTest",
027: new JChannelFactory(), props, timeout);
028: }
029:
030: public void eventLoop() throws Exception {
031: int c;
032:
033: while (true) {
034: System.out
035: .println("[1] Insert [2] Start [3] Stop [4] Delete [5] Size [6] Print [q] Quit");
036: c = System.in.read();
037: switch (c) {
038: case -1:
039: break;
040: case '1':
041: insertEntries();
042: break;
043: case '2':
044: start();
045: break;
046: case '3':
047: stop();
048: break;
049: case '4':
050: deleteEntries();
051: break;
052: case '5':
053: printSize();
054: break;
055: case '6':
056: printContents();
057: break;
058: case 'q':
059: ht.stop();
060: return;
061: default:
062: break;
063: }
064: }
065: }
066:
067: public void insertEntries() {
068: try {
069: DataInputStream in = new DataInputStream(System.in);
070: Address local = ht.getLocalAddress();
071: long start, stop;
072: System.out.print("Number of entries: ");
073: System.out.flush();
074: System.in.skip(System.in.available());
075: String line = in.readLine();
076: int num = Integer.parseInt(line);
077: start = System.currentTimeMillis();
078: for (int i = 0; i < num; i++) {
079: if (i % 100 == 0)
080: System.out.print(i + " ");
081: ht.put(local.toString() + '#' + i, new Integer(i));
082: }
083: stop = System.currentTimeMillis();
084: double num_per_sec = num / ((stop - start) / 1000.0);
085: System.out.println("\nInserted " + num + " elements in "
086: + (stop - start) + " ms, size=" + ht.size() + " ["
087: + num_per_sec + " / sec]");
088: } catch (Exception ex) {
089: System.err.println(ex);
090: }
091: }
092:
093: public void deleteEntries() {
094: try {
095: DataInputStream in = new DataInputStream(System.in);
096: Address local = ht.getLocalAddress();
097: Set keys;
098: long start, stop;
099: System.out.print("Number of entries: ");
100: System.out.flush();
101: System.in.skip(System.in.available());
102: String line = in.readLine();
103: int num = Integer.parseInt(line);
104: Object key;
105: int i = 0;
106:
107: Iterator it = ht.keySet().iterator();
108: keys = new TreeSet();
109: while (i++ < num) {
110: try {
111: key = it.next();
112: keys.add(key);
113: } catch (Exception ex) {
114: break;
115: }
116: }
117: start = System.currentTimeMillis();
118: for (it = keys.iterator(); it.hasNext();) {
119: key = it.next();
120: ht.remove(key);
121: }
122: stop = System.currentTimeMillis();
123: double num_per_sec = num / ((stop - start) / 1000.0);
124: System.out.println("\nRemoved " + keys.size()
125: + " elements in " + (stop - start) + "ms, size="
126: + ht.size() + " [" + num_per_sec + " / sec]");
127: } catch (Exception ex) {
128: System.err.println(ex);
129: }
130: }
131:
132: public void start() {
133: if (t == null) {
134: t = new Thread(this , "Modifier thread");
135: t.start();
136: }
137: }
138:
139: public void stop() {
140: if (t != null)
141: t = null;
142: }
143:
144: public void printSize() {
145: if (ht != null)
146: System.out.println("size=" + ht.size());
147: }
148:
149: public void printContents() {
150: Set s = ht.keySet();
151: TreeSet ss = new TreeSet(s);
152: Object key;
153:
154: for (Iterator it = ss.iterator(); it.hasNext();) {
155: key = it.next();
156: System.out.println(key + " --> " + ht.get(key));
157: }
158: }
159:
160: public void run() {
161: while (t != null) {
162: // todo: random insertion, deletion, reads
163: Util.sleep(timeout);
164: }
165: }
166:
167: public static void main(String[] args) {
168: long timeout = 500; // timeout in ms between puts()/gets()/removes()
169: String props = "UDP(mcast_addr=228.8.8.8;mcast_port=45566;ip_ttl=32;"
170: + "ucast_recv_buf_size=16000;ucast_send_buf_size=16000;"
171: + "mcast_send_buf_size=32000;mcast_recv_buf_size=64000;loopback=true):"
172: + "PING(timeout=2000;num_initial_members=3):"
173: + "MERGE2(min_interval=5000;max_interval=10000):"
174: + "FD_SOCK:"
175: + "VERIFY_SUSPECT(timeout=1500):"
176: + "pbcast.NAKACK(gc_lag=50;retransmit_timeout=300,600,1200,2400,4800;max_xmit_size=8192):"
177: + "UNICAST(timeout=2000):"
178: + "pbcast.STABLE(desired_avg_gossip=20000):"
179: + "FRAG(frag_size=8192;down_thread=false;up_thread=false):"
180: + "pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;shun=false;print_local_addr=true):"
181: + "pbcast.STATE_TRANSFER";
182:
183: for (int i = 0; i < args.length; i++) {
184: if ("-help".equals(args[i])) {
185: help();
186: return;
187: }
188: if ("-props".equals(args[i])) {
189: props = args[++i];
190: continue;
191: }
192: if ("-timeout".equals(args[i])) {
193: timeout = Long.parseLong(args[++i]);
194: continue;
195: }
196: }
197:
198: try {
199: DistributedHashtableTest test = new DistributedHashtableTest();
200: test.start(props, timeout);
201: test.eventLoop();
202: } catch (Exception ex) {
203: System.err.println(ex);
204: }
205: }
206:
207: static void help() {
208: System.out
209: .println("DistributedHashtableTest [-help] [-props <props>] [-timeout <timeout>]");
210: }
211: }
|