KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jgroups.tests;
2
3 import org.jgroups.*;
4 import org.jgroups.util.Util;
5
6 /**
7  * Class that measure RTT between a client and server
8  * @author Bela Ban
9  * @version $Id: RoundTrip.java,v 1.5 2006/08/08 06:28:19 belaban Exp $
10  */

11 public class RoundTrip extends ReceiverAdapter {
12     JChannel channel;
13     String JavaDoc props;
14     int num=1000;
15     int msg_size=10;
16     boolean server=false;
17     final byte[] RSP_BUF=new byte[]{1}; // 1=response
18
int num_responses=0;
19     final Object JavaDoc mutex=new Object JavaDoc(); // to sync sending and reception of a message
20

21
22     private void start(boolean server, int num, int msg_size, String JavaDoc 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     /**
47      * On the server: receive a request, send a response. On the client: send a request, wait for the response
48      * @param msg
49      */

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) { // request
57
if(!server) {// client ignores requests
58
return;
59             }
60             // System.out.println("-- SERVER: received " + num_requests + " requests");
61
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 JavaDoc e) {
67                 e.printStackTrace();
68             }
69         }
70         else { // response
71
synchronized(mutex) {
72                 num_responses++;
73                 // System.out.println("-- SERVER: received " + num_responses + " responses");
74
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; // 0=request
91
}
92
93 /* Address dest;
94         Vector v=new Vector(channel.getView().getMembers());
95         v.remove(channel.getLocalAddress());
96         dest=(Address)v.firstElement();*/

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 JavaDoc 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 JavaDoc[] args) throws ChannelException {
132         boolean server=false;
133         int num=100;
134         int msg_size=10; // 10 bytes
135
String JavaDoc 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