001: package org.jgroups.tests;
002:
003: import org.jgroups.*;
004: import org.jgroups.util.Util;
005:
006: /**
007: * Class that measure RTT between a client and server
008: * @author Bela Ban
009: * @version $Id: RoundTrip.java,v 1.5 2006/08/08 06:28:19 belaban Exp $
010: */
011: public class RoundTrip extends ReceiverAdapter {
012: JChannel channel;
013: String props;
014: int num = 1000;
015: int msg_size = 10;
016: boolean server = false;
017: final byte[] RSP_BUF = new byte[] { 1 }; // 1=response
018: int num_responses = 0;
019: final Object mutex = new Object(); // to sync sending and reception of a message
020:
021: private void start(boolean server, int num, int msg_size,
022: String props) throws ChannelException {
023: this .server = server;
024: this .num = num;
025: this .msg_size = msg_size;
026: this .props = props;
027:
028: channel = new JChannel(props);
029: channel.setReceiver(this );
030: channel.connect("rt");
031:
032: if (server) {
033: System.out.println("server started (ctrl-c to kill)");
034: while (true) {
035: Util.sleep(60000);
036: }
037: } else {
038: channel.setOpt(Channel.LOCAL, Boolean.FALSE);
039: System.out.println("sending " + num + " requests");
040: sendRequests();
041: channel.close();
042: }
043: }
044:
045: /**
046: * On the server: receive a request, send a response. On the client: send a request, wait for the response
047: * @param msg
048: */
049: public void receive(Message msg) {
050: byte[] buf = msg.getRawBuffer();
051: if (buf == null) {
052: System.err.println("buffer was null !");
053: return;
054: }
055: if (buf[0] == 0) { // request
056: if (!server) {// client ignores requests
057: return;
058: }
059: // System.out.println("-- SERVER: received " + num_requests + " requests");
060: Message response = new Message(msg.getSrc(), null, null);
061: response.setBuffer(RSP_BUF, 0, RSP_BUF.length);
062: try {
063: channel.send(response);
064: } catch (Exception e) {
065: e.printStackTrace();
066: }
067: } else { // response
068: synchronized (mutex) {
069: num_responses++;
070: // System.out.println("-- SERVER: received " + num_responses + " responses");
071: mutex.notify();
072: }
073: }
074: }
075:
076: private void sendRequests() {
077: byte[] buf = new byte[msg_size];
078: long start, stop, total;
079: double requests_per_sec;
080: double ms_per_req;
081: Message msg;
082: int print = num / 10;
083: int count = 0;
084:
085: num_responses = 0;
086: for (int i = 0; i < buf.length; i++) {
087: buf[i] = 0; // 0=request
088: }
089:
090: /* Address dest;
091: Vector v=new Vector(channel.getView().getMembers());
092: v.remove(channel.getLocalAddress());
093: dest=(Address)v.firstElement();*/
094:
095: start = System.currentTimeMillis();
096: for (int i = 0; i < num; i++) {
097: msg = new Message(null, null, null);
098: msg.setBuffer(buf);
099: try {
100: channel.send(msg);
101: synchronized (mutex) {
102: while (num_responses != count + 1) {
103: mutex.wait(1000);
104: }
105: count = num_responses;
106: if (num_responses >= num) {
107: System.out.println("received all responses ("
108: + num_responses + ")");
109: break;
110: }
111: }
112: if (num_responses % print == 0) {
113: System.out.println("- received " + num_responses);
114: }
115: } catch (Exception e) {
116: e.printStackTrace();
117: }
118: }
119: stop = System.currentTimeMillis();
120: total = stop - start;
121: requests_per_sec = num / (total / 1000.0);
122: ms_per_req = total / (double) num;
123: System.out.println("Took " + total + "ms for " + num
124: + " requests: " + requests_per_sec + " requests/sec, "
125: + ms_per_req + " ms/request");
126: }
127:
128: public static void main(String[] args) throws ChannelException {
129: boolean server = false;
130: int num = 100;
131: int msg_size = 10; // 10 bytes
132: String props = null;
133:
134: for (int i = 0; i < args.length; i++) {
135: if (args[i].equals("-num")) {
136: num = Integer.parseInt(args[++i]);
137: continue;
138: }
139: if (args[i].equals("-server")) {
140: server = true;
141: continue;
142: }
143: if (args[i].equals("-size")) {
144: msg_size = Integer.parseInt(args[++i]);
145: continue;
146: }
147: if (args[i].equals("-props")) {
148: props = args[++i];
149: continue;
150: }
151: help();
152: return;
153: }
154: new RoundTrip().start(server, num, msg_size, props);
155: }
156:
157: private static void help() {
158: System.out
159: .println("RoundTrip [-server] [-num <number of messages>] "
160: + "[-size <size of each message (in bytes)>] [-props <properties>]");
161: }
162: }
|