From 0270e19addd09758bb00037b60176f23c74ac49b Mon Sep 17 00:00:00 2001 From: Lie Ryan Date: Fri, 16 Jun 2017 12:48:19 +0000 Subject: [PATCH] Bug 1361855 - Filter TAB_COUNT histogram to record only on 5 minute intervals; r=Gijs MozReview-Commit-ID: F5mI1eiffWN --HG-- extra : rebase_source : 477b4c7df0c6c7dec0fad0648ff069037fc8e696 --- browser/modules/BrowserUsageTelemetry.jsm | 19 +++++-- .../test/browser/browser_UsageTelemetry.js | 51 +++++++++++++++++-- 2 files changed, 63 insertions(+), 7 deletions(-) diff --git a/browser/modules/BrowserUsageTelemetry.jsm b/browser/modules/BrowserUsageTelemetry.jsm index 82a618c11bf9..b3497b7931df 100644 --- a/browser/modules/BrowserUsageTelemetry.jsm +++ b/browser/modules/BrowserUsageTelemetry.jsm @@ -9,6 +9,7 @@ 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; @@ -84,6 +85,10 @@ const URLBAR_SELECTED_RESULT_METHODS = { click: 2, }; + +const MINIMUM_TAB_COUNT_INTERVAL_MS = 5 * 60 * 1000; // 5 minutes, in ms + + function getOpenTabsAndWinsCounts() { let tabCount = 0; let winCount = 0; @@ -317,6 +322,7 @@ let urlbarListener = { let BrowserUsageTelemetry = { init() { + this._lastRecordTabCount = 0; urlbarListener.init(); this._setupAfterRestore(); }, @@ -645,7 +651,14 @@ let BrowserUsageTelemetry = { 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; + } + } }; diff --git a/browser/modules/test/browser/browser_UsageTelemetry.js b/browser/modules/test/browser/browser_UsageTelemetry.js index 33cc8f071746..f1b63d8acd7d 100644 --- a/browser/modules/test/browser/browser_UsageTelemetry.js +++ b/browser/modules/test/browser/browser_UsageTelemetry.js @@ -10,6 +10,9 @@ 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); @@ -280,28 +283,68 @@ add_task(async function test_tabsHistogram() { 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) {