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