Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(9)

Unified Diff: tools/android/customtabs_benchmark/java/src/org/chromium/customtabs/test/MainActivity.java

Issue 2722733004: customtabs: Benchmark app for WebView / Custom Tabs comparisons. (Closed)
Patch Set: . Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: tools/android/customtabs_benchmark/java/src/org/chromium/customtabs/test/MainActivity.java
diff --git a/tools/android/customtabs_benchmark/java/src/org/chromium/customtabs/test/MainActivity.java b/tools/android/customtabs_benchmark/java/src/org/chromium/customtabs/test/MainActivity.java
index b1b336d46d39512fab62f319b3394cbe17613ac9..9c493b3d793beab476d2e78623263cbf6fd6af5f 100644
--- a/tools/android/customtabs_benchmark/java/src/org/chromium/customtabs/test/MainActivity.java
+++ b/tools/android/customtabs_benchmark/java/src/org/chromium/customtabs/test/MainActivity.java
@@ -18,14 +18,33 @@ import android.support.customtabs.CustomTabsIntent;
import android.support.customtabs.CustomTabsServiceConnection;
import android.support.customtabs.CustomTabsSession;
import android.util.Log;
+import android.view.View;
+import android.widget.Button;
+import android.widget.CheckBox;
+import android.widget.EditText;
+import android.widget.RadioButton;
/** Activity used to benchmark Custom Tabs PLT.
+ *
+ * This activity contains benchmark code for two modes:
+ * 1. Comparison between a basic use of Custom Tabs and a basic use of WebView.
+ * 2. Custom Tabs benchmarking under various scenarios.
+ *
+ * The two modes are not merged into one as the metrics we can extract in the two cases
+ * are constrained for the first one by what WebView provides.
*/
-public class MainActivity extends Activity {
- private static final String TAG = "CUSTOMTABSBENCH";
+public class MainActivity extends Activity implements View.OnClickListener {
+ static final String TAG = "CUSTOMTABSBENCH";
private static final String DEFAULT_URL = "https://www.android.com";
private static final String DEFAULT_PACKAGE = "com.google.android.apps.chrome";
private static final int NONE = -1;
+ // Common key between the benchmark modes.
+ private static final String URL_KEY = "url";
+
+ // Keys for the WebView / Custom Tabs comparison.
+ static final String INTENT_SENT_EXTRA = "intent_sent_ms";
+ private static final String USE_WEBVIEW_KEY = "use_webview";
+ private static final String WARMUP_KEY = "warmup";
// Keep in sync with the same constants in CustomTabsConnection.
private static final String DEBUG_OVERRIDE_KEY =
@@ -36,16 +55,167 @@ public class MainActivity extends Activity {
// Only for reporting.
private static final int NO_STATE_PREFETCH = 3;
+ private final Handler mHandler = new Handler(Looper.getMainLooper());
+
+ private EditText mUrlEditText;
+ private RadioButton mChromeRadioButton;
+ private RadioButton mWebViewRadioButton;
+ private CheckBox mWarmupCheckbox;
+ private long mIntentSentMs;
+
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
- processArguments(getIntent());
+ final Intent intent = getIntent();
+
+ setUpUi();
+
+ // Automated mode, 1s later to leave time for the app to settle.
+ if (intent.getStringExtra(URL_KEY) != null) {
+ mHandler.postDelayed(new Runnable() {
+ @Override
+ public void run() {
+ processArguments(intent);
+ }
+ }, 1000);
+ }
}
- /** Process the arguments from the Intent extras.
- */
+ /** Displays the UI and registers the click listeners. */
+ private void setUpUi() {
+ setContentView(R.layout.main);
+
+ mUrlEditText = (EditText) findViewById(R.id.url_text);
+ mChromeRadioButton = (RadioButton) findViewById(R.id.radio_chrome);
+ mWebViewRadioButton = (RadioButton) findViewById(R.id.radio_webview);
+ mWarmupCheckbox = (CheckBox) findViewById(R.id.warmup_checkbox);
+ Button goButton = (Button) findViewById(R.id.go_button);
+
+ mUrlEditText.setOnClickListener(this);
+ mChromeRadioButton.setOnClickListener(this);
+ mWebViewRadioButton.setOnClickListener(this);
+ mWarmupCheckbox.setOnClickListener(this);
+ goButton.setOnClickListener(this);
+ }
+
+ @Override
+ public void onClick(View v) {
+ int id = v.getId();
+
+ boolean warmup = mWarmupCheckbox.isChecked();
+ boolean useChrome = mChromeRadioButton.isChecked();
+ boolean useWebView = mWebViewRadioButton.isChecked();
+ String url = mUrlEditText.getText().toString();
+
+ if (id == R.id.go_button) {
+ customTabsWebViewBenchmark(url, useChrome, useWebView, warmup);
+ }
+ }
+
+ /** Routes to either of the benchmark modes. */
private void processArguments(Intent intent) {
- String url = intent.getStringExtra("url");
+ if (intent.hasExtra(USE_WEBVIEW_KEY)) {
+ startCustomTabsWebViewBenchmark(intent);
+ } else {
+ startCustomTabsBenchmark(intent);
+ }
+ }
+
+ /** Start the CustomTabs / WebView comparison benchmark.
+ *
+ * NOTE: Methods below are for the first benchmark mode.
+ */
+ private void startCustomTabsWebViewBenchmark(Intent intent) {
+ Bundle extras = intent.getExtras();
+ String url = extras.getString(URL_KEY);
+ boolean useWebView = extras.getBoolean(USE_WEBVIEW_KEY);
+ boolean useChrome = !useWebView;
+ boolean warmup = extras.getBoolean(WARMUP_KEY);
+ customTabsWebViewBenchmark(url, useChrome, useWebView, warmup);
+ }
+
+ /** Start the CustomTabs / WebView comparison benchmark. */
+ private void customTabsWebViewBenchmark(
+ String url, boolean useChrome, boolean useWebView, boolean warmup) {
+ if (useChrome) {
+ launchChrome(url, warmup);
+ } else {
+ assert useWebView;
+ launchWebView(url);
+ }
+ }
+
+ private void launchWebView(String url) {
+ Intent intent = new Intent();
+ intent.setData(Uri.parse(url));
+ intent.setClass(this, WebViewActivity.class);
+ intent.putExtra(INTENT_SENT_EXTRA, now());
+ startActivity(intent);
+ }
+
+ private void launchChrome(final String url, final boolean warmup) {
+ CustomTabsServiceConnection connection = new CustomTabsServiceConnection() {
+ @Override
+ public void onCustomTabsServiceConnected(ComponentName name, CustomTabsClient client) {
+ launchChromeIntent(url, warmup, client);
+ }
+
+ @Override
+ public void onServiceDisconnected(ComponentName name) {}
+ };
+ CustomTabsClient.bindCustomTabsService(this, DEFAULT_PACKAGE, connection);
+ }
+
+ private void launchChromeIntent(String url, boolean warmup, CustomTabsClient client) {
+ CustomTabsCallback callback = new CustomTabsCallback() {
+ private long mNavigationStartOffsetMs;
+
+ @Override
+ public void onNavigationEvent(int navigationEvent, Bundle extras) {
+ long offsetMs = now() - mIntentSentMs;
+ switch (navigationEvent) {
+ case CustomTabsCallback.NAVIGATION_STARTED:
+ mNavigationStartOffsetMs = offsetMs;
+ Log.w(TAG, "navigationStarted = " + offsetMs);
+ break;
+ case CustomTabsCallback.NAVIGATION_FINISHED:
+ Log.w(TAG, "navigationFinished = " + offsetMs);
+ Log.w(TAG, "CHROME," + mNavigationStartOffsetMs + "," + offsetMs);
+ break;
+ default:
+ break;
+ }
+ }
+ };
+ CustomTabsSession session = client.newSession(callback);
+ final CustomTabsIntent customTabsIntent = new CustomTabsIntent.Builder(session).build();
+ final Uri uri = Uri.parse(url);
+
+ if (warmup) {
+ client.warmup(0);
+ mHandler.postDelayed(new Runnable() {
+ @Override
+ public void run() {
+ mIntentSentMs = now();
+ customTabsIntent.launchUrl(MainActivity.this, uri);
+ }
+ }, 3000);
+ } else {
+ mIntentSentMs = now();
+ customTabsIntent.launchUrl(MainActivity.this, uri);
+ }
+ }
+
+ static long now() {
+ return System.currentTimeMillis();
+ }
+
+ /** Start the second benchmark mode.
+ *
+ * NOTE: Methods below are for the second mode.
+ */
+ private void startCustomTabsBenchmark(Intent intent) {
+ String url = intent.getStringExtra(URL_KEY);
if (url == null) url = DEFAULT_URL;
String packageName = intent.getStringExtra("package_name");
if (packageName == null) packageName = DEFAULT_PACKAGE;
@@ -146,8 +316,7 @@ public class MainActivity extends Activity {
/** Same as {@link logMetricsAndFinish()} with a set delay in ms. */
public void logMetricsAndFinishDelayed(int delayMs) {
- Handler handler = new Handler(Looper.getMainLooper());
- handler.postDelayed(new Runnable() {
+ mHandler.postDelayed(new Runnable() {
@Override
public void run() {
logMetricsAndFinish();
@@ -159,7 +328,6 @@ public class MainActivity extends Activity {
private void onCustomTabsServiceConnected(CustomTabsClient client, final Uri uri,
final CustomCallback cb, boolean warmup, final int prerenderMode,
int delayToMayLaunchUrl, final int delayToLaunchUrl, final int timeoutSeconds) {
- final Handler handler = new Handler(Looper.getMainLooper());
final CustomTabsSession session = client.newSession(cb);
final CustomTabsIntent intent = (new CustomTabsIntent.Builder(session)).build();
final Runnable launchRunnable = new Runnable() {
@@ -181,13 +349,13 @@ public class MainActivity extends Activity {
}
session.mayLaunchUrl(uri, extras, null);
- handler.postDelayed(launchRunnable, delayToLaunchUrl);
+ mHandler.postDelayed(launchRunnable, delayToLaunchUrl);
}
};
if (warmup) client.warmup(0);
if (delayToMayLaunchUrl != NONE) {
- handler.postDelayed(mayLaunchRunnable, delayToMayLaunchUrl);
+ mHandler.postDelayed(mayLaunchRunnable, delayToMayLaunchUrl);
} else {
launchRunnable.run();
}

Powered by Google App Engine
This is Rietveld 408576698