/bench/kilim/bench/Chain.java

http://github.com/kilim/kilim · Java · 98 lines · 73 code · 15 blank · 10 comment · 13 complexity · 762620c7a40a75236449b0cfb0f767c3 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.*;
  8. public class Chain extends Task {
  9. static class Mbx extends Mailbox<Integer>{}
  10. static int nTasks = 500;
  11. static int nMsgs = 10000;
  12. Mbx mymb, nextmb;
  13. static long startTime;
  14. static Mailbox<Integer> signalMbx = new Mailbox<Integer>();
  15. public static void main(String[] args) {
  16. // Scheduler.setDefaultScheduler(new Scheduler(2)); // 2 threads.
  17. try {
  18. for (int i = 0; i < args.length; i++) {
  19. String arg = args[i];
  20. if (arg.equalsIgnoreCase("-nMsgs")) {
  21. nMsgs = Integer.parseInt(args[++i]);
  22. } else if (arg.equalsIgnoreCase("-nTasks")) {
  23. nTasks = Integer.parseInt(args[++i]);
  24. }
  25. }
  26. }
  27. catch (NumberFormatException e) {
  28. System.err.println("Integer argument expected");
  29. }
  30. System.out.println("Num tasks in chain: " + nTasks + ". Num messages sent:" + nMsgs);
  31. for (int i = 0; i < 5; i++) {
  32. bench(nMsgs, nTasks);
  33. }
  34. System.exit(0);
  35. }
  36. static void bench(int nMsgs, int nTasks) {
  37. startTime = System.currentTimeMillis();
  38. Mbx mb = new Mbx();
  39. Mbx nextmb = null;
  40. // Create a chain of tasks.
  41. for (int i = 0; i < nTasks; i++) {
  42. Task t = new Chain(mb, nextmb);
  43. t.start();
  44. nextmb = mb;
  45. mb = new Mbx();
  46. }
  47. for (int i = 0; i < nMsgs; i++) {
  48. nextmb.putnb(0); // enqueue a message for the head of the chain.
  49. }
  50. signalMbx.getb();
  51. System.out.println("Bench finished");
  52. try {Thread.sleep(500);}catch (Exception ignore) {}
  53. System.gc();
  54. }
  55. public Chain(Mbx mb, Mbx next) {
  56. mymb = mb;
  57. nextmb = next;
  58. }
  59. int numReceived = 0;
  60. public void execute() throws Pausable {
  61. while (true) {
  62. // System.out.println("Waiting: # " + id());
  63. Integer val = mymb.get();
  64. // System.out.println("GET ===== # " + id());
  65. if (nextmb == null) {
  66. numReceived++;
  67. if (numReceived == nMsgs) {
  68. done();
  69. break;
  70. }
  71. } else {
  72. nextmb.put(val);
  73. }
  74. }
  75. }
  76. public void done() {
  77. System.out.println("Elapsed time: " +
  78. (System.currentTimeMillis() - startTime)
  79. + " ms ");
  80. signalMbx.putnb(0);
  81. // System.exit(0);
  82. }
  83. }