001: package org.jgroups.tests;
002:
003: import org.jgroups.*;
004: import org.jgroups.blocks.GroupRequest;
005: import org.jgroups.blocks.MethodCall;
006: import org.jgroups.blocks.MethodLookup;
007: import org.jgroups.blocks.RpcDispatcher;
008: import org.jgroups.util.Util;
009:
010: import java.lang.reflect.Method;
011:
012: /**
013: * Interactive test for measuring group RPCs using different invocation techniques.
014: * @author Bela Ban
015: * @version $Revision: 1.13.2.1 $
016: */
017: public class RpcDispatcherSpeedTest implements MembershipListener {
018: Channel channel;
019: RpcDispatcher disp;
020: String props = null;
021: boolean server = false; // role is client by default
022: int num = 1000;
023: int mode = OLD;
024: static final int OLD = 1;
025: static final int METHOD = 2;
026: static final int TYPES = 3;
027: static final int SIGNATURE = 4;
028: static final int ID = 5;
029: static final long TIMEOUT = 10000;
030: static final Class LONG_CLASS = long.class;
031: static final String LONG = long.class.getName();
032: final Method[] METHODS = new Method[1];
033: final Object[] EMPTY_OBJECT_ARRAY = new Object[] {};
034: final Class[] EMPTY_CLASS_ARRAY = new Class[] {};
035: final String[] EMPTY_STRING_ARRAY = new String[] {};
036:
037: public RpcDispatcherSpeedTest(String props, boolean server,
038: int num, int mode) throws NoSuchMethodException {
039: this .props = props;
040: this .server = server;
041: this .num = num;
042: this .mode = mode;
043: initMethods();
044: }
045:
046: final void initMethods() throws NoSuchMethodException {
047: Class cl = this .getClass();
048: METHODS[0] = cl.getMethod("measure", null);
049: }
050:
051: public long measure() throws Exception {
052: return System.currentTimeMillis();
053: }
054:
055: public void start() throws Exception {
056: channel = new JChannel(props);
057: channel.setOpt(Channel.LOCAL, Boolean.FALSE);
058: disp = new RpcDispatcher(channel, null, this , this , false, // no deadlock detection
059: false); // no concurrent processing on incoming method calls
060: // disp.setConcurrentProcessing(true);
061:
062: disp.setMethodLookup(new MethodLookup() {
063:
064: public Method findMethod(short id) {
065: return METHODS[0];
066: }
067: });
068:
069: channel.connect("RpcDispatcherSpeedTestGroup");
070:
071: try {
072: if (server) {
073: System.out
074: .println("-- Started as server. Press ctrl-c to kill");
075: while (true) {
076: Util.sleep(10000);
077: }
078: } else {
079: invokeRpcs(num, mode);
080: }
081: } catch (Throwable t) {
082: t.printStackTrace(System.err);
083: } finally {
084: channel.close();
085: disp.stop();
086: }
087: }
088:
089: void invokeRpcs(int num, int mode) throws Exception {
090: long start, stop;
091: int show = num / 10;
092: Method measure_method = getClass().getMethod("measure",
093: EMPTY_CLASS_ARRAY);
094: MethodCall measure_method_call = new MethodCall(measure_method,
095: EMPTY_OBJECT_ARRAY);
096:
097: if (show <= 0)
098: show = 1;
099: start = System.currentTimeMillis();
100: switch (mode) {
101: case OLD:
102: System.out.println("-- invoking " + num
103: + " methods using mode=OLD");
104: for (int i = 1; i <= num; i++) {
105: disp.callRemoteMethods(null, "measure",
106: EMPTY_OBJECT_ARRAY, EMPTY_CLASS_ARRAY,
107: GroupRequest.GET_ALL, TIMEOUT);
108: if (i % show == 0)
109: System.out.println(i);
110: }
111: break;
112:
113: case METHOD:
114: System.out.println("-- invoking " + num
115: + " methods using mode=METHOD");
116: for (int i = 1; i <= num; i++) {
117: disp.callRemoteMethods(null, measure_method_call,
118: GroupRequest.GET_ALL, TIMEOUT);
119: if (i % show == 0)
120: System.out.println(i);
121: }
122: break;
123:
124: case TYPES:
125: System.out.println("-- invoking " + num
126: + " methods using mode=TYPES");
127: for (int i = 1; i <= num; i++) {
128: disp.callRemoteMethods(null, "measure",
129: EMPTY_OBJECT_ARRAY, EMPTY_CLASS_ARRAY,
130: GroupRequest.GET_ALL, TIMEOUT);
131: if (i % show == 0)
132: System.out.println(i);
133: }
134: break;
135:
136: case SIGNATURE:
137: System.out.println("-- invoking " + num
138: + " methods using mode=SIGNATURE");
139: for (int i = 1; i <= num; i++) {
140: disp.callRemoteMethods(null, "measure",
141: EMPTY_OBJECT_ARRAY, EMPTY_STRING_ARRAY,
142: GroupRequest.GET_ALL, TIMEOUT);
143: if (i % show == 0)
144: System.out.println(i);
145: }
146: break;
147: case ID:
148: System.out.println("-- invoking " + num
149: + " methods using mode=ID");
150: measure_method_call = new MethodCall((short) 0, null);
151: for (int i = 1; i <= num; i++) {
152: disp.callRemoteMethods(null, measure_method_call,
153: GroupRequest.GET_ALL, TIMEOUT);
154: if (i % show == 0)
155: System.out.println(i);
156: }
157: break;
158: default:
159: break;
160: }
161: stop = System.currentTimeMillis();
162: printStats(stop - start, num);
163: }
164:
165: void printStats(long total_time, int num) {
166: double throughput = ((double) num)
167: / ((double) total_time / 1000.0);
168: System.out.println("time for " + num + " remote calls was "
169: + total_time + ", avg=" + (total_time / (double) num)
170: + "ms/invocation, " + (long) throughput + " calls/sec");
171: }
172:
173: public void viewAccepted(View new_view) {
174: System.out.println("-- new view: " + new_view);
175: }
176:
177: public void suspect(Address suspected_mbr) {
178: ;
179: }
180:
181: public void block() {
182: ;
183: }
184:
185: public static void main(String[] args) {
186: String props = null;
187: boolean server = false;
188: int num = 1000;
189: RpcDispatcherSpeedTest test;
190: int mode = OLD;
191:
192: for (int i = 0; i < args.length; i++) {
193: if ("-props".equals(args[i])) {
194: props = args[++i];
195: continue;
196: }
197: if ("-server".equals(args[i])) {
198: server = true;
199: continue;
200: }
201: if ("-num".equals(args[i])) {
202: num = Integer.parseInt(args[++i]);
203: continue;
204: }
205: if ("-mode".equals(args[i])) {
206: String m = args[++i].toLowerCase().trim();
207: if ("old".equals(m))
208: mode = OLD;
209: else if ("method".equals(m))
210: mode = METHOD;
211: else if ("types".equals(m))
212: mode = TYPES;
213: else if ("signature".equals(m))
214: mode = SIGNATURE;
215: else if ("ID".equals(m) || "id".equals(m)) {
216: mode = ID;
217: } else {
218: System.err.println("mode " + m + " is invalid");
219: help();
220: return;
221: }
222: continue;
223: }
224: help();
225: return;
226: }
227:
228: try {
229: test = new RpcDispatcherSpeedTest(props, server, num, mode);
230: test.start();
231: } catch (Exception e) {
232: e.printStackTrace();
233: }
234: }
235:
236: static void help() {
237: System.out
238: .println("RpcDispatcherSpeedTest [-help] [-props <props>] "
239: + "[-server] [-num <number of calls>] [-mode <mode>]");
240: System.out
241: .println("mode can be either 'old', 'method', 'types', signature' or 'id'");
242: }
243: }
|