Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
Add telemetry for uri count and loading time.
Browse files Browse the repository at this point in the history
  • Loading branch information
daoshengmu committed Sep 19, 2018
1 parent 074593e commit 3e4088f
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ dependencies {
svrImplementation fileTree(dir: "${project.rootDir}/third_party/svr/", include: ['*.jar'])
implementation 'com.android.support:design:27.1.1'
implementation 'com.google.vr:sdk-audio:1.170.0'
implementation "org.mozilla.components:telemetry:0.10"
implementation "org.mozilla.components:telemetry:0.23"
implementation "com.github.mozilla:mozillaspeechlibrary:1.0.4"
}

Expand Down
18 changes: 18 additions & 0 deletions app/src/common/shared/org/mozilla/vrbrowser/SessionStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import android.content.Context;
import android.graphics.Rect;
import android.os.SystemClock;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
Expand All @@ -16,6 +17,8 @@
import org.mozilla.gecko.GeckoAppShell;
import org.mozilla.gecko.GeckoProfile;
import org.mozilla.geckoview.*;
import org.mozilla.vrbrowser.telemetry.TelemetryWrapper;
import org.mozilla.vrbrowser.utils.UrlUtils;

import java.io.File;
import java.io.FileNotFoundException;
Expand Down Expand Up @@ -45,6 +48,8 @@ public static SessionStore get() {
private LinkedList<SessionChangeListener> mSessionChangeListeners;
private LinkedList<GeckoSession.TextInputDelegate> mTextInputListeners;
private LinkedList<GeckoSession.PromptDelegate> mPromptListeners;
private final long MIN_LOAD_TIME = 40;
private long startLoadTime = 0;

public interface SessionChangeListener {
void onNewSession(GeckoSession aSession, int aId);
Expand Down Expand Up @@ -156,6 +161,13 @@ public void dumpAllState(Integer sessionId) {
dumpAllState(getSession(sessionId));
}

private boolean isLocalizedContent(@Nullable String url) {
return url != null && (url.equals("about:blank")
|| url.equals(PRIVATE_BROWSING_URI)
|| url.equals(HOME_WITHOUT_REGION_ORIGIN)
|| url.equals(getHomeUri()));
}

private void dumpAllState(GeckoSession aSession) {
for (GeckoSession.NavigationDelegate listener: mNavigationListeners) {
dumpState(aSession, listener);
Expand Down Expand Up @@ -823,6 +835,7 @@ public void onPageStart(GeckoSession aSession, String aUri) {
return;
}
state.mIsLoading = true;
startLoadTime = SystemClock.elapsedRealtime();
for (GeckoSession.ProgressDelegate listener: mProgressListeners) {
listener.onPageStart(aSession, aUri);
}
Expand All @@ -837,6 +850,11 @@ public void onPageStop(GeckoSession aSession, boolean b) {
}

state.mIsLoading = false;
long elapsedLoad = SystemClock.elapsedRealtime() - startLoadTime;
if (elapsedLoad > MIN_LOAD_TIME && !isLocalizedContent(state.mUri)) {
Log.i(LOGTAG, "Sent load to histogram");
TelemetryWrapper.addLoadToHistogram(state.mUri, elapsedLoad);
}
for (GeckoSession.ProgressDelegate listener: mProgressListeners) {
listener.onPageStop(aSession, b);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import android.content.res.Resources;
import android.os.StrictMode;
import android.support.annotation.UiThread;
import android.util.Log;

import org.mozilla.telemetry.Telemetry;
import org.mozilla.telemetry.TelemetryHolder;
import org.mozilla.telemetry.config.TelemetryConfiguration;
Expand All @@ -19,31 +21,51 @@
import org.mozilla.vrbrowser.R;
import org.mozilla.vrbrowser.SettingsStore;
import org.mozilla.vrbrowser.search.SearchEngine;
import org.mozilla.vrbrowser.utils.UrlUtils;

import java.net.URI;
import java.util.HashSet;
import static java.lang.Math.toIntExact;


public class TelemetryWrapper {
private final static String APP_NAME = "FirefoxReality";
private final static String LOGTAG = "VRB";
private final static int BUCKET_SIZE_MS = 100;
private final static int HISTOGRAM_MIN_INDEX = 0;
private final static int HISTOGRAM_SIZE = 200;

private static HashSet<String> domainMap = new HashSet<String>();
private static int[] histogram = new int[HISTOGRAM_SIZE];
private static int numUri = 0;

private class Category {
private static final String ACTION = "action";
private static final String HISTOGRAM = "histogram";
}

private class Method {
private static final String FOREGROUND = "foreground";
private static final String BACKGROUND = "background";
private static final String OPEN = "open";
private static final String TYPE_URL = "type_url";
private static final String TYPE_QUERY = "type_query";
// TODO: Support "select_query" after providing search suggestion.
private static final String VOICE_QUERY = "voice_query";

}

private class Object {
private static final String APP = "app";
private static final String BROWSER = "browser";
private static final String SEARCH_BAR = "search_bar";
private static final String VOICE_INPUT = "voice_input";
}

private class Extra {
private static final String TOTAL_URI_COUNT = "total_uri_count";
private static final String UNIQUE_DOMAINS_COUNT = "unique_domains_count";
}

// We should call this at the application initial stage. Instead,
// it would be called when users turn on/off the setting of telemetry.
// e.g., SettingsStore.getInstance(context).setTelemetryEnabled();
Expand Down Expand Up @@ -84,6 +106,29 @@ public static void start() {

@UiThread
public static void stop() {
TelemetryEvent histogramEvent = TelemetryEvent.create(Category.HISTOGRAM, Method.FOREGROUND, Object.BROWSER);
for (int bucketIndex = 0; bucketIndex < histogram.length; ++bucketIndex) {
histogramEvent.extra(Integer.toString(bucketIndex * BUCKET_SIZE_MS), Integer.toString(histogram[bucketIndex]));
}
histogramEvent.queue();

// Clear histogram array after queueing it
histogram = new int[HISTOGRAM_SIZE];

// We only upload the domain and URI counts to the probes without including
// users' URI info.
TelemetryEvent.create(Category.ACTION, Method.OPEN, Object.BROWSER).extra(
Extra.UNIQUE_DOMAINS_COUNT,
Integer.toString(domainMap.size())
).queue();
domainMap.clear();

TelemetryEvent.create(Category.ACTION, Method.OPEN, Object.BROWSER).extra(
Extra.TOTAL_URI_COUNT,
Integer.toString(numUri)
).queue();
numUri = 0;

TelemetryEvent.create(Category.ACTION, Method.BACKGROUND, Object.APP).queue();
TelemetryHolder.get().recordSessionEnd();

Expand Down Expand Up @@ -129,5 +174,24 @@ private static void browseEvent() {
// TODO: Working on autocomplete result.
event.queue();
}

@UiThread
public static void addLoadToHistogram(String uri, Long newLoadTime) {
domainMap.add(UrlUtils.stripCommonSubdomains(URI.create(uri).getHost()));
numUri++;
int histogramLoadIndex = toIntExact(newLoadTime / BUCKET_SIZE_MS);

if (histogramLoadIndex > (HISTOGRAM_SIZE - 2)) {
histogramLoadIndex = HISTOGRAM_SIZE - 1;
} else if (histogramLoadIndex < HISTOGRAM_MIN_INDEX) {
histogramLoadIndex = HISTOGRAM_MIN_INDEX;
}

if (histogramLoadIndex >= histogram.length) {
Log.e(LOGTAG, "the histogram size is overflow.");
histogramLoadIndex = histogram.length - 1;
}
histogram[histogramLoadIndex]++;
}
}

33 changes: 33 additions & 0 deletions app/src/common/shared/org/mozilla/vrbrowser/utils/UrlUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.vrbrowser.utils;

import android.support.annotation.Nullable;


// This class refers from mozilla-mobile/focus-android
public class UrlUtils {

public static String stripCommonSubdomains(@Nullable String host) {
if (host == null) {
return null;
}

// In contrast to desktop, we also strip mobile subdomains,
// since its unlikely users are intentionally typing them
int start = 0;

if (host.startsWith("www.")) {
start = 4;
} else if (host.startsWith("mobile.")) {
start = 7;
} else if (host.startsWith("m.")) {
start = 2;
}

return host.substring(start);
}
}

0 comments on commit 3e4088f

Please sign in to comment.