/bench/kilim/bench/PingPong.java

http://github.com/kilim/kilim · Java · 104 lines · 79 code · 11 blank · 14 comment · 4 complexity · 75ef1d22a8afe0c208bb1118add1b455 MD5 · raw file

  1. /* Copyright (c) 2006, Sriram Srinivasan
  2. *
  3. * You may distribute this software under the terms of the license
  4. * specified in the file "License"
  5. */
  6. package kilim.bench;
  7. import kilim.Mailbox;
  8. import kilim.Pausable;
  9. import kilim.Task;
  10. /*
  11. * This code is unnecessarily long for what it aims to do: bounce a message back
  12. * and forth between two tasks for a certain number of times. The reason for the
  13. * length is to compare against a similar example in the scala distribution.
  14. */
  15. public class PingPong {
  16. public static void main(String[] args) {
  17. Mailbox<Msg> pingmb = new Mailbox<Msg>();
  18. Mailbox<Msg> pongmb = new Mailbox<Msg>();
  19. new Ping(pingmb, pongmb).start();
  20. new Pong(pongmb).start();
  21. pingmb.putnb(new Msg(MsgType.Init, 100000, null));
  22. pingmb.putnb(new Msg(MsgType.Start, pingmb));
  23. }
  24. }
  25. class Ping extends Task {
  26. Mailbox<Msg> mymb;
  27. Mailbox<Msg> pongmb;
  28. int origcount;
  29. int count;
  30. long beginTime;
  31. Ping(Mailbox<Msg> mb, Mailbox<Msg> pong) {
  32. mymb = mb;
  33. pongmb = pong;
  34. }
  35. public void execute() throws Pausable {
  36. while (true) {
  37. Msg m = mymb.get();
  38. switch(m.type) {
  39. case Init:
  40. origcount = count = m.count;
  41. beginTime = System.currentTimeMillis();
  42. break;
  43. case Start:
  44. case PongMsg:
  45. if (count > 0) {
  46. // System.out.println("Ping: " + m + " " + count); System.out.flush();
  47. // pongmb.put(new Msg(MsgType.PingMsg, mymb));
  48. m.type = MsgType.PingMsg;
  49. pongmb.put(m);
  50. count--;
  51. } else {
  52. long elapsed = System.currentTimeMillis() - beginTime;
  53. System.out.println("Total time: " + elapsed + " millis, "
  54. + origcount + " rounds");
  55. System.out.println("Time to send msg + context switch: " +
  56. (elapsed * 1000.0 / 2 / origcount) + " micros");
  57. System.exit(0);
  58. }
  59. break;
  60. }
  61. }
  62. }
  63. }
  64. class Pong extends Task {
  65. Mailbox<Msg> mymb;
  66. Pong(Mailbox<Msg> mb) {
  67. mymb = mb;
  68. }
  69. public void execute() throws Pausable {
  70. while (true) {
  71. Msg m = mymb.get();
  72. // System.out.println("Pong: " + m); System.out.flush();
  73. switch(m.type) {
  74. case PingMsg:
  75. // m.mb.put(new Msg(MsgType.PongMsg, null));
  76. m.type = MsgType.PongMsg;
  77. m.mb.put(m);
  78. break;
  79. }
  80. }
  81. }
  82. }
  83. enum MsgType {Init, Start, PingMsg, PongMsg};
  84. class Msg {
  85. MsgType type;
  86. Mailbox<Msg> mb;
  87. int count; // for init
  88. Msg(MsgType t, int c, Mailbox<Msg> amb) {type = t; mb = amb; count = c;}
  89. Msg(MsgType t, Mailbox<Msg> amb) {type = t; mb = amb;}
  90. public String toString() {
  91. return "" + System.identityHashCode(this) + " " + type;
  92. }
  93. }