padlock-solver/src/test/java/com/cleverthis/interview/PerformanceAnalyze.java

46 lines
1.7 KiB
Java

package com.cleverthis.interview;
import com.cleverthis.interview.padlock.PadlockImpl;
/**
* Performance test but not mean to run in unit test.
*/
public class PerformanceAnalyze {
private static void solve(PadlockAdapter padlock) {
new DumbBruteSolver().solve(padlock);
}
private static final int TOTAL_RUN = 500;
private static final int NUMPAD_SIZE = 9;
static {
System.out.println("Total run: " + TOTAL_RUN);
System.out.println("Numpad size: " + NUMPAD_SIZE);
}
public static void main(String[] args) {
long timeSum = 0;
long writeSum = 0;
for (int i = 0; i < TOTAL_RUN; i++) {
PadlockAdapter padlock = new PadlockJavaAdapter(NUMPAD_SIZE);
padlock.resetCounter();
long start = System.currentTimeMillis();
solve((PadlockAdapter) padlock);
long end = System.currentTimeMillis();
if (!padlock.isPasscodeCorrect()) throw new IllegalStateException(
"Invalid solution: passcode not correct after return");
long dT = end - start;
timeSum += dT;
writeSum += padlock.getWriteCounter();
System.out.println("Run #" + (i + 1) + ": time: " + dT + "ms; write: " + padlock.getWriteCounter());
}
System.out.println("Run time sum: " + timeSum + "ms");
System.out.println("Write sum: " + writeSum);
double avgTime = timeSum / (double) TOTAL_RUN;
double avgWrite = writeSum / (double) TOTAL_RUN;
System.out.println("Avg run time: " + avgTime + "ms");
System.out.println("Avg write: " + avgWrite);
System.out.println("Calculated estimate avg run time: " + (avgTime / 1000 + avgTime) + "s");
}
}