PageRenderTime 67ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/jgroups-2.10.0/tests/other/org/jgroups/tests/UnicastTestTcpRpc.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 482 lines | 328 code | 67 blank | 87 comment | 33 complexity | 0996f305ac139ce68f2b6f5cf6a0891e MD5 | raw file
  1. package org.jgroups.tests;
  2. import org.jgroups.util.Util;
  3. import java.io.*;
  4. import java.net.InetAddress;
  5. import java.net.ServerSocket;
  6. import java.net.Socket;
  7. import java.net.SocketException;
  8. import java.util.concurrent.atomic.AtomicInteger;
  9. import java.util.concurrent.atomic.AtomicLong;
  10. /**
  11. * Tests the UNICAST by invoking unicast RPCs between a sender and a receiver
  12. *
  13. * @author Bela Ban
  14. */
  15. public class UnicastTestTcpRpc {
  16. private ServerSocket srv_sock;
  17. private volatile Socket sock;
  18. private DataInputStream sock_in;
  19. private DataOutputStream sock_out;
  20. private long sleep_time=0;
  21. private boolean exit_on_end=false, busy_sleep=false, sync=false, oob=false;
  22. private int num_threads=1;
  23. private int num_msgs=50000, msg_size=1000;
  24. private InetAddress addr=null;
  25. private int local_port=8000, dest_port=9000;
  26. private boolean started=false;
  27. private long start=0, stop=0;
  28. private AtomicInteger current_value=new AtomicInteger(0);
  29. private int num_values=0, print;
  30. private AtomicLong total_bytes=new AtomicLong(0);
  31. private Thread acceptor;
  32. private final byte[] buf=new byte[65535];
  33. long total_req_time=0, total_rsp_time=0, entire_req_time=0, num_entire_reqs=0;
  34. int num_reqs=0, num_rsps=0;
  35. static final byte START = 0;
  36. static final byte RECEIVE_ASYNC = 1;
  37. static final byte RECEIVE_SYNC = 2;
  38. static final byte ACK = 10;
  39. public void init(long sleep_time, boolean exit_on_end, boolean busy_sleep, boolean sync, boolean oob,
  40. String addr, int local_port, int dest_port) throws Exception {
  41. this.sleep_time=sleep_time;
  42. this.exit_on_end=exit_on_end;
  43. this.busy_sleep=busy_sleep;
  44. this.sync=sync;
  45. this.oob=oob;
  46. this.addr=InetAddress.getByName(addr);
  47. this.local_port=local_port;
  48. this.dest_port=dest_port;
  49. srv_sock=new ServerSocket(local_port);
  50. System.out.println("Listening on " + srv_sock.getLocalSocketAddress());
  51. acceptor=new Thread() {
  52. public void run() {
  53. while(true) {
  54. Socket client_sock=null;
  55. DataInputStream in=null;
  56. DataOutputStream out=null;
  57. try {
  58. client_sock=srv_sock.accept();
  59. set(client_sock);
  60. in=new DataInputStream(client_sock.getInputStream());
  61. out=new DataOutputStream(client_sock.getOutputStream());
  62. if(!handleRequest(in, out)) {
  63. Util.close(client_sock);
  64. Util.close(out);
  65. Util.close(in);
  66. break;
  67. }
  68. }
  69. catch(IOException e) {
  70. Util.close(client_sock);
  71. Util.close(out);
  72. Util.close(in);
  73. break;
  74. }
  75. }
  76. }
  77. };
  78. acceptor.start();
  79. }
  80. void createSocket() throws IOException {
  81. if(sock == null) {
  82. sock=new Socket(addr, dest_port);
  83. set(sock);
  84. sock_in=new DataInputStream(sock.getInputStream());
  85. sock_out=new DataOutputStream(sock.getOutputStream());
  86. }
  87. }
  88. boolean handleRequest(DataInputStream in, DataOutputStream out) throws IOException {
  89. while(true) {
  90. byte type=(byte)in.read();
  91. if(type == -1)
  92. return false;
  93. switch(type) {
  94. case START:
  95. int num=in.readInt();
  96. startTest(num);
  97. break;
  98. case RECEIVE_ASYNC:
  99. case RECEIVE_SYNC:
  100. long val=in.readLong();
  101. int len=in.readInt();
  102. byte data[]=new byte[len];
  103. in.readFully(data, 0, data.length);
  104. receiveData(val, data);
  105. if(type == RECEIVE_SYNC) {
  106. out.writeLong(System.currentTimeMillis());
  107. out.flush();
  108. }
  109. break;
  110. default:
  111. System.err.println("type " + type + " not known");
  112. }
  113. }
  114. }
  115. static void set(Socket socket) throws SocketException {
  116. socket.setTcpNoDelay(true);
  117. socket.setReceiveBufferSize(20000000);
  118. socket.setSendBufferSize(10000000);
  119. }
  120. void stop() {
  121. }
  122. public void startTest(int num_values) {
  123. if(started) {
  124. System.err.println("UnicastTest.run(): received START data, but am already processing data");
  125. }
  126. else {
  127. started=true;
  128. current_value.set(0); // first value to be received
  129. total_bytes.set(0);
  130. this.num_values=num_values;
  131. print=num_values / 10;
  132. total_req_time=0; num_reqs=0; total_rsp_time=0; num_rsps=0; entire_req_time=0;
  133. num_entire_reqs=0;
  134. start=System.currentTimeMillis();
  135. }
  136. }
  137. public void receiveData(long value, byte[] buffer) {
  138. long diff=System.currentTimeMillis() - value;
  139. total_req_time+=diff;
  140. num_reqs++;
  141. long new_val=current_value.incrementAndGet();
  142. total_bytes.addAndGet(buffer.length);
  143. if(print > 0 && new_val % print == 0)
  144. System.out.println("received " + current_value);
  145. if(new_val >= num_values) {
  146. stop=System.currentTimeMillis();
  147. long total_time=stop - start;
  148. long msgs_per_sec=(long)(num_values / (total_time / 1000.0));
  149. double throughput=total_bytes.get() / (total_time / 1000.0);
  150. System.out.println("\n-- received " + num_values + " messages in " + total_time +
  151. " ms (" + msgs_per_sec + " messages/sec, " + Util.printBytes(throughput) + " / sec)");
  152. double time_per_req=(double)total_req_time / num_reqs;
  153. System.out.println("received " + num_reqs + " requests in " + total_req_time + " ms, " + time_per_req +
  154. " ms / req (only requests)\n");
  155. started=false;
  156. if(exit_on_end)
  157. System.exit(0);
  158. }
  159. }
  160. public void eventLoop() throws Throwable {
  161. int c;
  162. while(true) {
  163. System.out.print("[1] Send msgs [2] Print view [3] Print conns " +
  164. "[4] Trash conn [5] Trash all conns" +
  165. "\n[6] Set sender threads (" + num_threads + ") [7] Set num msgs (" + num_msgs + ") " +
  166. "[8] Set msg size (" + Util.printBytes(msg_size) + ")" +
  167. "\n[o] Toggle OOB (" + oob + ") [s] Toggle sync (" + sync + ")" +
  168. "\n[q] Quit\n");
  169. System.out.flush();
  170. c=System.in.read();
  171. switch(c) {
  172. case -1:
  173. break;
  174. case '1':
  175. try {
  176. invokeRpcs();
  177. }
  178. catch(Throwable t) {
  179. System.err.println(t);
  180. }
  181. break;
  182. case '2':
  183. break;
  184. case '3':
  185. break;
  186. case '4':
  187. break;
  188. case '5':
  189. break;
  190. case '6':
  191. setSenderThreads();
  192. break;
  193. case '7':
  194. setNumMessages();
  195. break;
  196. case '8':
  197. setMessageSize();
  198. break;
  199. case 'o':
  200. oob=!oob;
  201. System.out.println("oob=" + oob);
  202. break;
  203. case 's':
  204. sync=!sync;
  205. System.out.println("sync=" + sync);
  206. break;
  207. case 'q':
  208. Util.close(sock);
  209. Util.close(srv_sock);
  210. return;
  211. default:
  212. break;
  213. }
  214. }
  215. }
  216. void invokeRpcs() throws Throwable {
  217. if(sock == null)
  218. createSocket();
  219. if(num_threads > 1 && num_msgs % num_threads != 0) {
  220. System.err.println("num_msgs (" + num_msgs + " ) has to be divisible by num_threads (" + num_threads + ")");
  221. return;
  222. }
  223. System.out.println("invoking " + num_msgs + " RPCs of " + Util.printBytes(msg_size) + " on " +
  224. ", sync=" + sync + ", oob=" + oob);
  225. num_entire_reqs=entire_req_time=total_rsp_time=num_rsps=0;
  226. sock_out.write(START);
  227. sock_out.writeInt(num_msgs);
  228. byte[] data=new byte[msg_size];
  229. byte type=sync? RECEIVE_SYNC : RECEIVE_ASYNC;
  230. for(int i=0; i < num_msgs; i++) {
  231. long tmp_start=System.currentTimeMillis();
  232. sock_out.write(type);
  233. sock_out.writeLong(tmp_start);
  234. sock_out.writeInt(msg_size);
  235. sock_out.write(data, 0, data.length);
  236. // sock_out.flush();
  237. if(sync) {
  238. long timestamp=sock_in.readLong();
  239. long curr_time=System.currentTimeMillis();
  240. long diff=curr_time - tmp_start;
  241. num_entire_reqs++;
  242. entire_req_time+=diff;
  243. diff=curr_time - timestamp;
  244. total_rsp_time+=diff;
  245. num_rsps++;
  246. }
  247. }
  248. sock_out.flush();
  249. System.out.println("done sending " + num_msgs + " to " + sock.getRemoteSocketAddress());
  250. double time_per_req=entire_req_time / (double)num_msgs;
  251. System.out.println("\ninvoked " + num_entire_reqs + " requests in " + entire_req_time + " ms: " + time_per_req +
  252. " ms / req (entire request)");
  253. if(sync) {
  254. double time_per_rsp=total_rsp_time / (double)num_rsps;
  255. System.out.println("received " + num_rsps + " responses in " + total_rsp_time + " ms: " + time_per_rsp +
  256. " ms / rsp (only response)\n");
  257. }
  258. }
  259. void setSenderThreads() throws Exception {
  260. int threads=Util.readIntFromStdin("Number of sender threads: ");
  261. int old=this.num_threads;
  262. this.num_threads=threads;
  263. System.out.println("sender threads set to " + num_threads + " (from " + old + ")");
  264. }
  265. void setNumMessages() throws Exception {
  266. num_msgs=Util.readIntFromStdin("Number of RPCs: ");
  267. System.out.println("Set num_msgs=" + num_msgs);
  268. print=num_msgs / 10;
  269. }
  270. void setMessageSize() throws Exception {
  271. msg_size=Util.readIntFromStdin("Message size: ");
  272. System.out.println("set msg_size=" + msg_size);
  273. }
  274. /* private class Invoker extends Thread {
  275. private final Address destination;
  276. private final RequestOptions options;
  277. private final int number_of_msgs;
  278. long total=0;
  279. public Invoker(Address destination, RequestOptions options, int number_of_msgs) {
  280. this.destination=destination;
  281. this.options=options;
  282. this.number_of_msgs=number_of_msgs;
  283. }
  284. public void run() {
  285. byte[] buf=new byte[msg_size];
  286. Object[] args=new Object[]{0, buf};
  287. MethodCall call=new MethodCall((short)1, args);
  288. for(int i=1; i <= number_of_msgs; i++) {
  289. args[0]=System.currentTimeMillis();
  290. try {
  291. long start=System.currentTimeMillis();
  292. disp.callRemoteMethod(destination, call, options);
  293. long diff=System.currentTimeMillis() - start;
  294. total+=diff;
  295. if(print > 0 && i % print == 0)
  296. System.out.println("-- invoked " + i);
  297. if(sleep_time > 0)
  298. Util.sleep(sleep_time, busy_sleep);
  299. }
  300. catch(Throwable throwable) {
  301. throwable.printStackTrace();
  302. }
  303. }
  304. double time_per_req=total / (double)number_of_msgs;
  305. System.out.println("invoked " + number_of_msgs + " requests in " + total + " ms: " + time_per_req +
  306. " ms / req");
  307. }
  308. }*/
  309. /* static class CustomMarshaller implements RpcDispatcher.Marshaller {
  310. public byte[] objectToByteBuffer(Object obj) throws Exception {
  311. MethodCall call=(MethodCall)obj;
  312. if(call.getId() == 0) {
  313. Integer arg=(Integer)call.getArgs()[0];
  314. ByteBuffer buf=ByteBuffer.allocate(Global.BYTE_SIZE + Global.INT_SIZE);
  315. buf.put((byte)0).putInt(arg);
  316. return buf.array();
  317. }
  318. else if(call.getId() == 1) {
  319. Long arg=(Long)call.getArgs()[0];
  320. byte[] arg2=(byte[])call.getArgs()[1];
  321. ByteBuffer buf=ByteBuffer.allocate(Global.BYTE_SIZE + Global.INT_SIZE + Global.LONG_SIZE + arg2.length);
  322. buf.put((byte)1).putLong(arg).putInt(arg2.length).put(arg2, 0, arg2.length);
  323. return buf.array();
  324. }
  325. else
  326. throw new IllegalStateException("method " + call.getMethod() + " not known");
  327. }
  328. public Object objectFromByteBuffer(byte[] buffer) throws Exception {
  329. ByteBuffer buf=ByteBuffer.wrap(buffer);
  330. byte type=buf.get();
  331. switch(type) {
  332. case 0:
  333. int arg=buf.getInt();
  334. return new MethodCall((short)0, new Object[]{arg});
  335. case 1:
  336. Long longarg=buf.getLong();
  337. int len=buf.getInt();
  338. byte[] arg2=new byte[len];
  339. buf.get(arg2, 0, arg2.length);
  340. return new MethodCall((short)1, new Object[]{longarg, arg2});
  341. default:
  342. throw new IllegalStateException("type " + type + " not known");
  343. }
  344. }
  345. }*/
  346. public static void main(String[] args) {
  347. long sleep_time=0;
  348. boolean exit_on_end=false;
  349. boolean busy_sleep=false;
  350. boolean sync=false;
  351. boolean oob=false;
  352. String addr=null;
  353. int dest_port=9000, local_port=8000;
  354. for(int i=0; i < args.length; i++) {
  355. if("-sleep".equals(args[i])) {
  356. sleep_time=Long.parseLong(args[++i]);
  357. continue;
  358. }
  359. if("-exit_on_end".equals(args[i])) {
  360. exit_on_end=true;
  361. continue;
  362. }
  363. if("-busy_sleep".equals(args[i])) {
  364. busy_sleep=true;
  365. continue;
  366. }
  367. if("-sync".equals(args[i])) {
  368. sync=true;
  369. continue;
  370. }
  371. if("-oob".equals(args[i])) {
  372. oob=true;
  373. continue;
  374. }
  375. if("-addr".equals(args[i])) {
  376. addr=args[++i];
  377. continue;
  378. }
  379. if("-dest_port".equals(args[i])) {
  380. dest_port=Integer.parseInt(args[++i]);
  381. continue;
  382. }
  383. if("-local_port".equals(args[i])) {
  384. local_port=Integer.parseInt(args[++i]);
  385. continue;
  386. }
  387. help();
  388. return;
  389. }
  390. UnicastTestTcpRpc test=null;
  391. try {
  392. test=new UnicastTestTcpRpc();
  393. test.init(sleep_time, exit_on_end, busy_sleep, sync, oob, addr, local_port, dest_port);
  394. test.eventLoop();
  395. }
  396. catch(Throwable ex) {
  397. ex.printStackTrace();
  398. if(test != null)
  399. test.stop();
  400. }
  401. }
  402. static void help() {
  403. System.out.println("UnicastTestRpc [-help] [-sleep <time in ms between msg sends] " +
  404. "[-exit_on_end] [-busy-sleep] [-addr address] [-dest_port port] [-local_port port]");
  405. }
  406. }