-
-
Save kirillp/77dd5559cabe5941f956153ffe7ce5f0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package delightex.graphicsTest.performance.framework; | |
import delightex.tools.Format; | |
import delightex.tools.time.PerformanceTimer; | |
import java.util.Arrays; | |
import java.util.Comparator; | |
import java.util.Map; | |
import java.util.TreeMap; | |
public class PerformanceFramework { | |
static <T>Summary runIterations(int iterations, PerfTest<T> test, PerformanceTimer timer) { | |
Result[] results = new Result[iterations]; | |
for (int i = 0; i < iterations; i++) { | |
double start = timer.time(); | |
T val = test.run(); | |
double end = timer.time(); | |
results[i] = new Result(end - start, val); | |
} | |
return Summary.fromResults(results); | |
} | |
public static <T>Summary run(double duration, PerfTest<T> test, PerformanceTimer timer) { | |
int count = 0; | |
double stopCount = timer.time() + duration / 2; | |
double time; | |
do { | |
test.run(); | |
count++; | |
time = timer.time(); | |
} while (time < stopCount); | |
// int perSecond = (int)(2 * count / duration); | |
return runIterations(count, test, timer); | |
} | |
public static String runTests(PerfTest<?>[] tests, PerformanceTimer timer, String nameHeader) { | |
return run(tests.length, tests, timer, nameHeader); | |
} | |
public static String run(double duration, PerfTest<?>[] tests, PerformanceTimer timer, String nameHeader) { | |
if (tests.length == 0) { | |
return "empty tests"; | |
} | |
TreeMap<Summary, String> resultToName = new TreeMap<>(sortOrder); | |
int maxNameLength = 0; | |
int maxResultLength = 0; | |
for (PerfTest<?> test : tests) { | |
String name = test.toString(); | |
Summary result = run(duration / tests.length, test, timer); | |
resultToName.put(result, name); | |
maxNameLength = Math.max(maxNameLength, name.length()); | |
maxResultLength = Math.max(maxResultLength, result.toString().length()); | |
} | |
maxNameLength = Math.max(maxNameLength, 50); | |
StringBuilder sb = new StringBuilder(); | |
// String minHeader = "min"; | |
String maxHeader = "max op/sec"; | |
String meanHeader = "mean op/sec"; | |
int resultMaxLength = 20; | |
sb.append(nSpaces(maxNameLength - nameHeader.length())) | |
.append(nameHeader) | |
.append(" | ") | |
// .append(nSpaces(resultMaxLength - minHeader.length())) | |
// .append(minHeader) | |
// .append(" | ") | |
.append(nSpaces(resultMaxLength - maxHeader.length())) | |
.append(maxHeader) | |
.append(" | ") | |
.append(nSpaces(resultMaxLength - meanHeader.length())) | |
.append(meanHeader) | |
.append(" | ") | |
.append("first result") | |
.append("\n"); | |
for (Map.Entry<Summary, String> e : resultToName.entrySet()) { | |
String name = e.getValue(); | |
Summary result = e.getKey(); | |
Object firstResult = result.firstResult; | |
String resultStr = (firstResult instanceof Double) | |
? Integer.toString(((Double) firstResult).intValue()) | |
: firstResult.toString(); | |
sb.append(nSpaces(maxNameLength - name.length())) | |
.append(name) | |
.append(" | ") | |
// .append(nSpaces(resultMaxLength - Format.format3(result.min).length())) | |
// .append(Format.format3(result.min)) | |
// .append(" | ") | |
.append(nSpaces(resultMaxLength - Format.format3(result.max).length())) | |
.append(Format.format3(result.max)) | |
.append(" | ") | |
.append(nSpaces(resultMaxLength - Format.format3(result.mean).length())) | |
.append(Format.format3(result.mean)) | |
.append(" | ") | |
.append(resultStr) | |
.append("\n"); | |
} | |
return sb.toString(); | |
} | |
private static Comparator<Summary> sortOrder = new Comparator<Summary>() { | |
@Override | |
public int compare(Summary o1, Summary o2) { | |
return -Double.compare(o1.max, o2.max); | |
} | |
}; | |
private static char[] nSpaces(int n) { | |
char[] result = new char[n]; | |
Arrays.fill(result, ' '); | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment