126 lines
4.1 KiB
Java
126 lines
4.1 KiB
Java
package tv.luma.launcher.tests;
|
|
|
|
import android.app.*;
|
|
import android.content.*;
|
|
import android.graphics.Bitmap;
|
|
import android.os.*;
|
|
import java.lang.reflect.*;
|
|
import java.util.*;
|
|
|
|
/** Measures complete stop/start image recovery, including Java allocation and view reuse. */
|
|
public final class WarmReturnInstrumentation extends Instrumentation {
|
|
private Activity activity;
|
|
|
|
private Object get(Object o, String name) throws Exception {
|
|
Field f = o.getClass().getDeclaredField(name);
|
|
f.setAccessible(true);
|
|
return f.get(o);
|
|
}
|
|
|
|
private void awaitImages() throws Exception {
|
|
long until = SystemClock.uptimeMillis() + 8000;
|
|
boolean[] ready = {false};
|
|
while (SystemClock.uptimeMillis() < until) {
|
|
runOnMainSync(
|
|
() -> {
|
|
try {
|
|
Map<?, ?> tiles = (Map<?, ?>) get(activity, "tiles");
|
|
ready[0] =
|
|
!tiles.isEmpty()
|
|
&& !(boolean) get(activity, "appsNeedRefresh")
|
|
&& get(get(activity, "scene"), "frame") != null;
|
|
for (Object tile : tiles.values()) {
|
|
Bitmap b = (Bitmap) get(get(tile, "art"), "bitmap");
|
|
ready[0] &= b != null && !b.isRecycled();
|
|
}
|
|
} catch (Exception e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
});
|
|
if (ready[0]) {
|
|
waitForIdleSync();
|
|
return;
|
|
}
|
|
Thread.sleep(5);
|
|
}
|
|
throw new AssertionError("images not restored");
|
|
}
|
|
|
|
private long allocated() {
|
|
return Long.parseLong(Debug.getRuntimeStat("art.gc.bytes-allocated"));
|
|
}
|
|
|
|
@Override
|
|
public void onCreate(Bundle b) {
|
|
super.onCreate(b);
|
|
start();
|
|
}
|
|
|
|
@Override
|
|
public void onStart() {
|
|
Bundle result = new Bundle();
|
|
StringBuilder report = new StringBuilder();
|
|
try {
|
|
activity =
|
|
startActivitySync(
|
|
new Intent(Intent.ACTION_MAIN)
|
|
.setComponent(
|
|
new ComponentName("tv.luma.launcher", "tv.luma.launcher.MainActivity"))
|
|
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
|
|
awaitImages();
|
|
Thread.sleep(600);
|
|
List<Long> elapsed = new ArrayList<>(), allocations = new ArrayList<>();
|
|
for (int round = 0; round < 8; round++) {
|
|
Map<?, ?> before = new LinkedHashMap<>((Map<?, ?>) get(activity, "tiles"));
|
|
runOnMainSync(
|
|
() -> {
|
|
callActivityOnPause(activity);
|
|
callActivityOnStop(activity);
|
|
});
|
|
long bytes = allocated(), start = SystemClock.elapsedRealtimeNanos();
|
|
runOnMainSync(
|
|
() -> {
|
|
callActivityOnRestart(activity);
|
|
callActivityOnStart(activity);
|
|
callActivityOnResume(activity);
|
|
});
|
|
awaitImages();
|
|
long ms = (SystemClock.elapsedRealtimeNanos() - start) / 1000000;
|
|
long used = allocated() - bytes;
|
|
Map<?, ?> after = (Map<?, ?>) get(activity, "tiles");
|
|
int reused = 0;
|
|
for (Object key : before.keySet()) if (before.get(key) == after.get(key)) reused++;
|
|
if (round >= 2) {
|
|
elapsed.add(ms);
|
|
allocations.add(used);
|
|
}
|
|
report
|
|
.append("cycle=")
|
|
.append(round)
|
|
.append(" readyMs=")
|
|
.append(ms)
|
|
.append(" javaAllocatedBytes=")
|
|
.append(used)
|
|
.append(" reusedTiles=")
|
|
.append(reused)
|
|
.append('/')
|
|
.append(after.size())
|
|
.append('\n');
|
|
Thread.sleep(350);
|
|
}
|
|
Collections.sort(elapsed);
|
|
Collections.sort(allocations);
|
|
report
|
|
.append("medianReadyMs=")
|
|
.append(elapsed.get(elapsed.size() / 2))
|
|
.append(" medianJavaAllocatedBytes=")
|
|
.append(allocations.get(allocations.size() / 2))
|
|
.append("\nPASS: eight complete warm returns restored all images\n");
|
|
result.putString("stream", report.toString());
|
|
finish(Activity.RESULT_OK, result);
|
|
} catch (Throwable t) {
|
|
result.putString("stream", report + "FAIL: " + t + "\n");
|
|
finish(Activity.RESULT_CANCELED, result);
|
|
}
|
|
}
|
|
}
|