1 package org.jgroups.tests; 2 3 import org.jgroups.util.Util; 4 5 import java.net.DatagramPacket ; 6 import java.net.InetAddress ; 7 import java.net.MulticastSocket ; 8 9 13 public class LatencyTest { 14 InetAddress GROUP=null; 15 int PORT=7500; 16 17 private void start(boolean sender, boolean local) throws Exception { 18 GROUP=InetAddress.getByName("228.1.2.3"); 19 long start; 20 DatagramPacket send_packet, recv_packet; 21 byte[] send_buf; 22 byte[] recv_buf=new byte[2100]; 23 24 if(local) { 25 MulticastSocket send_sock=new MulticastSocket (PORT); 26 send_sock.setTrafficClass(8); 27 MulticastSocket recv_sock=new MulticastSocket (PORT); 28 recv_sock.joinGroup(GROUP); 29 recv_packet=new DatagramPacket (recv_buf, 0, recv_buf.length); 30 for(int i=0; i < 10; i++) { 31 start=System.currentTimeMillis(); 32 send_buf=Util.objectToByteBuffer(start); 33 send_packet=new DatagramPacket (send_buf, 0, send_buf.length, GROUP, PORT); 34 send_sock.send(send_packet); 35 recv_sock.receive(recv_packet); 36 start=((Long )Util.objectFromByteBuffer(recv_buf, recv_packet.getOffset(), recv_packet.getLength())).longValue(); 37 System.out.println("took " + (System.currentTimeMillis() - start) + " ms"); 38 Util.sleep(1000); 39 } 40 return; 41 } 42 43 if(sender) { 44 MulticastSocket send_sock=new MulticastSocket (PORT); 45 send_sock.setTrafficClass(8); 46 for(int i=0; i < 10; i++) { 47 start=System.currentTimeMillis(); 48 send_buf=Util.objectToByteBuffer(start); 49 send_packet=new DatagramPacket (send_buf, 0, send_buf.length, GROUP, PORT); 50 send_sock.send(send_packet); 51 Util.sleep(1000); 52 } 53 } 54 else { 55 MulticastSocket recv_sock=new MulticastSocket (PORT); 56 recv_sock.joinGroup(GROUP); 57 recv_packet=new DatagramPacket (recv_buf, 0, recv_buf.length); 58 System.out.println("receiver started"); 59 for(;;) { 60 recv_sock.receive(recv_packet); 61 start=((Long )Util.objectFromByteBuffer(recv_buf, recv_packet.getOffset(), recv_packet.getLength())).longValue(); 62 System.out.println("took " + (System.currentTimeMillis() - start) + " ms"); 63 } 64 } 65 } 66 67 68 69 70 71 public static void main(String [] args) throws Exception { 72 boolean sender=false; 73 boolean local=false; 74 for(int i=0; i < args.length; i++) { 75 if(args[i].equalsIgnoreCase("-sender")) { 76 sender=true; 77 continue; 78 } 79 if(args[i].equalsIgnoreCase("-local")) { 80 local=true; 81 continue; 82 } 83 help(); 84 return; 85 } 86 new LatencyTest().start(sender, local); 87 } 88 89 private static void help() { 90 System.out.println("LatencyTest [-sender] [-local (overrides -sender)]"); 91 } 92 93 94 } 95 | Popular Tags |