wait until background activity finishes before measuring performance in tests

This commit is contained in:
peter
2017-03-25 07:24:51 +01:00
parent 4cdb3721d5
commit 8837c226c0
2 changed files with 22 additions and 1 deletions
@@ -66,6 +66,12 @@ public class CpuUsageData {
return indent + "GC: " + getGcStats() + "\n" + indent + "Threads: " + getThreadStats() + "\n" + indent + "JIT: " + myCompilationTime + "ms";
}
boolean hasAnyActivityBesides(Thread thread) {
return myCompilationTime > 0 ||
myThreadTimes.stream().anyMatch(pair -> pair.first > 0 && !pair.second.equals(thread.getName())) ||
myGcTimes.stream().anyMatch(pair -> pair.first > 0);
}
@NotNull
private static String printLongestNames(List<Pair<Long, String>> times) {
String stats = StreamEx.of(times)
@@ -583,11 +583,16 @@ public class PlatformTestUtil {
if (COVERAGE_ENABLED_BUILD) return;
Timings.getStatistics(); // warm-up, measure
if (attempts == 1) {
System.gc();
}
while (true) {
attempts--;
CpuUsageData data;
try {
if (setup != null) setup.run();
waitForAllBackgroundActivityToCalmDown();
data = CpuUsageData.measureCpuUsage(test);
}
catch (RuntimeException|Error throwable) {
@@ -691,12 +696,22 @@ public class PlatformTestUtil {
}
}
private static void waitForAllBackgroundActivityToCalmDown() {
for (int i = 0; i < 50; i++) {
CpuUsageData data = CpuUsageData.measureCpuUsage(() -> TimeoutUtil.sleep(100));
if (!data.hasAnyActivityBesides(Thread.currentThread())) {
break;
}
}
}
public static void assertTiming(String message, long expected, @NotNull Runnable actionToMeasure) {
assertTiming(message, expected, 4, actionToMeasure);
}
public static long measure(@NotNull Runnable actionToMeasure) {
private static long measure(@NotNull Runnable actionToMeasure) {
waitForAllBackgroundActivityToCalmDown();
long start = System.currentTimeMillis();
actionToMeasure.run();
long finish = System.currentTimeMillis();