Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.PrintStream;
- import java.util.ArrayList;
- import java.util.Comparator;
- import java.util.HashMap;
- import java.util.LinkedHashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.TreeMap;
- public class Benchmark {
- private static final String STARTING_MAIN_LOOP_ITERATION = "Starting main loop iteration %s / %s...";
- private static final String STARTING_OPERATION = "\tStarting operation '%s'...";
- private static final int DEFAULT_MAIN_LOOP_ITERATIONS = 100;
- private static final int DEFAULT_NESTED_LOOP_ITERATIONS = 1000000;
- private final Operation[] operations;
- private int mainLoopIterations;
- private int nestedLoopIterations;
- private PrintStream logger;
- private final Map<String, Performance> performances;
- public interface Operation {
- void run();
- String getId();
- }
- public static class Performance implements Comparable<Performance> {
- private final List<Long> durations;
- private long average;
- public Performance() {
- durations = new ArrayList<Long>();
- }
- public void add(final Long duration) {
- durations.add(duration);
- int n = durations.size();
- if (n == 1) {
- average = durations.get(0);
- } else {
- int sum = 0;
- for (int i = 0; i < n; i++) {
- sum += durations.get(i);
- }
- average = sum / n;
- }
- }
- public List<Long> getDurations() {
- return durations;
- }
- public Long getAverage() {
- return average;
- }
- @Override
- public int compareTo(final Performance o) {
- return getAverage().compareTo(o.getAverage());
- }
- @Override
- public String toString() {
- return average + "ms";
- }
- }
- public Benchmark(final Operation... operations) {
- this.operations = operations;
- setMainLoopIterations(DEFAULT_MAIN_LOOP_ITERATIONS);
- setNestedLoopIterations(DEFAULT_NESTED_LOOP_ITERATIONS);
- logger = System.out;
- performances = new HashMap<String, Performance>();
- for (final Operation operation : operations) {
- performances.put(operation.getId(), new Performance());
- }
- }
- public void setMainLoopIterations(final int mainLoopIterations) {
- this.mainLoopIterations = mainLoopIterations;
- }
- public void setNestedLoopIterations(final int nestedLoopIterations) {
- this.nestedLoopIterations = nestedLoopIterations;
- }
- public void setLogger(final PrintStream logger) {
- this.logger = logger;
- }
- public void run() {
- for (int i = 0; i < mainLoopIterations; i++) {
- logger.println(String.format(STARTING_MAIN_LOOP_ITERATION, (i + 1), mainLoopIterations));
- for (Operation operation : operations) {
- logger.println(String.format(STARTING_OPERATION, operation.getId()));
- final long before = System.currentTimeMillis();
- for (int j = 0; j < nestedLoopIterations; j++) {
- operation.run();
- }
- final long after = System.currentTimeMillis();
- performances.get(operation.getId()).add(after - before);
- }
- }
- }
- public Map<String, Performance> getPerformances() {
- final Comparator<String> valueComparator = new Comparator<String>() {
- @Override
- public int compare(final String o1, final String o2) {
- int result = performances.get(o1).compareTo(performances.get(o2));
- return result == 0 ? 1 : result;
- }
- };
- TreeMap<String, Performance> sortedPerformances = new TreeMap<String, Performance>(valueComparator);
- sortedPerformances.putAll(performances);
- return new LinkedHashMap<String, Performance>(sortedPerformances);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement