toolkit/components/places/tests/unit/test_telemetry.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /* Any copyright is dedicated to the Public Domain.
     2    http://creativecommons.org/publicdomain/zero/1.0/ */
     4 // Tests common Places telemetry probes by faking the telemetry service.
     6 Components.utils.import("resource://gre/modules/PlacesDBUtils.jsm");
     8 let histograms = {
     9   PLACES_PAGES_COUNT: function (val) do_check_eq(val, 1),
    10   PLACES_BOOKMARKS_COUNT: function (val) do_check_eq(val, 1),
    11   PLACES_TAGS_COUNT: function (val) do_check_eq(val, 1),
    12   PLACES_FOLDERS_COUNT: function (val) do_check_eq(val, 1),
    13   PLACES_KEYWORDS_COUNT: function (val) do_check_eq(val, 1),
    14   PLACES_SORTED_BOOKMARKS_PERC: function (val) do_check_eq(val, 100),
    15   PLACES_TAGGED_BOOKMARKS_PERC: function (val) do_check_eq(val, 100),
    16   PLACES_DATABASE_FILESIZE_MB: function (val) do_check_true(val > 0),
    17   // The journal may have been truncated.
    18   PLACES_DATABASE_JOURNALSIZE_MB: function (val) do_check_true(val >= 0),
    19   PLACES_DATABASE_PAGESIZE_B: function (val) do_check_eq(val, 32768),
    20   PLACES_DATABASE_SIZE_PER_PAGE_B: function (val) do_check_true(val > 0),
    21   PLACES_EXPIRATION_STEPS_TO_CLEAN2: function (val) do_check_true(val > 1),
    22   //PLACES_AUTOCOMPLETE_1ST_RESULT_TIME_MS:  function (val) do_check_true(val > 1),
    23   PLACES_IDLE_FRECENCY_DECAY_TIME_MS: function (val) do_check_true(val > 0),
    24   PLACES_IDLE_MAINTENANCE_TIME_MS: function (val) do_check_true(val > 0),
    25   PLACES_ANNOS_BOOKMARKS_COUNT: function (val) do_check_eq(val, 1),
    26   PLACES_ANNOS_BOOKMARKS_SIZE_KB: function (val) do_check_eq(val, 1),
    27   PLACES_ANNOS_PAGES_COUNT: function (val) do_check_eq(val, 1),
    28   PLACES_ANNOS_PAGES_SIZE_KB: function (val) do_check_eq(val, 1),
    29   PLACES_FRECENCY_CALC_TIME_MS: function (val) do_check_true(val >= 0),
    30 }
    32 function run_test()
    33 {
    34   run_next_test();
    35 }
    37 add_task(function test_execute()
    38 {
    39   // Put some trash in the database.
    40   const URI = NetUtil.newURI("http://moz.org/");
    42   let folderId = PlacesUtils.bookmarks.createFolder(PlacesUtils.unfiledBookmarksFolderId,
    43                                                     "moz test",
    44                                                     PlacesUtils.bookmarks.DEFAULT_INDEX);
    45   let itemId = PlacesUtils.bookmarks.insertBookmark(folderId,
    46                                                     uri,
    47                                                     PlacesUtils.bookmarks.DEFAULT_INDEX,
    48                                                     "moz test");
    49   PlacesUtils.tagging.tagURI(uri, ["tag"]);
    50   PlacesUtils.bookmarks.setKeywordForBookmark(itemId, "keyword");
    52   // Set a large annotation.
    53   let content = "";
    54   while (content.length < 1024) {
    55     content += "0";
    56   }
    57   PlacesUtils.annotations.setItemAnnotation(itemId, "test-anno", content, 0,
    58                                             PlacesUtils.annotations.EXPIRE_NEVER);
    59   PlacesUtils.annotations.setPageAnnotation(uri, "test-anno", content, 0,
    60                                             PlacesUtils.annotations.EXPIRE_NEVER);
    62   // Request to gather telemetry data.
    63   Cc["@mozilla.org/places/categoriesStarter;1"]
    64     .getService(Ci.nsIObserver)
    65     .observe(null, "gather-telemetry", null);
    67   yield promiseAsyncUpdates();
    69   // Test expiration probes.
    70   for (let i = 0; i < 2; i++) {
    71     yield promiseAddVisits({
    72       uri: uri("http://" +  i + ".moz.org/"),
    73       visitDate: Date.now() // [sic]
    74     });
    75   }
    76   Services.prefs.setIntPref("places.history.expiration.max_pages", 0);
    77   let expire = Cc["@mozilla.org/places/expiration;1"].getService(Ci.nsIObserver);
    78   expire.observe(null, "places-debug-start-expiration", 1);
    79   expire.observe(null, "places-debug-start-expiration", -1);
    81   // Test autocomplete probes.
    82   /*
    83   // This is useful for manual testing by changing the minimum time for
    84   // autocomplete telemetry to 0, but there is no way to artificially delay
    85   // autocomplete by more than 50ms in a realiable way.
    86   Services.prefs.setIntPref("browser.urlbar.search.sources", 3);
    87   Services.prefs.setIntPref("browser.urlbar.default.behavior", 0);
    88   function AutoCompleteInput(aSearches) {
    89     this.searches = aSearches;
    90   }
    91   AutoCompleteInput.prototype = {
    92     timeout: 10,
    93     textValue: "",
    94     searchParam: "",
    95     popupOpen: false,
    96     minResultsForPopup: 0,
    97     invalidate: function() {},
    98     disableAutoComplete: false,
    99     completeDefaultIndex: false,
   100     get popup() { return this; },
   101     onSearchBegin: function() {},
   102     onSearchComplete: function() {},
   103     setSelectedIndex: function() {},
   104     get searchCount() { return this.searches.length; },
   105     getSearchAt: function(aIndex) this.searches[aIndex],
   106     QueryInterface: XPCOMUtils.generateQI([
   107       Ci.nsIAutoCompleteInput,
   108       Ci.nsIAutoCompletePopup,
   109     ])
   110   };
   111   let controller = Cc["@mozilla.org/autocomplete/controller;1"].
   112                    getService(Ci.nsIAutoCompleteController);
   113   controller.input = new AutoCompleteInput(["history"]);
   114   controller.startSearch("moz");
   115   */
   117   // Test idle probes.
   118   PlacesUtils.history.QueryInterface(Ci.nsIObserver)
   119                      .observe(null, "idle-daily", null);
   120   PlacesDBUtils.maintenanceOnIdle();
   122   yield promiseTopicObserved("places-maintenance-finished");
   124   for (let histogramId in histograms) {
   125     do_log_info("checking histogram " + histogramId);
   126     let validate = histograms[histogramId];
   127     let snapshot = Services.telemetry.getHistogramById(histogramId).snapshot();
   128     validate(snapshot.sum);
   129     do_check_true(snapshot.counts.reduce(function(a, b) a + b) > 0);
   130   }
   131 });
   133 add_test(function test_healthreport_callback() {
   134   PlacesDBUtils.telemetry(null, function onResult(data) {
   135     do_check_neq(data, null);
   137     do_check_eq(Object.keys(data).length, 2);
   138     do_check_eq(data.PLACES_PAGES_COUNT, 1);
   139     do_check_eq(data.PLACES_BOOKMARKS_COUNT, 1);
   141     run_next_test();
   142   });
   143 });

mercurial