Commit 4d614fbc authored by Tim Volodine's avatar Tim Volodine Committed by Commit Bot

[SystemWebViewShell] Add WebViewTracingActivity for Tracing API telemetry.

Add activity to measure Tracing API related timings. In particular it allows
to measure time for loading a given URL with or without tracing enabled. It
also provides the overall tracing time, i.e. time it takes from starting
tracing until completion when all traces are written to disk.

Example usage:
$ adb shell am start -n org.chromium.webview_shell/.WebViewTracingActivity -a VIEW -d \
  http://www.google.com --ez enableTracing true

BUG=797312

Change-Id: Iac5e871952d356762f351527b6aafdb4faf07255
Reviewed-on: https://chromium-review.googlesource.com/1153249
Commit-Queue: Tim Volodine <timvolodine@chromium.org>
Reviewed-by: default avatarRichard Coles <torne@chromium.org>
Cr-Commit-Position: refs/heads/master@{#580267}
parent ef6a17cf
......@@ -28,6 +28,7 @@ android_apk("system_webview_shell_apk") {
"apk/src/org/chromium/webview_shell/WebViewBrowserActivity.java",
"apk/src/org/chromium/webview_shell/WebViewLayoutTestActivity.java",
"apk/src/org/chromium/webview_shell/WebViewThreadTestActivity.java",
"apk/src/org/chromium/webview_shell/WebViewTracingActivity.java",
]
android_manifest = "apk/AndroidManifest.xml"
deps = [
......@@ -45,18 +46,16 @@ instrumentation_test_apk("system_webview_shell_page_cycler_apk") {
apk_name = "SystemWebViewShellPageCycler"
apk_under_test = ":system_webview_shell_apk"
android_manifest = "page_cycler/AndroidManifest.xml"
java_files = [
"page_cycler/src/org/chromium/webview_shell/page_cycler/PageCyclerTest.java",
]
java_files = [ "page_cycler/src/org/chromium/webview_shell/page_cycler/PageCyclerTest.java" ]
deps = [
"//base:base_java",
"//base:base_java_test_support",
"//content/public/android:content_java",
"//content/public/test/android:content_java_test_support",
"//testing/android/reporter:reporter_java",
"//third_party/junit",
"//third_party/android_support_test_runner:rules_java",
"//third_party/android_support_test_runner:runner_java",
"//third_party/junit",
]
enable_multidex = true
}
......
......@@ -115,6 +115,13 @@
android:exported="true">
</activity>
<activity
android:name="org.chromium.webview_shell.WebViewTracingActivity"
android:label="@string/title_activity_telemetry"
android:noHistory="true"
android:exported="true">
</activity>
<uses-library android:name="android.test.runner" />
</application>
</manifest>
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.webview_shell;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.webkit.TracingConfig;
import android.webkit.TracingController;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.concurrent.Executors;
/**
* This activity is designed for telemetry testing of WebView Tracing API.
*
* In particular it allows to measure time for loading a given URL with or without
* tracing enabled. It also provides the overall tracing time, i.e. time it takes from
* starting tracing until completion when all traces are written to disk.
*
* Example usage:
* $ adb shell am start -n org.chromium.webview_shell/.WebViewTracingActivity -a VIEW -d \
* http://www.google.com --ez enableTracing true
*
*/
public class WebViewTracingActivity extends Activity {
private static final String TAG = "WebViewTracingActivity";
private static long sUrlLoadTimeStartMs;
private static long sUrlLoadTimeEndMs;
private static long sOverallTracingTimeStartMs;
private static long sOverallTracingTimeEndMs;
private static class TracingLogger extends FileOutputStream {
private long mByteCount;
private Activity mActivity;
public TracingLogger(String fileName, Activity activity) throws FileNotFoundException {
super(fileName);
mActivity = activity;
}
@Override
public void write(byte[] chunk) throws IOException {
mByteCount += chunk.length;
super.write(chunk);
}
@Override
public void close() throws IOException {
super.close();
sOverallTracingTimeEndMs = SystemClock.elapsedRealtime();
android.util.Log.i(TAG,
"TracingLogger.complete(), byte count = " + getByteCount()
+ ", overall duration (ms) = "
+ (sOverallTracingTimeEndMs - sOverallTracingTimeStartMs));
mActivity.finish();
}
public long getByteCount() {
return mByteCount;
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setTitle("WebViewTracingActivity");
Intent intent = getIntent();
String url = getUrlFromIntent(intent);
if (url == null) {
url = "about:blank";
}
boolean enableTracing = false;
if (intent.getExtras() != null) {
enableTracing = intent.getExtras().getBoolean("enableTracing", false);
}
loadUrl(url, enableTracing);
}
@SuppressLint("NewApi") // TracingController related methods require API level 28.
private void loadUrl(final String url, boolean enableTracing) {
final Activity activity = this;
WebView webView = new WebView(this);
setContentView(webView);
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
final TracingController tracingController = TracingController.getInstance();
webView.setWebViewClient(new WebViewClient() {
@SuppressLint("NewApi") // TracingController related methods require API level 28.
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
sUrlLoadTimeEndMs = SystemClock.elapsedRealtime();
android.util.Log.i(TAG,
"onPageFinished(), enableTracing = " + enableTracing + ", url = " + url
+ ", urlLoadTimeMillis = "
+ (sUrlLoadTimeEndMs - sUrlLoadTimeStartMs));
if (enableTracing) {
String outFileName = getFilesDir() + "/webview_tracing.json";
try {
tracingController.stop(new TracingLogger(outFileName, activity),
Executors.newSingleThreadExecutor());
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
} else {
activity.finish();
}
}
});
if (enableTracing) {
sOverallTracingTimeStartMs = SystemClock.elapsedRealtime();
tracingController.start(new TracingConfig.Builder()
.addCategories(TracingConfig.CATEGORIES_WEB_DEVELOPER)
.setTracingMode(TracingConfig.RECORD_UNTIL_FULL)
.build());
}
sUrlLoadTimeStartMs = SystemClock.elapsedRealtime();
webView.loadUrl(url);
}
private static String getUrlFromIntent(Intent intent) {
return intent != null ? intent.getDataString() : null;
}
}
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