001: package org.jgroups.tests;
002:
003: import org.jgroups.util.Util;
004:
005: import java.io.IOException;
006: import java.io.InputStream;
007: import java.io.OutputStream;
008: import java.net.InetAddress;
009: import java.net.ServerSocket;
010: import java.net.Socket;
011:
012: /**
013: * Class that measure RTT between a client and server using ServerSocket/Socket
014: * @author Bela Ban
015: * @version $Id: RoundTripTcp.java,v 1.3 2006/10/11 14:28:55 belaban Exp $
016: */
017: public class RoundTripTcp {
018: ServerSocket srv_sock;
019: InetAddress host;
020: int port = 7500;
021: int num = 1000;
022: int msg_size = 10;
023: boolean server = false;
024: final byte[] RSP_BUF = new byte[] { 1 }; // 1=response
025: int num_responses = 0;
026:
027: private void start(boolean server, int num, int msg_size,
028: InetAddress host, int port) throws IOException {
029: this .server = server;
030: this .num = num;
031: this .msg_size = msg_size;
032: this .host = host;
033: this .port = port;
034: Socket client_sock;
035:
036: if (server) {
037: srv_sock = new ServerSocket(port, 50, host);
038: System.out.println("server started (ctrl-c to kill)");
039: while (true) {
040: client_sock = srv_sock.accept();
041: while (true) {
042: InputStream in = null;
043: OutputStream out = null;
044: try {
045: in = client_sock.getInputStream();
046: out = client_sock.getOutputStream();
047: int b = in.read();
048: out.write(RSP_BUF, 0, RSP_BUF.length);
049: } catch (Exception ex) {
050: // ex.printStackTrace();
051: Util.close(in);
052: Util.close(out);
053: client_sock.close();
054: break;
055: }
056: }
057: }
058: } else {
059: Socket sock = new Socket(host, port);
060: System.out.println("sending " + num + " requests");
061: sendRequests(sock);
062: }
063: }
064:
065: private void sendRequests(Socket sock) throws IOException {
066: byte[] buf = new byte[msg_size];
067: long start, stop, total;
068: double requests_per_sec;
069: double ms_per_req;
070: int print = num / 10;
071:
072: num_responses = 0;
073: for (int i = 0; i < buf.length; i++) {
074: buf[i] = 0; // 0=request
075: }
076:
077: OutputStream out = null;
078: InputStream in = null;
079:
080: try {
081: out = sock.getOutputStream();
082: in = sock.getInputStream();
083: start = System.currentTimeMillis();
084: for (int i = 0; i < num; i++) {
085: try {
086: out.write(buf, 0, buf.length);
087: int b = in.read();
088: num_responses++;
089: if (num_responses >= num) {
090: System.out.println("received all responses ("
091: + num_responses + ")");
092: break;
093: }
094: if (num_responses % print == 0) {
095: System.out.println("- received "
096: + num_responses);
097: }
098: } catch (Exception e) {
099: e.printStackTrace();
100: }
101: }
102: stop = System.currentTimeMillis();
103: total = stop - start;
104: requests_per_sec = num / (total / 1000.0);
105: ms_per_req = total / (double) num;
106: System.out.println("Took " + total + "ms for " + num
107: + " requests: " + requests_per_sec
108: + " requests/sec, " + ms_per_req + " ms/request");
109: } finally {
110: Util.close(in);
111: Util.close(out);
112: sock.close();
113: }
114: }
115:
116: public static void main(String[] args) throws IOException {
117: boolean server = false;
118: int num = 100;
119: int msg_size = 10; // 10 bytes
120: InetAddress host = null;
121: int port = 7500;
122:
123: for (int i = 0; i < args.length; i++) {
124: if (args[i].equals("-num")) {
125: num = Integer.parseInt(args[++i]);
126: continue;
127: }
128: if (args[i].equals("-server")) {
129: server = true;
130: continue;
131: }
132: if (args[i].equals("-size")) {
133: msg_size = Integer.parseInt(args[++i]);
134: continue;
135: }
136: if (args[i].equals("-host")) {
137: host = InetAddress.getByName(args[++i]);
138: continue;
139: }
140: if (args[i].equals("-port")) {
141: port = Integer.parseInt(args[++i]);
142: continue;
143: }
144: RoundTripTcp.help();
145: return;
146: }
147:
148: if (host == null)
149: host = InetAddress.getLocalHost();
150:
151: new RoundTripTcp().start(server, num, msg_size, host, port);
152: }
153:
154: private static void help() {
155: System.out
156: .println("RoundTrip [-server] [-num <number of messages>] "
157: + "[-size <size of each message (in bytes)>] [-host <host>] [-port <port>]");
158: }
159: }
|