Bug 1361855 - Filter TAB_COUNT histogram to record only on 5 minute intervals; r?Gijs draft
authorLie Ryan <lie.1296@gmail.com>
Fri, 16 Jun 2017 12:48:19 +0000
changeset 647635 9c462fa1024a1ce285c9051477c666a6bdb0d6d1
parent 647634 77f0130b6835cf6ca400fccee816a1b6959adfec
child 726583 d369e32c88a6cdbb6da5a1ca56cb021d009798a0
push id74488
push userbmo:lie.1296@gmail.com
push dateWed, 16 Aug 2017 17:50:44 +0000
reviewersGijs
bugs1361855
milestone57.0a1
Bug 1361855 - Filter TAB_COUNT histogram to record only on 5 minute intervals; r?Gijs MozReview-Commit-ID: F5mI1eiffWN
browser/modules/BrowserUsageTelemetry.jsm
browser/modules/test/browser/browser_UsageTelemetry.js
--- a/browser/modules/BrowserUsageTelemetry.jsm
+++ b/browser/modules/BrowserUsageTelemetry.jsm
@@ -4,16 +4,17 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 "use strict";
 
 this.EXPORTED_SYMBOLS = [
   "BrowserUsageTelemetry",
   "URLBAR_SELECTED_RESULT_TYPES",
   "URLBAR_SELECTED_RESULT_METHODS",
+  "MINIMUM_TAB_COUNT_INTERVAL_MS",
  ];
 
 const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
 
 Cu.import("resource://gre/modules/Services.jsm");
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 
 XPCOMUtils.defineLazyModuleGetter(this, "PrivateBrowsingUtils",
@@ -79,16 +80,20 @@ const URLBAR_SELECTED_RESULT_TYPES = {
  * these category names directly when they add to a histogram.
  */
 const URLBAR_SELECTED_RESULT_METHODS = {
   enter: 0,
   enterSelection: 1,
   click: 2,
 };
 
+
+const MINIMUM_TAB_COUNT_INTERVAL_MS = 5 * 60 * 1000; // 5 minutes, in ms
+
+
 function getOpenTabsAndWinsCounts() {
   let tabCount = 0;
   let winCount = 0;
 
   let browserEnum = Services.wm.getEnumerator("navigator:browser");
   while (browserEnum.hasMoreElements()) {
     let win = browserEnum.getNext();
     winCount++;
@@ -312,16 +317,17 @@ let urlbarListener = {
   },
 
   QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver,
                                          Ci.nsISupportsWeakReference]),
 };
 
 let BrowserUsageTelemetry = {
   init() {
+    this._lastRecordTabCount = 0;
     urlbarListener.init();
     this._setupAfterRestore();
   },
 
   /**
    * Handle subsession splits in the parent process.
    */
   afterSubsessionSplit() {
@@ -640,12 +646,19 @@ let BrowserUsageTelemetry = {
 
       // We won't receive the "TabOpen" event for the first tab within a new window.
       // Account for that.
       this._onTabOpen(counts.tabCount);
     };
     win.addEventListener("load", onLoad);
   },
 
-  _recordTabCount(tabCount = getTabCount()) {
-    Services.telemetry.getHistogramById("TAB_COUNT").add(tabCount);
-  },
+  _recordTabCount(tabCount) {
+    let currentTime = Date.now();
+    if (currentTime > this._lastRecordTabCount + MINIMUM_TAB_COUNT_INTERVAL_MS) {
+      if (tabCount === undefined) {
+        tabCount = getTabCount();
+      }
+      Services.telemetry.getHistogramById("TAB_COUNT").add(tabCount);
+      this._lastRecordTabCount = currentTime;
+    }
+  }
 };
--- a/browser/modules/test/browser/browser_UsageTelemetry.js
+++ b/browser/modules/test/browser/browser_UsageTelemetry.js
@@ -5,16 +5,19 @@ const TAB_EVENT_COUNT = "browser.engagem
 const MAX_CONCURRENT_WINDOWS = "browser.engagement.max_concurrent_window_count";
 const WINDOW_OPEN_COUNT = "browser.engagement.window_open_event_count";
 const TOTAL_URI_COUNT = "browser.engagement.total_uri_count";
 const UNIQUE_DOMAINS_COUNT = "browser.engagement.unique_domains_count";
 const UNFILTERED_URI_COUNT = "browser.engagement.unfiltered_uri_count";
 
 const TELEMETRY_SUBSESSION_TOPIC = "internal-telemetry-after-subsession-split";
 
+XPCOMUtils.defineLazyModuleGetter(this, "MINIMUM_TAB_COUNT_INTERVAL_MS",
+                                  "resource:///modules/BrowserUsageTelemetry.jsm");
+
 // Reset internal URI counter in case URIs were opened by other tests.
 Services.obs.notifyObservers(null, TELEMETRY_SUBSESSION_TOPIC);
 
 /**
  * Waits for the web progress listener associated with this tab to fire an
  * onLocationChange for a non-error page.
  *
  * @param {xul:browser} browser
@@ -275,37 +278,77 @@ function checkTabCountHistogram(result, 
 
 add_task(async function test_tabsHistogram() {
   let openedTabs = [];
   let tabCountHist = getAndClearHistogram("TAB_COUNT");
 
   checkTabCountHistogram(tabCountHist.snapshot(), [0, 0], "TAB_COUNT telemetry - initial tab counts")
 
   // Add a new tab and check that the count is right.
+  BrowserUsageTelemetry._lastRecordTabCount = 0;
   openedTabs.push(await BrowserTestUtils.openNewForegroundTab(gBrowser, "about:blank"));
   checkTabCountHistogram(tabCountHist.snapshot(), [0, 0, 1], "TAB_COUNT telemetry - opening tabs");
 
   // Open a different page and check the counts.
+  BrowserUsageTelemetry._lastRecordTabCount = 0;
   let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, "about:blank");
   openedTabs.push(tab);
+  BrowserUsageTelemetry._lastRecordTabCount = 0;
   await BrowserTestUtils.loadURI(tab.linkedBrowser, "http://example.com/");
   await BrowserTestUtils.browserLoaded(tab.linkedBrowser);
   checkTabCountHistogram(tabCountHist.snapshot(), [0, 0, 1, 2], "TAB_COUNT telemetry - loading page");
 
   // Open another tab
+  BrowserUsageTelemetry._lastRecordTabCount = 0;
   openedTabs.push(await BrowserTestUtils.openNewForegroundTab(gBrowser, "about:blank"));
   checkTabCountHistogram(tabCountHist.snapshot(), [0, 0, 1, 2, 1], "TAB_COUNT telemetry - opening more tabs");
 
   // Add a new window and then some tabs in it. A new window starts with one tab.
+  BrowserUsageTelemetry._lastRecordTabCount = 0;
   let win = await BrowserTestUtils.openNewBrowserWindow();
   checkTabCountHistogram(tabCountHist.snapshot(), [0, 0, 1, 2, 1, 1], "TAB_COUNT telemetry - opening window");
 
-  openedTabs.push(await BrowserTestUtils.openNewForegroundTab(win.gBrowser, "about:blank"));
-  openedTabs.push(await BrowserTestUtils.openNewForegroundTab(win.gBrowser, "about:blank"));
-  openedTabs.push(await BrowserTestUtils.openNewForegroundTab(win.gBrowser, "about:blank"));
-  checkTabCountHistogram(tabCountHist.snapshot(), [0, 0, 1, 2, 1, 1, 1, 1, 1], "TAB_COUNT telemetry - opening more tabs in another window");
+  // Do not trigger a recount if _lastRecordTabCount is recent on new tab
+  BrowserUsageTelemetry._lastRecordTabCount = Date.now() - (MINIMUM_TAB_COUNT_INTERVAL_MS / 2);
+  {
+    let oldLastRecordTabCount = BrowserUsageTelemetry._lastRecordTabCount;
+    openedTabs.push(await BrowserTestUtils.openNewForegroundTab(win.gBrowser, "about:blank"));
+    checkTabCountHistogram(tabCountHist.snapshot(), [0, 0, 1, 2, 1, 1, 0], "TAB_COUNT telemetry - new tab, recount event ignored");
+    ok(BrowserUsageTelemetry._lastRecordTabCount == oldLastRecordTabCount, "TAB_COUNT telemetry - _lastRecordTabCount unchanged");
+  }
+
+  // Trigger a recount if _lastRecordTabCount has passed on new tab
+  BrowserUsageTelemetry._lastRecordTabCount = Date.now() - (MINIMUM_TAB_COUNT_INTERVAL_MS + 1000);
+  {
+    let oldLastRecordTabCount = BrowserUsageTelemetry._lastRecordTabCount;
+    openedTabs.push(await BrowserTestUtils.openNewForegroundTab(win.gBrowser, "about:blank"));
+    checkTabCountHistogram(tabCountHist.snapshot(), [0, 0, 1, 2, 1, 1, 0, 1], "TAB_COUNT telemetry - new tab, recount event included");
+    ok(BrowserUsageTelemetry._lastRecordTabCount != oldLastRecordTabCount, "TAB_COUNT telemetry - _lastRecordTabCount updated");
+    ok(BrowserUsageTelemetry._lastRecordTabCount > Date.now() - MINIMUM_TAB_COUNT_INTERVAL_MS, "TAB_COUNT telemetry - _lastRecordTabCount invariant");
+  }
+
+  // Do not trigger a recount if _lastRecordTabCount is recent on page load
+  BrowserUsageTelemetry._lastRecordTabCount = Date.now() - (MINIMUM_TAB_COUNT_INTERVAL_MS / 2);
+  {
+    let oldLastRecordTabCount = BrowserUsageTelemetry._lastRecordTabCount;
+    await BrowserTestUtils.loadURI(tab.linkedBrowser, "http://example.com/");
+    await BrowserTestUtils.browserLoaded(tab.linkedBrowser);
+    checkTabCountHistogram(tabCountHist.snapshot(), [0, 0, 1, 2, 1, 1, 0, 1], "TAB_COUNT telemetry - page load, recount event ignored");
+    ok(BrowserUsageTelemetry._lastRecordTabCount == oldLastRecordTabCount, "TAB_COUNT telemetry - _lastRecordTabCount unchanged");
+  }
+
+  // Trigger a recount if _lastRecordTabCount has passed on page load
+  BrowserUsageTelemetry._lastRecordTabCount = Date.now() - (MINIMUM_TAB_COUNT_INTERVAL_MS + 1000);
+  {
+    let oldLastRecordTabCount = BrowserUsageTelemetry._lastRecordTabCount;
+    await BrowserTestUtils.loadURI(tab.linkedBrowser, "http://example.com/");
+    await BrowserTestUtils.browserLoaded(tab.linkedBrowser);
+    checkTabCountHistogram(tabCountHist.snapshot(), [0, 0, 1, 2, 1, 1, 0, 2], "TAB_COUNT telemetry - page load, recount event included");
+    ok(BrowserUsageTelemetry._lastRecordTabCount != oldLastRecordTabCount, "TAB_COUNT telemetry - _lastRecordTabCount updated");
+    ok(BrowserUsageTelemetry._lastRecordTabCount > Date.now() - MINIMUM_TAB_COUNT_INTERVAL_MS, "TAB_COUNT telemetry - _lastRecordTabCount invariant");
+  }
 
   // Remove all the extra windows and tabs.
   for (let openTab of openedTabs) {
     await BrowserTestUtils.removeTab(openTab);
   }
   await BrowserTestUtils.closeWindow(win);
 });