/demo/src/main/java/demo/ContinuationThroughputImpl.java

https://github.com/ChiralBehaviors/Prime-Mover · Java · 170 lines · 94 code · 15 blank · 61 comment · 14 complexity · 1fa5b43fe4cbc1b2c665232b3c76f789 MD5 · raw file

  1. /**
  2. * Copyright (C) 2008 Hal Hildebrand. All rights reserved.
  3. *
  4. * This file is part of the Prime Mover Event Driven Simulation Framework.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as
  8. * published by the Free Software Foundation, either version 3 of the
  9. * License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package demo;
  20. import static com.hellblazer.primeMover.Kronos.currentTime;
  21. import static com.hellblazer.primeMover.Kronos.sleep;
  22. import com.hellblazer.primeMover.Blocking;
  23. import com.hellblazer.primeMover.Entity;
  24. /**
  25. *
  26. * @author <a href="mailto:hal.hildebrand@gmail.com">Hal Hildebrand</a>
  27. *
  28. */
  29. @Entity(ContinuationThroughput.class)
  30. public class ContinuationThroughputImpl implements ContinuationThroughput {
  31. /** benchmark type. */
  32. protected final String mode;
  33. /** number of continuation events. */
  34. protected final int nevents;
  35. /** number of warm-up events. */
  36. protected final int nwarm;
  37. /**
  38. * Create new continuation event benchmarking entity.
  39. *
  40. * @param mode
  41. * benchmark type
  42. * @param nevents
  43. * number of continuation events
  44. * @param nwarm
  45. * number of warm-up events
  46. */
  47. public ContinuationThroughputImpl(String mode, int nevents, int nwarm) {
  48. this.mode = mode;
  49. this.nevents = nevents;
  50. this.nwarm = nwarm;
  51. System.out.println(" type: " + mode);
  52. System.out.println(" events: " + nevents);
  53. System.out.println(" warmup: " + nwarm);
  54. }
  55. /**
  56. * Perform single continuation call.
  57. */
  58. protected int call() {
  59. if (mode.equals("NULL")) {
  60. operation_null();
  61. } else if (mode.equals("INT")) {
  62. operation_int(1);
  63. } else if (mode.equals("DOUBLE")) {
  64. operation_double(1.0);
  65. } else if (mode.equals("STRING")) {
  66. operation_string("foo");
  67. } else if (mode.equals("ARRAY")) {
  68. operation_array(new byte[] {});
  69. } else if (mode.equals("SHOW")) {
  70. try {
  71. operation_show();
  72. } catch (RuntimeException e) {
  73. System.out.println("caught exception: " + e);
  74. }
  75. } else {
  76. throw new RuntimeException("unrecognized benchmark: " + mode);
  77. }
  78. return 1;
  79. }
  80. /* (non-Javadoc)
  81. * @see testClasses.ContinuationThroughput#go()
  82. */
  83. @Override
  84. public void go() {
  85. for (int i = 0; i < nwarm; i++) {
  86. sleep(1);
  87. call();
  88. }
  89. System.out.println("benchmark BEGIN");
  90. System.gc();
  91. long startTime = System.nanoTime();
  92. for (int i = 0; i < nevents; i++) {
  93. sleep(1);
  94. call();
  95. }
  96. System.out.println("benchmark END");
  97. long endTime = System.nanoTime();
  98. double duration = (endTime - startTime) / 1000000000.0;
  99. System.out.println("seconds: " + duration);
  100. System.out.println(Math.round(nevents / duration) + " " + mode
  101. + " continuation events/second");
  102. System.out.println();
  103. }
  104. /* (non-Javadoc)
  105. * @see testClasses.ContinuationThroughput#operation_array(byte[])
  106. */
  107. @Override
  108. @Blocking
  109. public byte[] operation_array(byte[] b) {
  110. sleep(1);
  111. return b;
  112. }
  113. /* (non-Javadoc)
  114. * @see testClasses.ContinuationThroughput#operation_double(double)
  115. */
  116. @Override
  117. @Blocking
  118. public void operation_double(double d) {
  119. sleep(1);
  120. }
  121. /* (non-Javadoc)
  122. * @see testClasses.ContinuationThroughput#operation_int(int)
  123. */
  124. @Override
  125. @Blocking
  126. public void operation_int(int i) {
  127. sleep(1);
  128. }
  129. /* (non-Javadoc)
  130. * @see testClasses.ContinuationThroughput#operation_null()
  131. */
  132. @Override
  133. @Blocking
  134. public void operation_null() {
  135. sleep(1);
  136. }
  137. /* (non-Javadoc)
  138. * @see testClasses.ContinuationThroughput#operation_show()
  139. */
  140. @Override
  141. @Blocking
  142. public void operation_show() {
  143. System.out.println("operation_show at t=" + currentTime());
  144. sleep(1);
  145. // throw new RuntimeException("hi");
  146. }
  147. /* (non-Javadoc)
  148. * @see testClasses.ContinuationThroughput#operation_string(java.lang.String)
  149. */
  150. @Override
  151. @Blocking
  152. public void operation_string(String s) {
  153. sleep(1);
  154. }
  155. }