KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jgroups.tests;
2
3 import org.jgroups.util.Util;
4
5 import java.io.IOException JavaDoc;
6 import java.io.InputStream JavaDoc;
7 import java.io.OutputStream JavaDoc;
8 import java.net.InetAddress JavaDoc;
9 import java.net.ServerSocket JavaDoc;
10 import java.net.Socket JavaDoc;
11
12 /**
13  * Class that measure RTT between a client and server using ServerSocket/Socket
14  * @author Bela Ban
15  * @version $Id: RoundTripTcp.java,v 1.3 2006/10/11 14:28:55 belaban Exp $
16  */

17 public class RoundTripTcp {
18     ServerSocket JavaDoc srv_sock;
19     InetAddress JavaDoc host;
20     int port=7500;
21     int num=1000;
22     int msg_size=10;
23     boolean server=false;
24     final byte[] RSP_BUF=new byte[]{1}; // 1=response
25
int num_responses=0;
26
27
28     private void start(boolean server, int num, int msg_size, InetAddress JavaDoc host, int port) throws IOException JavaDoc {
29         this.server=server;
30         this.num=num;
31         this.msg_size=msg_size;
32         this.host=host;
33         this.port=port;
34         Socket JavaDoc client_sock;
35
36         if(server) {
37             srv_sock=new ServerSocket JavaDoc(port, 50, host);
38             System.out.println("server started (ctrl-c to kill)");
39             while(true) {
40                 client_sock=srv_sock.accept();
41                 while(true) {
42                     InputStream JavaDoc in=null;
43                     OutputStream JavaDoc out=null;
44                     try {
45                         in=client_sock.getInputStream();
46                         out=client_sock.getOutputStream();
47                         int b=in.read();
48                         out.write(RSP_BUF, 0, RSP_BUF.length);
49                     }
50                     catch(Exception JavaDoc ex) {
51                         // ex.printStackTrace();
52
Util.close(in);
53                         Util.close(out);
54                         client_sock.close();
55                         break;
56                     }
57                 }
58             }
59         }
60         else {
61             Socket JavaDoc sock=new Socket JavaDoc(host, port);
62             System.out.println("sending " + num + " requests");
63             sendRequests(sock);
64         }
65     }
66
67
68
69     private void sendRequests(Socket JavaDoc sock) throws IOException JavaDoc {
70         byte[] buf=new byte[msg_size];
71         long start, stop, total;
72         double requests_per_sec;
73         double ms_per_req;
74         int print=num / 10;
75
76         num_responses=0;
77         for(int i=0; i < buf.length; i++) {
78             buf[i]=0; // 0=request
79
}
80
81
82         OutputStream JavaDoc out=null;
83         InputStream JavaDoc in=null;
84
85         try {
86             out=sock.getOutputStream();
87             in=sock.getInputStream();
88             start=System.currentTimeMillis();
89             for(int i=0; i < num; i++) {
90                 try {
91                     out.write(buf, 0, buf.length);
92                     int b=in.read();
93                     num_responses++;
94                     if(num_responses >= num) {
95                         System.out.println("received all responses (" + num_responses + ")");
96                         break;
97                     }
98                     if(num_responses % print == 0) {
99                         System.out.println("- received " + num_responses);
100                     }
101                 }
102                 catch(Exception JavaDoc e) {
103                     e.printStackTrace();
104                 }
105             }
106             stop=System.currentTimeMillis();
107             total=stop-start;
108             requests_per_sec=num / (total / 1000.0);
109             ms_per_req=total / (double)num;
110             System.out.println("Took " + total + "ms for " + num + " requests: " + requests_per_sec +
111                     " requests/sec, " + ms_per_req + " ms/request");
112         }
113         finally {
114             Util.close(in);
115             Util.close(out);
116             sock.close();
117         }
118     }
119
120
121     public static void main(String JavaDoc[] args) throws IOException JavaDoc {
122         boolean server=false;
123         int num=100;
124         int msg_size=10; // 10 bytes
125
InetAddress JavaDoc host=null;
126         int port=7500;
127
128         for(int i=0; i < args.length; i++) {
129             if(args[i].equals("-num")) {
130                 num=Integer.parseInt(args[++i]);
131                 continue;
132             }
133             if(args[i].equals("-server")) {
134                 server=true;
135                 continue;
136             }
137             if(args[i].equals("-size")) {
138                 msg_size=Integer.parseInt(args[++i]);
139                 continue;
140             }
141             if(args[i].equals("-host")) {
142                 host=InetAddress.getByName(args[++i]);
143                 continue;
144             }
145             if(args[i].equals("-port")) {
146                 port=Integer.parseInt(args[++i]);
147                 continue;
148             }
149             RoundTripTcp.help();
150             return;
151         }
152
153         if(host == null)
154             host=InetAddress.getLocalHost();
155
156         new RoundTripTcp().start(server, num, msg_size, host, port);
157     }
158
159
160
161     private static void help() {
162         System.out.println("RoundTrip [-server] [-num <number of messages>] " +
163                 "[-size <size of each message (in bytes)>] [-host <host>] [-port <port>]");
164     }
165 }
166
Popular Tags