/bench/kilim/bench/LotsOfTasks.java

http://github.com/kilim/kilim · Java · 88 lines · 67 code · 10 blank · 11 comment · 15 complexity · 50bb2ea7889b2fca42b4e3fba219991f 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. // Usage: java kilim.bench.LotsOfTasks -ntasks
  9. // creates ntasks and waits for them to finish
  10. // Or
  11. //Usage: java kilim.bench.LotsOfTasks ntasks pause
  12. // creates ntasks, which in turn block indefinitely on their mailboxes.
  13. public class LotsOfTasks {
  14. static boolean block;
  15. static int nTasks = 100000;
  16. static int nRounds = 10;
  17. public static void main(String[] args) throws Exception {
  18. try {
  19. for (int i = 0; i < args.length; i++) {
  20. String arg = args[i];
  21. if (arg.equalsIgnoreCase("-nRounds")) {
  22. nRounds = Integer.parseInt(args[++i]);
  23. } else if (arg.equalsIgnoreCase("-nTasks")) {
  24. nTasks = Integer.parseInt(args[++i]);
  25. } else if (arg.equalsIgnoreCase("-block")) {
  26. block = true;
  27. }
  28. }
  29. } catch (NumberFormatException e) {
  30. e.printStackTrace();
  31. System.exit(0);
  32. }
  33. System.out.println("kilim.bench.LotsOfTasks -nTasks " + nTasks + (block ? " -block": "") + " -nRounds " + nRounds);
  34. final Stopwatch s = new Stopwatch("Tasks(" + nTasks + ")");
  35. for (int round = 1; round <= nRounds; round++) {
  36. System.out.println("Round #" + round + " ================= ");
  37. s.tick();
  38. final Mailbox<ExitMsg> exitmb = new Mailbox<ExitMsg>();
  39. System.out.println("Creating " + nTasks + (block ? " blocking tasks" : " tasks"));
  40. for (int i = 1; i <= nTasks; i++) {
  41. Task t = new LTask();
  42. t.informOnExit(exitmb);
  43. t.start();
  44. if (i % 100000 == 0) {
  45. System.out.println(" created " + i + " tasks .... (contd.)");
  46. }
  47. }
  48. profilerMark(); // dummy method to study memory consumption at this stage
  49. if (!block) {
  50. System.out.println("Waiting for completion");
  51. for (int i = 1; i <= nTasks; i++) {
  52. exitmb.getb();
  53. if (i % 100000 == 0) {
  54. System.out.println(" " + i + " tasks finished.... (contd.)");
  55. }
  56. }
  57. ;
  58. }
  59. System.out.println("Round #" + round + " done:");
  60. System.out.print(" ");
  61. s.tickPrint(nTasks);
  62. System.gc();
  63. Thread.sleep(100); // give the GC a chance.
  64. }
  65. System.exit(0);
  66. }
  67. public static void profilerMark() {
  68. // dummy method to help as a profiler breakpoint in JProfiler.
  69. }
  70. }
  71. class LTask extends Task {
  72. Mailbox<String> mymb = new Mailbox<String>();
  73. public void execute() throws Pausable {
  74. if (LotsOfTasks.block) {
  75. mymb.get();
  76. }
  77. }
  78. }