Advertisement
Guest User

Benchmark

a guest
Apr 23rd, 2013
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.05 KB | None | 0 0
  1. import java.io.PrintStream;
  2. import java.util.ArrayList;
  3. import java.util.Comparator;
  4. import java.util.HashMap;
  5. import java.util.LinkedHashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8. import java.util.TreeMap;
  9.  
  10. public class Benchmark {
  11.  
  12.     private static final String STARTING_MAIN_LOOP_ITERATION = "Starting main loop iteration %s / %s...";
  13.     private static final String STARTING_OPERATION = "\tStarting operation '%s'...";
  14.     private static final int DEFAULT_MAIN_LOOP_ITERATIONS = 100;
  15.     private static final int DEFAULT_NESTED_LOOP_ITERATIONS = 1000000;
  16.  
  17.     private final Operation[] operations;
  18.     private int mainLoopIterations;
  19.     private int nestedLoopIterations;
  20.     private PrintStream logger;
  21.     private final Map<String, Performance> performances;
  22.  
  23.     public interface Operation {
  24.  
  25.         void run();
  26.  
  27.         String getId();
  28.  
  29.     }
  30.  
  31.     public static class Performance implements Comparable<Performance> {
  32.  
  33.         private final List<Long> durations;
  34.         private long average;
  35.  
  36.         public Performance() {
  37.             durations = new ArrayList<Long>();
  38.         }
  39.  
  40.         public void add(final Long duration) {
  41.             durations.add(duration);
  42.             int n = durations.size();
  43.             if (n == 1) {
  44.                 average = durations.get(0);
  45.             } else {
  46.                 int sum = 0;
  47.                 for (int i = 0; i < n; i++) {
  48.                     sum += durations.get(i);
  49.                 }
  50.                 average = sum / n;
  51.             }
  52.         }
  53.  
  54.         public List<Long> getDurations() {
  55.             return durations;
  56.         }
  57.  
  58.         public Long getAverage() {
  59.             return average;
  60.         }
  61.  
  62.         @Override
  63.         public int compareTo(final Performance o) {
  64.             return getAverage().compareTo(o.getAverage());
  65.         }
  66.  
  67.         @Override
  68.         public String toString() {
  69.             return average + "ms";
  70.         }
  71.  
  72.     }
  73.  
  74.     public Benchmark(final Operation... operations) {
  75.         this.operations = operations;
  76.         setMainLoopIterations(DEFAULT_MAIN_LOOP_ITERATIONS);
  77.         setNestedLoopIterations(DEFAULT_NESTED_LOOP_ITERATIONS);
  78.         logger = System.out;
  79.         performances = new HashMap<String, Performance>();
  80.         for (final Operation operation : operations) {
  81.             performances.put(operation.getId(), new Performance());
  82.         }
  83.     }
  84.  
  85.     public void setMainLoopIterations(final int mainLoopIterations) {
  86.         this.mainLoopIterations = mainLoopIterations;
  87.     }
  88.  
  89.     public void setNestedLoopIterations(final int nestedLoopIterations) {
  90.         this.nestedLoopIterations = nestedLoopIterations;
  91.     }
  92.  
  93.     public void setLogger(final PrintStream logger) {
  94.         this.logger = logger;
  95.     }
  96.  
  97.     public void run() {
  98.         for (int i = 0; i < mainLoopIterations; i++) {
  99.             logger.println(String.format(STARTING_MAIN_LOOP_ITERATION, (i + 1), mainLoopIterations));
  100.             for (Operation operation : operations) {
  101.                 logger.println(String.format(STARTING_OPERATION, operation.getId()));
  102.                 final long before = System.currentTimeMillis();
  103.                 for (int j = 0; j < nestedLoopIterations; j++) {
  104.                     operation.run();
  105.                 }
  106.                 final long after = System.currentTimeMillis();
  107.                 performances.get(operation.getId()).add(after - before);
  108.             }
  109.         }
  110.     }
  111.  
  112.     public Map<String, Performance> getPerformances() {
  113.         final Comparator<String> valueComparator = new Comparator<String>() {
  114.  
  115.             @Override
  116.             public int compare(final String o1, final String o2) {
  117.                 int result = performances.get(o1).compareTo(performances.get(o2));
  118.                 return result == 0 ? 1 : result;
  119.             }
  120.  
  121.         };
  122.         TreeMap<String, Performance> sortedPerformances = new TreeMap<String, Performance>(valueComparator);
  123.         sortedPerformances.putAll(performances);
  124.         return new LinkedHashMap<String, Performance>(sortedPerformances);
  125.     }
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement