KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > tests > DistributedHashtableTest


1 // $Id: DistributedHashtableTest.java,v 1.5 2004/07/05 14:15:11 belaban Exp $
2

3 package org.jgroups.tests;
4
5
6 import org.jgroups.Address;
7 import org.jgroups.JChannelFactory;
8 import org.jgroups.blocks.DistributedHashtable;
9 import org.jgroups.util.Util;
10
11 import java.io.DataInputStream JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.Set JavaDoc;
14 import java.util.TreeSet JavaDoc;
15
16
17
18
19 /**
20  * Tests the DistributedHashtable interactively
21  * @author Bela Ban
22  */

23 public class DistributedHashtableTest implements Runnable JavaDoc {
24     DistributedHashtable ht;
25     long timeout=500;
26     Thread JavaDoc t=null;
27
28
29     public void start(String JavaDoc props, long timeout) throws Exception JavaDoc {
30         this.timeout=timeout;
31         ht=new DistributedHashtable("HashtableTest", new JChannelFactory(), props, timeout);
32     }
33
34
35     public void eventLoop() throws Exception JavaDoc {
36         int c;
37
38         while(true) {
39             System.out.println("[1] Insert [2] Start [3] Stop [4] Delete [5] Size [6] Print [q] Quit");
40             c=System.in.read();
41             switch(c) {
42                 case -1:
43                     break;
44                 case '1':
45                     insertEntries();
46                     break;
47                 case '2':
48                     start();
49                     break;
50                 case '3':
51                     stop();
52                     break;
53                 case '4':
54                     deleteEntries();
55                     break;
56                 case '5':
57                     printSize();
58                     break;
59                 case '6':
60                     printContents();
61                     break;
62                 case 'q':
63                     ht.stop();
64                     return;
65                 default:
66                     break;
67             }
68         }
69     }
70
71
72     public void insertEntries() {
73         try {
74             DataInputStream JavaDoc in=new DataInputStream JavaDoc(System.in);
75             Address local=ht.getLocalAddress();
76             long start, stop;
77             System.out.print("Number of entries: ");
78             System.out.flush();
79             System.in.skip(System.in.available());
80             String JavaDoc line=in.readLine();
81             int num=Integer.parseInt(line);
82             start=System.currentTimeMillis();
83             for(int i=0; i < num; i++) {
84                 if(i % 100 == 0)
85                     System.out.print(i + " ");
86                 ht.put(local.toString() + '#' + i, new Integer JavaDoc(i));
87             }
88             stop=System.currentTimeMillis();
89             double num_per_sec=num / ((stop-start)/1000.0);
90             System.out.println("\nInserted " + num + " elements in " + (stop - start) +
91                     " ms, size=" + ht.size() + " [" + num_per_sec + " / sec]");
92         }
93         catch(Exception JavaDoc ex) {
94             System.err.println(ex);
95         }
96     }
97
98     public void deleteEntries() {
99         try {
100             DataInputStream JavaDoc in=new DataInputStream JavaDoc(System.in);
101             Address local=ht.getLocalAddress();
102             Set JavaDoc keys;
103             long start, stop;
104             System.out.print("Number of entries: ");
105             System.out.flush();
106             System.in.skip(System.in.available());
107             String JavaDoc line=in.readLine();
108             int num=Integer.parseInt(line);
109             Object JavaDoc key;
110             int i=0;
111
112             Iterator JavaDoc it=ht.keySet().iterator();
113             keys=new TreeSet JavaDoc();
114             while(i++ < num) {
115                 try {
116                     key=it.next();
117                     keys.add(key);
118                 }
119                 catch(Exception JavaDoc ex) {
120                     break;
121                 }
122             }
123             start=System.currentTimeMillis();
124             for(it=keys.iterator(); it.hasNext();) {
125                 key=it.next();
126                 ht.remove(key);
127             }
128             stop=System.currentTimeMillis();
129             double num_per_sec=num / ((stop-start)/1000.0);
130             System.out.println("\nRemoved " + keys.size() + " elements in " + (stop - start) +
131                     "ms, size=" + ht.size() + " [" + num_per_sec + " / sec]");
132         }
133         catch(Exception JavaDoc ex) {
134             System.err.println(ex);
135         }
136     }
137
138     public void start() {
139         if(t == null) {
140             t=new Thread JavaDoc(this, "Modifier thread");
141             t.start();
142         }
143     }
144
145     public void stop() {
146         if(t != null)
147             t=null;
148     }
149
150     public void printSize() {
151         if(ht != null)
152             System.out.println("size=" + ht.size());
153     }
154
155     public void printContents() {
156         Set JavaDoc s=ht.keySet();
157         TreeSet JavaDoc ss=new TreeSet JavaDoc(s);
158         Object JavaDoc key;
159
160         for(Iterator JavaDoc it=ss.iterator(); it.hasNext();) {
161             key=it.next();
162             System.out.println(key + " --> " + ht.get(key));
163         }
164     }
165
166
167     public void run() {
168         while(t != null) {
169             // todo: random insertion, deletion, reads
170
Util.sleep(timeout);
171         }
172     }
173
174
175     public static void main(String JavaDoc[] args) {
176         long timeout=500; // timeout in ms between puts()/gets()/removes()
177
String JavaDoc props="UDP(mcast_addr=228.8.8.8;mcast_port=45566;ip_ttl=32;" +
178                 "ucast_recv_buf_size=16000;ucast_send_buf_size=16000;" +
179                 "mcast_send_buf_size=32000;mcast_recv_buf_size=64000;loopback=true):" +
180                 "PING(timeout=2000;num_initial_members=3):" +
181                 "MERGE2(min_interval=5000;max_interval=10000):" +
182                 "FD_SOCK:" +
183                 "VERIFY_SUSPECT(timeout=1500):" +
184                 "pbcast.NAKACK(gc_lag=50;retransmit_timeout=300,600,1200,2400,4800;max_xmit_size=8192):" +
185                 "UNICAST(timeout=2000):" +
186                 "pbcast.STABLE(desired_avg_gossip=20000):" +
187                 "FRAG(frag_size=8192;down_thread=false;up_thread=false):" +
188                 "pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;shun=false;print_local_addr=true):" +
189                 "pbcast.STATE_TRANSFER";
190
191         for(int i=0; i < args.length; i++) {
192             if("-help".equals(args[i])) {
193                 help();
194                 return;
195             }
196             if("-props".equals(args[i])) {
197                 props=args[++i];
198                 continue;
199             }
200             if("-timeout".equals(args[i])) {
201                 timeout=Long.parseLong(args[++i]);
202                 continue;
203             }
204         }
205
206
207
208         try {
209             DistributedHashtableTest test=new DistributedHashtableTest();
210             test.start(props, timeout);
211             test.eventLoop();
212         }
213         catch(Exception JavaDoc ex) {
214             System.err.println(ex);
215         }
216     }
217
218     static void help() {
219         System.out.println("DistributedHashtableTest [-help] [-props <props>] [-timeout <timeout>]");
220     }
221 }
222
Popular Tags