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