KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jgroups.tests;
2
3
4 import org.jgroups.JChannel;
5 import org.jgroups.JChannelFactory;
6 import org.jgroups.blocks.DistributedHashtable;
7 import org.jgroups.blocks.ReplicatedHashtable;
8 import org.jgroups.debug.Debugger;
9
10 import java.util.Hashtable JavaDoc;
11 import java.util.Map JavaDoc;
12 import java.util.Vector JavaDoc;
13
14
15 /**
16  * Add 1000 1k items to a hashtable (Distributed- or Replicated Hashtable) simultaneously
17  * @author Bela Ban
18  */

19 public class HashtableTest {
20     static int NUM_ITEMS=1000;
21     static long start, stop;
22
23     
24     
25     static class Notifier implements DistributedHashtable.Notification, ReplicatedHashtable.Notification {
26     int num_items=0;
27     int tmp;
28
29     Notifier(int n) {num_items=n;}
30
31     public void entrySet(Object JavaDoc key, Object JavaDoc value) {
32         tmp=((Integer JavaDoc)key).intValue();
33         if(tmp % 100 == 0)
34         System.out.println("** entrySet(" + key + ')');
35         if(tmp >= num_items) {
36         stop=System.currentTimeMillis();
37         System.out.println(num_items + " elements took " +
38                    (stop - start) + " msecs to receive and insert");
39         }
40     }
41
42     public void entryRemoved(Object JavaDoc key) {
43
44     }
45
46     public void viewChange(Vector JavaDoc new_mbrs, Vector JavaDoc old_mbrs) {
47         System.out.println("** viewChange(" + new_mbrs + ", " + old_mbrs + ')');
48     }
49
50         public void contentsSet(Map JavaDoc m) {
51             System.out.println("** contentsSet (" + (m != null? m.size()+"" : "0") + " items");
52         }
53
54         public void contentsCleared() {
55             System.out.println("** contentsCleared()");
56         }
57
58     }
59     
60
61     public static void main(String JavaDoc[] args) {
62     Hashtable ht;
63     int i;
64     byte[] buf;
65     boolean use_replicated_hashtable=false, debug=false, cummulative=false;
66     Debugger debugger=null;
67
68
69     /*
70     String props="UDP(mcast_addr=224.0.0.36;mcast_port=55566;ip_ttl=32;" +
71         "mcast_send_buf_size=150000;mcast_recv_buf_size=80000):" +
72         "PING(timeout=2000;num_initial_members=3):" +
73         "MERGE2(min_interval=5000;max_interval=10000):" +
74         "FD_SOCK:" +
75         "VERIFY_SUSPECT(timeout=1500):" +
76         "pbcast.STABLE(desired_avg_gossip=20000):" +
77         "pbcast.NAKACK(gc_lag=50;retransmit_timeout=300,600,1200,2400,4800):" +
78         "UNICAST(timeout=5000):" +
79         "FRAG(frag_size=16000;down_thread=false;up_thread=false):" +
80         "pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;" +
81         "shun=false;print_local_addr=true):" +
82         "pbcast.STATE_TRANSFER";
83         // "PERF(details=true)";
84     */

85
86
87     
88     String JavaDoc props="UDP(mcast_addr=224.0.0.36;mcast_port=55566;ip_ttl=32;" +
89             "mcast_send_buf_size=150000;mcast_recv_buf_size=80000):" +
90             "PING(timeout=2000;num_initial_members=3):" +
91             "MERGE2(min_interval=5000;max_interval=10000):" +
92             "FD_SOCK:" +
93             "VERIFY_SUSPECT(timeout=1500):" +
94             "pbcast.NAKACK(gc_lag=50;retransmit_timeout=600,1200,2400,4800):" +
95             "UNICAST(timeout=5000):" +
96             "pbcast.STABLE(desired_avg_gossip=20000):" +
97             "FRAG(frag_size=16000;down_thread=false;up_thread=false):" +
98             "pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;" +
99             "shun=false;print_local_addr=true):" +
100             "pbcast.STATE_TRANSFER";
101         // "PERF(details=true)";
102

103
104
105
106     
107
108
109
110
111
112     /*
113     String props="UDP(mcast_addr=224.0.0.35;mcast_port=45566;ip_ttl=32;" +
114         "mcast_send_buf_size=150000;mcast_recv_buf_size=80000):" +
115         //"PIGGYBACK:" +
116         "PING(timeout=2000;num_initial_members=5):" +
117         "FD_SOCK:" +
118         "VERIFY_SUSPECT(timeout=1500):" +
119         "UNICAST(timeout=5000):" +
120         "FRAG(frag_size=4096;down_thread=false;up_thread=false):" +
121         "TOTAL_TOKEN(block_sending=1000;unblock_sending=200):" +
122         "pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;" +
123         "shun=false;print_local_addr=true)";
124     */

125     
126
127     for(i=0; i < args.length; i++) {
128         if("-help".equals(args[i])) {
129         help();
130         return;
131         }
132         if("-use_rht".equals(args[i])) {
133         use_replicated_hashtable=true;
134         continue;
135         }
136         if("-props".equals(args[i])) {
137         props=args[++i];
138         continue;
139         }
140         if("-debug".equals(args[i])) {
141         debug=true;
142         continue;
143         }
144         if("-cummulative".equals(args[i])) {
145         cummulative=true;
146         continue;
147         }
148     }
149
150
151
152
153     
154     
155     try {
156         if(use_replicated_hashtable) {
157         ht=new ReplicatedHashtable("HashtableTest", new JChannelFactory(), props, 1000);
158         if(debug) {
159             debugger=new Debugger((JChannel)((ReplicatedHashtable)ht).getChannel(), cummulative);
160         }
161         ((ReplicatedHashtable)ht).addNotifier(new Notifier(NUM_ITEMS));
162         }
163         else {
164         ht=new DistributedHashtable("HashtableTest", new JChannelFactory(), props, 1000);
165         if(debug) {
166             debugger=new Debugger((JChannel)((DistributedHashtable)ht).getChannel(), cummulative);
167         }
168         // ((DistributedHashtable)ht).addNotifier(new MyNotifier());
169
}
170         if(debugger != null)
171         debugger.start();
172         
173         System.out.println("Hashtable already has " + ht.size() + " items");
174
175         System.out.print("Press key to insert " + NUM_ITEMS + " 1k items into DistributedHashtable");
176         System.in.read();
177
178         buf=new byte[1024];
179         for(i=0; i < buf.length; i++)
180         buf[i]=(byte)'x';
181
182         start=System.currentTimeMillis();
183         for(i=1; i <= NUM_ITEMS; i++) {
184         ht.put(new Integer JavaDoc(i), buf);
185         if(i % 100 == 0)
186             System.out.println(i); // will slow down insertion
187
}
188         stop=System.currentTimeMillis();
189         System.out.println(i + " elements took " + (stop - start) + " msecs to " +
190                    (use_replicated_hashtable? "send" : "insert"));
191         while(true) {
192         System.out.println("Hashtable has " + ht.size() + " entries");
193         System.in.read();
194         }
195     }
196     catch(Exception JavaDoc ex) {
197         System.err.println(ex);
198     }
199     }
200
201
202     static void help() {
203     System.out.println("HashtableTest [-help] [-use_rht] [-props <properties>] [-debug] [-cummulative]");
204     }
205
206 }
207
Popular Tags