Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.19 KB | None | 0 0
  1. @Retention(RetentionPolicy.RUNTIME)
  2. public @interface MeasureTime {
  3.  
  4. int[] iterations() default {};
  5.  
  6. int[] warmUpIterations() default {};
  7. }
  8.  
  9. @Retention(RetentionPolicy.RUNTIME)
  10. public @interface Benchmark {
  11.  
  12. int iterations() default BenchmarkRunner.DEFAULT_ITERATIONS;
  13.  
  14. int warmUpIterations() default BenchmarkRunner.DEFAULT_WARM_UP_ITERATIONS;
  15. }
  16.  
  17. import microbench.api.annotation.Benchmark;
  18. import microbench.api.annotation.MeasureTime;
  19. import microbench.api.annotation.Prepare;
  20. import microbench.api.annotation.Validate;
  21.  
  22. import java.lang.reflect.InvocationTargetException;
  23. import java.lang.reflect.Method;
  24. import java.util.*;
  25.  
  26. public class BenchmarkRunner {
  27.  
  28. public static final int DEFAULT_ITERATIONS = 1;
  29. public static final int DEFAULT_WARM_UP_ITERATIONS = 0;
  30.  
  31. private final Object target;
  32.  
  33. private final int defaultIterations;
  34. private final int defaultWarmUpIterations;
  35.  
  36. private final List<Method> measureTimeMethods = new ArrayList<>();
  37. private final List<Method> prepareMethods = new ArrayList<>();
  38. private final List<Method> validateMethods = new ArrayList<>();
  39.  
  40. public BenchmarkRunner(Object target) {
  41. this.target = target;
  42. Class<?> clazz = target.getClass();
  43.  
  44. Benchmark annotation = clazz.getAnnotation(Benchmark.class);
  45. if (annotation != null) {
  46. defaultIterations = annotation.iterations();
  47. defaultWarmUpIterations = annotation.warmUpIterations();
  48. } else {
  49. defaultIterations = DEFAULT_ITERATIONS;
  50. defaultWarmUpIterations = DEFAULT_WARM_UP_ITERATIONS;
  51. }
  52.  
  53. for (Method method : clazz.getDeclaredMethods()) {
  54. if (method.getAnnotation(MeasureTime.class) != null) {
  55. measureTimeMethods.add(method);
  56. } else if (method.getAnnotation(Prepare.class) != null) {
  57. prepareMethods.add(method);
  58. } else if (method.getAnnotation(Validate.class) != null) {
  59. validateMethods.add(method);
  60. }
  61. }
  62. Collections.sort(measureTimeMethods, (o1, o2) -> o1.getName().compareTo(o2.getName()));
  63. }
  64.  
  65. public static void run(Object target) {
  66. new BenchmarkRunner(target).run();
  67. }
  68.  
  69. public void run() {
  70. runQuietly();
  71. }
  72.  
  73. private void runQuietly() {
  74. try {
  75. runNormally();
  76. } catch (InvocationTargetException | IllegalAccessException e) {
  77. e.printStackTrace();
  78. }
  79. }
  80.  
  81. private void runNormally() throws InvocationTargetException, IllegalAccessException {
  82. Map<Method, Throwable> validationFailures = new LinkedHashMap<>();
  83.  
  84. for (Method method : measureTimeMethods) {
  85. MeasureTime measureTime = method.getAnnotation(MeasureTime.class);
  86. if (measureTime != null) {
  87. try {
  88. runMeasureTime(target, method, measureTime);
  89. } catch (InvocationTargetException e) {
  90. Throwable cause = e.getCause();
  91. if (cause instanceof AssertionError) {
  92. validationFailures.put(method, cause);
  93. printExecutionFailure(method);
  94. } else {
  95. throw e;
  96. }
  97. }
  98. }
  99. }
  100.  
  101. if (!validationFailures.isEmpty()) {
  102. System.out.println();
  103. for (Map.Entry<Method, Throwable> entry : validationFailures.entrySet()) {
  104. System.out.print("Validation failed while executing " + entry.getKey().getName() + ": ");
  105. System.out.println(entry.getValue());
  106. }
  107. }
  108. }
  109.  
  110. private void invokeMethods(Object instance, List<Method> methods) throws InvocationTargetException, IllegalAccessException {
  111. for (Method method : methods) {
  112. method.invoke(instance);
  113. }
  114. }
  115.  
  116. private void runMeasureTime(Object instance, Method method, MeasureTime measureTime)
  117. throws InvocationTargetException, IllegalAccessException {
  118. for (int i = 0; i < getWarmUpIterations(measureTime); ++i) {
  119. invokeMethods(instance, prepareMethods);
  120. method.invoke(instance);
  121. invokeMethods(instance, validateMethods);
  122. }
  123.  
  124. int iterations = getIterations(measureTime);
  125. long sumDiffs = 0;
  126.  
  127. for (int i = 0; i < iterations; ++i) {
  128. invokeMethods(instance, prepareMethods);
  129. long start = System.nanoTime();
  130. method.invoke(instance);
  131. sumDiffs += System.nanoTime() - start;
  132. invokeMethods(instance, validateMethods);
  133. }
  134. printExecutionResult(method, sumDiffs / iterations);
  135. }
  136.  
  137. private void printExecutionInfo(String message, String ms) {
  138. System.out.println(String.format("%-60s: %10s ms", message, ms));
  139. }
  140.  
  141. private void printExecutionFailure(Method method) {
  142. printExecutionInfo("Validation failed while executing " + method.getName(), "-");
  143. }
  144.  
  145. private void printExecutionResult(Method method, long nanoSeconds) {
  146. printExecutionInfo("Average execution time of " + method.getName(), "" + nanoSeconds / 1_000_000);
  147. }
  148.  
  149. private int getParamValue(int[] values, int defaultValue) {
  150. if (values.length > 0) {
  151. return values[0];
  152. }
  153. return defaultValue;
  154. }
  155.  
  156. private int getWarmUpIterations(MeasureTime measureTime) {
  157. return getParamValue(measureTime.warmUpIterations(), defaultWarmUpIterations);
  158. }
  159.  
  160. private int getIterations(MeasureTime measureTime) {
  161. return getParamValue(measureTime.iterations(), defaultIterations);
  162. }
  163. }
  164.  
  165. public class SimpleSortingDemo {
  166. private List<Integer> shuffledList;
  167.  
  168. public SimpleSortingDemo() {
  169. shuffledList = new ArrayList<>();
  170. for (int i = 0; i < 10000; ++i) {
  171. shuffledList.add(i);
  172. }
  173. Collections.shuffle(shuffledList);
  174. }
  175.  
  176. public static void main(String[] args) {
  177. new BenchmarkRunner(new SimpleSortingDemo()).run();
  178. }
  179.  
  180. @MeasureTime
  181. public void bubbleSort() {
  182. BubbleSort.sort(new ArrayList<Integer>(shuffledList));
  183. }
  184.  
  185. @MeasureTime
  186. public void insertionSort() {
  187. InsertionSort.sort(new ArrayList<Integer>(shuffledList));
  188. }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement