1 package org.jgroups.tests; 2 3 import org.jgroups.ReceiverAdapter; 4 5 import java.io.IOException ; 6 import java.net.DatagramPacket ; 7 import java.net.DatagramSocket ; 8 import java.net.InetAddress ; 9 10 15 public class RoundTripUdp extends ReceiverAdapter { 16 DatagramSocket sock; 17 InetAddress host; 18 int port=7500; 19 int num=1000; 20 int msg_size=10; 21 boolean server=false; 22 final byte[] RSP_BUF=new byte[]{1}; int num_responses=0; 24 25 26 private void start(boolean server, int num, int msg_size, InetAddress host, int port) throws IOException { 27 this.server=server; 28 this.num=num; 29 this.msg_size=msg_size; 30 this.host=host; 31 this.port=port; 32 33 34 if(server) { 35 sock=new DatagramSocket (port, host); 36 System.out.println("server started (ctrl-c to kill)"); 37 while(true) { 38 byte[] buf=new byte[65000]; 39 DatagramPacket packet; 40 packet=new DatagramPacket (buf, 0, buf.length); 41 sock.receive(packet); 42 packet=new DatagramPacket (RSP_BUF, 0, RSP_BUF.length, packet.getAddress(), packet.getPort()); 43 sock.send(packet); } 45 } 46 else { 47 sock=new DatagramSocket (); 48 System.out.println("sending " + num + " requests"); 49 sendRequests(); 50 } 51 sock.close(); 52 } 53 54 55 56 private void sendRequests() { 57 byte[] buf=new byte[msg_size]; 58 long start, stop, total; 59 double requests_per_sec; 60 double ms_per_req; 61 int print=num / 10; 62 63 num_responses=0; 64 for(int i=0; i < buf.length; i++) { 65 buf[i]=0; } 67 68 start=System.currentTimeMillis(); 69 for(int i=0; i < num; i++) { 70 DatagramPacket packet=new DatagramPacket (buf, 0, buf.length, host, port); 71 try { 72 sock.send(packet); 73 74 byte[] response=new byte[1]; 75 DatagramPacket rsp_packet=new DatagramPacket (response, 0, response.length); 76 sock.receive(rsp_packet); 77 num_responses++; 78 if(num_responses >= num) { 79 System.out.println("received all responses (" + num_responses + ")"); 80 break; 81 } 82 if(num_responses % print == 0) { 83 System.out.println("- received " + num_responses); 84 } 85 } 86 catch(Exception e) { 87 e.printStackTrace(); 88 } 89 } 90 stop=System.currentTimeMillis(); 91 total=stop-start; 92 requests_per_sec=num / (total / 1000.0); 93 ms_per_req=total / (double)num; 94 System.out.println("Took " + total + "ms for " + num + " requests: " + requests_per_sec + 95 " requests/sec, " + ms_per_req + " ms/request"); 96 } 97 98 99 public static void main(String [] args) throws IOException { 100 boolean server=false; 101 int num=100; 102 int msg_size=10; InetAddress host=null; 104 int port=7500; 105 106 for(int i=0; i < args.length; i++) { 107 if(args[i].equals("-num")) { 108 num=Integer.parseInt(args[++i]); 109 continue; 110 } 111 if(args[i].equals("-server")) { 112 server=true; 113 continue; 114 } 115 if(args[i].equals("-size")) { 116 msg_size=Integer.parseInt(args[++i]); 117 continue; 118 } 119 if(args[i].equals("-host")) { 120 host=InetAddress.getByName(args[++i]); 121 continue; 122 } 123 if(args[i].equals("-port")) { 124 port=Integer.parseInt(args[++i]); 125 continue; 126 } 127 RoundTripUdp.help(); 128 return; 129 } 130 131 if(host == null) 132 host=InetAddress.getLocalHost(); 133 new RoundTripUdp().start(server, num, msg_size, host, port); 134 } 135 136 137 138 private static void help() { 139 System.out.println("RoundTrip [-server] [-num <number of messages>] " + 140 "[-size <size of each message (in bytes)>] [-host <host>] [-port <port>]"); 141 } 142 } 143 | Popular Tags |