1 package org.jgroups.tests; 2 3 import org.jgroups.*; 4 import org.jgroups.util.Util; 5 6 11 public class RoundTrip extends ReceiverAdapter { 12 JChannel channel; 13 String props; 14 int num=1000; 15 int msg_size=10; 16 boolean server=false; 17 final byte[] RSP_BUF=new byte[]{1}; int num_responses=0; 19 final Object mutex=new Object (); 21 22 private void start(boolean server, int num, int msg_size, String props) throws ChannelException { 23 this.server=server; 24 this.num=num; 25 this.msg_size=msg_size; 26 this.props=props; 27 28 channel=new JChannel(props); 29 channel.setReceiver(this); 30 channel.connect("rt"); 31 32 if(server) { 33 System.out.println("server started (ctrl-c to kill)"); 34 while(true) { 35 Util.sleep(60000); 36 } 37 } 38 else { 39 channel.setOpt(Channel.LOCAL, Boolean.FALSE); 40 System.out.println("sending " + num + " requests"); 41 sendRequests(); 42 channel.close(); 43 } 44 } 45 46 50 public void receive(Message msg) { 51 byte[] buf=msg.getRawBuffer(); 52 if(buf == null) { 53 System.err.println("buffer was null !"); 54 return; 55 } 56 if(buf[0] == 0) { if(!server) { return; 59 } 60 Message response=new Message(msg.getSrc(), null, null); 62 response.setBuffer(RSP_BUF, 0, RSP_BUF.length); 63 try { 64 channel.send(response); 65 } 66 catch(Exception e) { 67 e.printStackTrace(); 68 } 69 } 70 else { synchronized(mutex) { 72 num_responses++; 73 mutex.notify(); 75 } 76 } 77 } 78 79 private void sendRequests() { 80 byte[] buf=new byte[msg_size]; 81 long start, stop, total; 82 double requests_per_sec; 83 double ms_per_req; 84 Message msg; 85 int print=num / 10; 86 int count=0; 87 88 num_responses=0; 89 for(int i=0; i < buf.length; i++) { 90 buf[i]=0; } 92 93 97 98 start=System.currentTimeMillis(); 99 for(int i=0; i < num; i++) { 100 msg=new Message(null, null, null); 101 msg.setBuffer(buf); 102 try { 103 channel.send(msg); 104 synchronized(mutex) { 105 while(num_responses != count +1) { 106 mutex.wait(1000); 107 } 108 count=num_responses; 109 if(num_responses >= num) { 110 System.out.println("received all responses (" + num_responses + ")"); 111 break; 112 } 113 } 114 if(num_responses % print == 0) { 115 System.out.println("- received " + num_responses); 116 } 117 } 118 catch(Exception e) { 119 e.printStackTrace(); 120 } 121 } 122 stop=System.currentTimeMillis(); 123 total=stop-start; 124 requests_per_sec=num / (total / 1000.0); 125 ms_per_req=total / (double)num; 126 System.out.println("Took " + total + "ms for " + num + " requests: " + requests_per_sec + 127 " requests/sec, " + ms_per_req + " ms/request"); 128 } 129 130 131 public static void main(String [] args) throws ChannelException { 132 boolean server=false; 133 int num=100; 134 int msg_size=10; String props=null; 136 137 for(int i=0; i < args.length; i++) { 138 if(args[i].equals("-num")) { 139 num=Integer.parseInt(args[++i]); 140 continue; 141 } 142 if(args[i].equals("-server")) { 143 server=true; 144 continue; 145 } 146 if(args[i].equals("-size")) { 147 msg_size=Integer.parseInt(args[++i]); 148 continue; 149 } 150 if(args[i].equals("-props")) { 151 props=args[++i]; 152 continue; 153 } 154 help(); 155 return; 156 } 157 new RoundTrip().start(server, num, msg_size, props); 158 } 159 160 161 162 private static void help() { 163 System.out.println("RoundTrip [-server] [-num <number of messages>] " + 164 "[-size <size of each message (in bytes)>] [-props <properties>]"); 165 } 166 } 167 | Popular Tags |