KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > tests > LatencyTest


1 package org.jgroups.tests;
2
3 import org.jgroups.util.Util;
4
5 import java.net.DatagramPacket JavaDoc;
6 import java.net.InetAddress JavaDoc;
7 import java.net.MulticastSocket JavaDoc;
8
9 /**
10  * @author Bela Ban
11  * @version $Id: LatencyTest.java,v 1.4 2007/05/04 12:51:57 belaban Exp $
12  */

13 public class LatencyTest {
14     InetAddress JavaDoc GROUP=null;
15     int PORT=7500;
16
17     private void start(boolean sender, boolean local) throws Exception JavaDoc {
18         GROUP=InetAddress.getByName("228.1.2.3");
19         long start;
20         DatagramPacket JavaDoc send_packet, recv_packet;
21         byte[] send_buf;
22         byte[] recv_buf=new byte[2100];
23
24         if(local) {
25             MulticastSocket JavaDoc send_sock=new MulticastSocket JavaDoc(PORT);
26             send_sock.setTrafficClass(8);
27             MulticastSocket JavaDoc recv_sock=new MulticastSocket JavaDoc(PORT);
28             recv_sock.joinGroup(GROUP);
29             recv_packet=new DatagramPacket JavaDoc(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 JavaDoc(send_buf, 0, send_buf.length, GROUP, PORT);
34                 send_sock.send(send_packet);
35                 recv_sock.receive(recv_packet);
36                 start=((Long JavaDoc)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 JavaDoc send_sock=new MulticastSocket JavaDoc(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 JavaDoc(send_buf, 0, send_buf.length, GROUP, PORT);
50                 send_sock.send(send_packet);
51                 Util.sleep(1000);
52             }
53         }
54         else {
55             MulticastSocket JavaDoc recv_sock=new MulticastSocket JavaDoc(PORT);
56             recv_sock.joinGroup(GROUP);
57             recv_packet=new DatagramPacket JavaDoc(recv_buf, 0, recv_buf.length);
58             System.out.println("receiver started");
59             for(;;) {
60                 recv_sock.receive(recv_packet);
61                 start=((Long JavaDoc)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 JavaDoc[] args) throws Exception JavaDoc {
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