Commit 6540ed59 authored by Changwan Ryu's avatar Changwan Ryu Committed by Commit Bot

Parameterize startup UI measurements

Extend the StartupTimeActivity to handle different intent options
to measure different startup scenarios.

Bug: 1055970
Change-Id: Ib0616ee40cc4d2833ab69f994faedf9abf6345c0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2068646Reviewed-by: default avatarTao Bai <michaelbai@chromium.org>
Reviewed-by: default avatarRichard Coles <torne@chromium.org>
Commit-Queue: Changwan Ryu <changwan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#745490}
parent 52fae3f4
...@@ -5,15 +5,19 @@ ...@@ -5,15 +5,19 @@
package org.chromium.webview_shell; package org.chromium.webview_shell;
import android.app.Activity; import android.app.Activity;
import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.Process; import android.text.TextUtils;
import android.os.SystemClock; import android.webkit.WebSettings;
import android.webkit.WebView; import android.webkit.WebView;
import android.widget.LinearLayout;
import androidx.annotation.IntDef;
import org.chromium.base.Log; import org.chromium.base.Log;
import java.util.concurrent.LinkedBlockingQueue; import java.util.LinkedList;
/** /**
* This activity is designed for startup time testing of the WebView. * This activity is designed for startup time testing of the WebView.
...@@ -24,12 +28,17 @@ public class StartupTimeActivity extends Activity { ...@@ -24,12 +28,17 @@ public class StartupTimeActivity extends Activity {
private static final long MIN_TIME_TO_RECORD_MS = 33; private static final long MIN_TIME_TO_RECORD_MS = 33;
private static final long TIME_TO_FINISH_APP_MS = 5000; private static final long TIME_TO_FINISH_APP_MS = 5000;
private LinkedBlockingQueue<Long> mEventQueue = new LinkedBlockingQueue<>(); private static final String TARGET_KEY = "target";
private LinkedList<Long> mEventQueue = new LinkedList<>();
private boolean mFinished; private boolean mFinished;
// Keep track of the time that the last task was run. // Keep track of the time that the last task was run.
private long mLastTaskTimeMs = -1; private long mLastTaskTimeMs = -1;
private LinearLayout mLayout;
private WebView mWebView;
private Handler mHandler; private Handler mHandler;
private Runnable mUiBlockingTaskTracker = new Runnable() { private Runnable mUiBlockingTaskTracker = new Runnable() {
...@@ -42,11 +51,7 @@ public class StartupTimeActivity extends Activity { ...@@ -42,11 +51,7 @@ public class StartupTimeActivity extends Activity {
// the time other UI tasks were run. // the time other UI tasks were run.
long gap = now - mLastTaskTimeMs; long gap = now - mLastTaskTimeMs;
if (gap > MIN_TIME_TO_RECORD_MS) { if (gap > MIN_TIME_TO_RECORD_MS) {
try { mEventQueue.add(gap);
mEventQueue.put(gap);
} catch (Exception e) {
throw new RuntimeException(e);
}
} }
} }
mLastTaskTimeMs = now; mLastTaskTimeMs = now;
...@@ -59,35 +64,79 @@ public class StartupTimeActivity extends Activity { ...@@ -59,35 +64,79 @@ public class StartupTimeActivity extends Activity {
@Override @Override
public void run() { public void run() {
mFinished = true; mFinished = true;
StringBuilder sb = new StringBuilder(); String res = TextUtils.join(", ", mEventQueue);
while (true) { Log.i(TAG, "UI blocking times in startup (ms): " + res);
Long gap = mEventQueue.poll(); if (mWebView != null) {
if (gap == null) break; // Remove webview from view hierarchy while preventing NPE.
sb.append(gap); mLayout.removeAllViews();
if (mEventQueue.peek() != null) sb.append(", "); mWebView.destroy();
mWebView = null;
} }
Log.i(TAG, "UI blocking times in startup (ms): " + sb.toString());
finish(); finish();
// Automatically clean up WebView to measure WebView startup cleanly
// next time.
Process.killProcess(Process.myPid());
} }
}; };
@IntDef({Target.DO_NOTHING, Target.CREATE, Target.ADD_VIEW, Target.LOAD, Target.WORKAROUND})
@interface Target {
int DO_NOTHING = 0;
int CREATE = 1;
int ADD_VIEW = 2;
int LOAD = 3;
int WORKAROUND = 4;
}
private @Target int getTarget() {
Intent intent = getIntent();
if (intent == null) return Target.CREATE;
Bundle extras = intent.getExtras();
if (extras == null) return Target.CREATE;
return extras.getInt(TARGET_KEY, Target.CREATE);
}
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@Target
int target = getTarget();
getWindow().setTitle( getWindow().setTitle(
getResources().getString(R.string.title_activity_startup_time)); getResources().getString(R.string.title_activity_startup_time));
mLayout = new LinearLayout(this);
setContentView(mLayout);
mHandler = new Handler(); mHandler = new Handler();
mUiBlockingTaskTracker.run(); mUiBlockingTaskTracker.run();
long t1 = SystemClock.elapsedRealtime(); switch (target) {
WebView webView = new WebView(this); case Target.DO_NOTHING: {
setContentView(webView); break;
long t2 = SystemClock.elapsedRealtime(); }
case Target.CREATE: {
mWebView = new WebView(this);
break;
}
case Target.ADD_VIEW: {
mWebView = new WebView(this);
mLayout.addView(mWebView);
break;
}
case Target.LOAD: {
mWebView = new WebView(this);
mLayout.addView(mWebView);
mWebView.loadUrl("about:blank");
break;
}
case Target.WORKAROUND: {
// This is a useful hack to run some of the startup tasks in a background
// thread to reduce the UI thread blocking time.
Thread t = new Thread(() -> {
WebSettings.getDefaultUserAgent(this);
// Note that there are some UI tasks caused by getDefaultUserAgent().
// But this will ensure that new WebView() can be run after those UI tasks
// are run first.
mHandler.post(() -> { mWebView = new WebView(this); });
});
t.start();
break;
}
}
mHandler.postDelayed(mFinishTask, TIME_TO_FINISH_APP_MS); mHandler.postDelayed(mFinishTask, TIME_TO_FINISH_APP_MS);
Log.i(TAG, "WebViewStartupTimeMillis=" + (t2 - t1));
} }
}
} \ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment