1 package org.jgroups.tests; 2 3 4 import org.jgroups.*; 5 import org.jgroups.blocks.GroupRequest; 6 import org.jgroups.blocks.MethodCall; 7 import org.jgroups.blocks.RpcDispatcher; 8 import org.jgroups.util.Rsp; 9 import org.jgroups.util.RspList; 10 import org.jgroups.util.Util; 11 12 import java.lang.reflect.Method ; 13 14 15 16 17 22 public class RpcDispatcherSpeedTest implements MembershipListener { 23 Channel channel; 24 RpcDispatcher disp; 25 String props=null; 26 boolean server=false; int num=1000; 28 int mode=OLD; 29 static final int OLD=1; 30 static final int METHOD=2; 31 static final int TYPES=3; 32 static final int SIGNATURE=4; 33 static final long TIMEOUT=10000; 34 static final Class LONG_CLASS=long.class; 35 static final String LONG=long.class.getName(); 36 37 38 39 public RpcDispatcherSpeedTest(String props, boolean server, int num, int mode) { 40 this.props=props; 41 this.server=server; 42 this.num=num; 43 this.mode=mode; 44 } 45 46 public long measure(long start_time) throws Exception { 47 return System.currentTimeMillis() - start_time; 48 } 49 50 51 public void start() throws Exception { 52 channel=new JChannel(props); 53 disp=new RpcDispatcher(channel, null, this, this, 54 false, false); channel.connect("RpcDispatcherSpeedTestGroup"); 57 58 try { 59 if(server) { 60 System.out.println("-- Started as server. Press ctrl-c to kill"); 61 while(true) { 62 Util.sleep(10000); 63 } 64 } 65 else { 66 invokeRpcs(num, mode); 67 } 68 } 69 catch(Throwable t) { 70 t.printStackTrace(System.err); 71 } 72 finally { 73 System.out.println("Closing channel"); 74 channel.close(); 75 System.out.println("Closing channel: -- done"); 76 77 System.out.println("Stopping dispatcher"); 78 disp.stop(); 79 System.out.println("Stopping dispatcher: -- done"); 80 } 81 } 82 83 84 void invokeRpcs(int num, int mode) throws Exception { 85 RspList rsp_list; 86 Long start_time; 87 long total_time=0; 88 int show=num/10; 89 90 if(show <=0) show=1; 91 switch(mode) { 92 case OLD: 93 System.out.println("-- invoking " + num + " methods using mode=OLD"); 94 for(int i=1; i <= num; i++) { 95 start_time=new Long (System.currentTimeMillis()); 96 rsp_list=disp.callRemoteMethods(null, 97 "measure", 98 new Object [] {start_time}, 99 new Class []{long.class}, 100 GroupRequest.GET_ALL, TIMEOUT); 101 total_time+=getAverage(rsp_list); 102 if(i % show == 0) 103 System.out.println(i); 104 } 105 printStats(total_time, num); 106 break; 107 108 case METHOD: 109 System.out.println("-- invoking " + num + " methods using mode=METHOD"); 110 Method method=getClass().getMethod("measure", new Class []{long.class}); 111 MethodCall method_call; 112 for(int i=1; i <= num; i++) { 113 start_time=new Long (System.currentTimeMillis()); 114 method_call=new MethodCall(method, new Object []{start_time}); 115 rsp_list=disp.callRemoteMethods(null, method_call, GroupRequest.GET_ALL, 116 TIMEOUT); 117 total_time+=getAverage(rsp_list); 118 if(i % show == 0) 119 System.out.println(i); 120 } 121 printStats(total_time, num); 122 break; 123 124 case TYPES: 125 System.out.println("-- invoking " + num + " methods using mode=TYPES"); 126 for(int i=1; i <= num; i++) { 127 start_time=new Long (System.currentTimeMillis()); 128 rsp_list=disp.callRemoteMethods(null, "measure", 129 new Object []{start_time}, 130 new Class []{LONG_CLASS}, 131 GroupRequest.GET_ALL, 132 TIMEOUT); 133 total_time+=getAverage(rsp_list); 134 if(i % show == 0) 135 System.out.println(i); 136 } 137 printStats(total_time, num); 138 break; 139 140 case SIGNATURE: 141 System.out.println("-- invoking " + num + " methods using mode=SIGNATURE"); 142 for(int i=1; i <= num; i++) { 143 start_time=new Long (System.currentTimeMillis()); 144 rsp_list=disp.callRemoteMethods(null, "measure", 145 new Object []{start_time}, 146 new String []{LONG}, 147 GroupRequest.GET_ALL, 148 TIMEOUT); 149 total_time+=getAverage(rsp_list); 150 if(i % show == 0) 151 System.out.println(i); 152 } 153 printStats(total_time, num); 154 break; 155 default: 156 break; 157 } 158 159 } 160 161 162 double getAverage(RspList rsps) { 163 Rsp rsp; 164 double retval=0; 165 int num=0; 166 167 if(rsps == null || rsps.size() == 0) { 168 System.err.println("response list is empty"); 169 return 0.0; 170 } 171 for(int i=0; i < rsps.size(); i++) { 172 rsp=(Rsp)rsps.elementAt(i); 173 if(rsp.getValue() != null && rsp.getValue() instanceof Long ) { 174 retval+=((Long )rsp.getValue()).longValue(); 175 num++; 176 } 177 else { 178 System.err.println("response " + rsp.getValue() + " invalid"); 179 } 180 } 181 return retval / num; 182 } 183 184 void printStats(long total_time, int num) { 185 double throughput=((double)num)/((double)total_time/1000.0); 186 System.out.println("time for " + num + " remote calls was " + 187 total_time + ", avg=" + (total_time / (double)num) + 188 "ms/invocation, " + (long)throughput + " calls/sec"); 189 } 190 191 public void viewAccepted(View new_view) { 192 System.out.println("-- new view: " + new_view); 193 } 194 195 196 197 public void suspect(Address suspected_mbr) { 198 ; 199 } 200 201 202 203 public void block() { 204 ; 205 } 206 207 208 209 public static void main(String [] args) { 210 String props=null; 211 boolean server=false; 212 int num=1000; 213 RpcDispatcherSpeedTest test; 214 int mode=OLD; 215 216 for(int i=0; i < args.length; i++) { 217 if("-props".equals(args[i])) { 218 props=args[++i]; 219 continue; 220 } 221 if("-server".equals(args[i])) { 222 server=true; 223 continue; 224 } 225 if("-num".equals(args[i])) { 226 num=Integer.parseInt(args[++i]); 227 continue; 228 } 229 if("-mode".equals(args[i])) { 230 String m=args[++i].toLowerCase().trim(); 231 if("old".equals(m)) 232 mode=OLD; 233 else if("method".equals(m)) 234 mode=METHOD; 235 else if("types".equals(m)) 236 mode=TYPES; 237 else if("signature".equals(m)) 238 mode=SIGNATURE; 239 else { 240 System.err.println("mode " + m + " is invalid"); 241 help(); 242 return; 243 } 244 continue; 245 } 246 help(); 247 return; 248 } 249 250 251 try { 252 test=new RpcDispatcherSpeedTest(props, server, num, mode); 253 test.start(); 254 } 255 catch(Exception e) { 256 System.err.println(e); 257 } 258 } 259 260 static void help() { 261 System.out.println("RpcDispatcherSpeedTest [-help] [-props <props>] " + 262 "[-server] [-num <number of calls>] [-mode <mode>]"); 263 System.out.println("mode can be either 'old', 'method', 'types' or 'signature'"); 264 } 265 } 266 | Popular Tags |