1 package org.jgroups.tests; 2 3 4 import org.jgroups.*; 5 import org.jgroups.blocks.GroupRequest; 6 import org.jgroups.blocks.MessageDispatcher; 7 import org.jgroups.blocks.RequestHandler; 8 import org.jgroups.util.Util; 9 10 11 12 13 17 public class MessageDispatcherSpeedTest implements MembershipListener, RequestHandler { 18 Channel channel; 19 MessageDispatcher disp; 20 String props=null; 21 boolean server=false; int num=1000, received=0; 23 static final long TIMEOUT=10000; 24 25 26 27 public MessageDispatcherSpeedTest(String props, boolean server, int num) { 28 this.props=props; 29 this.server=server; 30 this.num=num; 31 } 32 33 34 public Object handle(Message msg) { 35 received++; 36 if(received % 1000 == 0) 37 System.out.println("-- received " + received); 38 return null; 39 } 40 41 public void start() throws Exception { 42 channel=new JChannel(props); 43 disp=new MessageDispatcher(channel, null, this, this, false); 45 channel.connect("MessageDispatcherSpeedTestGroup"); 46 47 try { 48 if(server) { 49 System.out.println("-- Started as server. Press ctrl-c to kill"); 50 while(true) { 51 Util.sleep(10000); 52 } 53 } 54 else { 55 sendMessages(num); 56 } 57 } 58 catch(Throwable t) { 59 t.printStackTrace(System.err); 60 } 61 finally { 62 channel.close(); 63 disp.stop(); 64 } 65 } 66 67 68 void sendMessages(int num) throws Exception { 69 long start, stop; 70 int show=num/10; 71 72 if(show <=0) show=1; 73 start=System.currentTimeMillis(); 74 75 System.out.println("-- sending " + num + " messages"); 76 for(int i=1; i <= num; i++) { 77 disp.castMessage(null, new Message(), GroupRequest.GET_ALL, TIMEOUT); 78 if(i % show == 0) 79 System.out.println("-- sent " + i); 80 } 81 stop=System.currentTimeMillis(); 82 printStats(stop-start, num); 83 } 84 85 86 87 void printStats(long total_time, int num) { 88 double throughput=((double)num)/((double)total_time/1000.0); 89 System.out.println("time for " + num + " remote calls was " + 90 total_time + ", avg=" + (total_time / (double)num) + 91 "ms/invocation, " + (long)throughput + " calls/sec"); 92 } 93 94 public void viewAccepted(View new_view) { 95 System.out.println("-- new view: " + new_view); 96 } 97 98 99 100 public void suspect(Address suspected_mbr) { 101 ; 102 } 103 104 105 106 public void block() { 107 ; 108 } 109 110 111 112 public static void main(String [] args) { 113 String props=null; 114 boolean server=false; 115 int num=1000; 116 MessageDispatcherSpeedTest test; 117 118 for(int i=0; i < args.length; i++) { 119 if("-props".equals(args[i])) { 120 props=args[++i]; 121 continue; 122 } 123 if("-server".equals(args[i])) { 124 server=true; 125 continue; 126 } 127 if("-num".equals(args[i])) { 128 num=Integer.parseInt(args[++i]); 129 continue; 130 } 131 help(); 132 return; 133 } 134 135 136 try { 137 test=new MessageDispatcherSpeedTest(props, server, num); 138 test.start(); 139 } 140 catch(Exception e) { 141 System.err.println(e); 142 } 143 } 144 145 static void help() { 146 System.out.println("RpcDispatcherSpeedTest [-help] [-props <props>] [-server] [-num <number of calls>]"); 147 } 148 149 150 } 151 | Popular Tags |