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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/places/tests/unit/test_telemetry.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,144 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +// Tests common Places telemetry probes by faking the telemetry service.
     1.8 +
     1.9 +Components.utils.import("resource://gre/modules/PlacesDBUtils.jsm");
    1.10 +
    1.11 +let histograms = {
    1.12 +  PLACES_PAGES_COUNT: function (val) do_check_eq(val, 1),
    1.13 +  PLACES_BOOKMARKS_COUNT: function (val) do_check_eq(val, 1),
    1.14 +  PLACES_TAGS_COUNT: function (val) do_check_eq(val, 1),
    1.15 +  PLACES_FOLDERS_COUNT: function (val) do_check_eq(val, 1),
    1.16 +  PLACES_KEYWORDS_COUNT: function (val) do_check_eq(val, 1),
    1.17 +  PLACES_SORTED_BOOKMARKS_PERC: function (val) do_check_eq(val, 100),
    1.18 +  PLACES_TAGGED_BOOKMARKS_PERC: function (val) do_check_eq(val, 100),
    1.19 +  PLACES_DATABASE_FILESIZE_MB: function (val) do_check_true(val > 0),
    1.20 +  // The journal may have been truncated.
    1.21 +  PLACES_DATABASE_JOURNALSIZE_MB: function (val) do_check_true(val >= 0),
    1.22 +  PLACES_DATABASE_PAGESIZE_B: function (val) do_check_eq(val, 32768),
    1.23 +  PLACES_DATABASE_SIZE_PER_PAGE_B: function (val) do_check_true(val > 0),
    1.24 +  PLACES_EXPIRATION_STEPS_TO_CLEAN2: function (val) do_check_true(val > 1),
    1.25 +  //PLACES_AUTOCOMPLETE_1ST_RESULT_TIME_MS:  function (val) do_check_true(val > 1),
    1.26 +  PLACES_IDLE_FRECENCY_DECAY_TIME_MS: function (val) do_check_true(val > 0),
    1.27 +  PLACES_IDLE_MAINTENANCE_TIME_MS: function (val) do_check_true(val > 0),
    1.28 +  PLACES_ANNOS_BOOKMARKS_COUNT: function (val) do_check_eq(val, 1),
    1.29 +  PLACES_ANNOS_BOOKMARKS_SIZE_KB: function (val) do_check_eq(val, 1),
    1.30 +  PLACES_ANNOS_PAGES_COUNT: function (val) do_check_eq(val, 1),
    1.31 +  PLACES_ANNOS_PAGES_SIZE_KB: function (val) do_check_eq(val, 1),
    1.32 +  PLACES_FRECENCY_CALC_TIME_MS: function (val) do_check_true(val >= 0),
    1.33 +}
    1.34 +
    1.35 +function run_test()
    1.36 +{
    1.37 +  run_next_test();
    1.38 +}
    1.39 +
    1.40 +add_task(function test_execute()
    1.41 +{
    1.42 +  // Put some trash in the database.
    1.43 +  const URI = NetUtil.newURI("http://moz.org/");
    1.44 +
    1.45 +  let folderId = PlacesUtils.bookmarks.createFolder(PlacesUtils.unfiledBookmarksFolderId,
    1.46 +                                                    "moz test",
    1.47 +                                                    PlacesUtils.bookmarks.DEFAULT_INDEX);
    1.48 +  let itemId = PlacesUtils.bookmarks.insertBookmark(folderId,
    1.49 +                                                    uri,
    1.50 +                                                    PlacesUtils.bookmarks.DEFAULT_INDEX,
    1.51 +                                                    "moz test");
    1.52 +  PlacesUtils.tagging.tagURI(uri, ["tag"]);
    1.53 +  PlacesUtils.bookmarks.setKeywordForBookmark(itemId, "keyword");
    1.54 +
    1.55 +  // Set a large annotation.
    1.56 +  let content = "";
    1.57 +  while (content.length < 1024) {
    1.58 +    content += "0";
    1.59 +  }
    1.60 +  PlacesUtils.annotations.setItemAnnotation(itemId, "test-anno", content, 0,
    1.61 +                                            PlacesUtils.annotations.EXPIRE_NEVER);
    1.62 +  PlacesUtils.annotations.setPageAnnotation(uri, "test-anno", content, 0,
    1.63 +                                            PlacesUtils.annotations.EXPIRE_NEVER);
    1.64 +
    1.65 +  // Request to gather telemetry data.
    1.66 +  Cc["@mozilla.org/places/categoriesStarter;1"]
    1.67 +    .getService(Ci.nsIObserver)
    1.68 +    .observe(null, "gather-telemetry", null);
    1.69 +
    1.70 +  yield promiseAsyncUpdates();
    1.71 +
    1.72 +  // Test expiration probes.
    1.73 +  for (let i = 0; i < 2; i++) {
    1.74 +    yield promiseAddVisits({
    1.75 +      uri: uri("http://" +  i + ".moz.org/"),
    1.76 +      visitDate: Date.now() // [sic]
    1.77 +    });
    1.78 +  }
    1.79 +  Services.prefs.setIntPref("places.history.expiration.max_pages", 0);
    1.80 +  let expire = Cc["@mozilla.org/places/expiration;1"].getService(Ci.nsIObserver);
    1.81 +  expire.observe(null, "places-debug-start-expiration", 1);
    1.82 +  expire.observe(null, "places-debug-start-expiration", -1);
    1.83 +
    1.84 +  // Test autocomplete probes.
    1.85 +  /*
    1.86 +  // This is useful for manual testing by changing the minimum time for
    1.87 +  // autocomplete telemetry to 0, but there is no way to artificially delay
    1.88 +  // autocomplete by more than 50ms in a realiable way.
    1.89 +  Services.prefs.setIntPref("browser.urlbar.search.sources", 3);
    1.90 +  Services.prefs.setIntPref("browser.urlbar.default.behavior", 0);
    1.91 +  function AutoCompleteInput(aSearches) {
    1.92 +    this.searches = aSearches;
    1.93 +  }
    1.94 +  AutoCompleteInput.prototype = {
    1.95 +    timeout: 10,
    1.96 +    textValue: "",
    1.97 +    searchParam: "",
    1.98 +    popupOpen: false,
    1.99 +    minResultsForPopup: 0,
   1.100 +    invalidate: function() {},
   1.101 +    disableAutoComplete: false,
   1.102 +    completeDefaultIndex: false,
   1.103 +    get popup() { return this; },
   1.104 +    onSearchBegin: function() {},
   1.105 +    onSearchComplete: function() {},
   1.106 +    setSelectedIndex: function() {},
   1.107 +    get searchCount() { return this.searches.length; },
   1.108 +    getSearchAt: function(aIndex) this.searches[aIndex],
   1.109 +    QueryInterface: XPCOMUtils.generateQI([
   1.110 +      Ci.nsIAutoCompleteInput,
   1.111 +      Ci.nsIAutoCompletePopup,
   1.112 +    ])
   1.113 +  };
   1.114 +  let controller = Cc["@mozilla.org/autocomplete/controller;1"].
   1.115 +                   getService(Ci.nsIAutoCompleteController);
   1.116 +  controller.input = new AutoCompleteInput(["history"]);
   1.117 +  controller.startSearch("moz");
   1.118 +  */
   1.119 +
   1.120 +  // Test idle probes.
   1.121 +  PlacesUtils.history.QueryInterface(Ci.nsIObserver)
   1.122 +                     .observe(null, "idle-daily", null);
   1.123 +  PlacesDBUtils.maintenanceOnIdle();
   1.124 +
   1.125 +  yield promiseTopicObserved("places-maintenance-finished");
   1.126 +
   1.127 +  for (let histogramId in histograms) {
   1.128 +    do_log_info("checking histogram " + histogramId);
   1.129 +    let validate = histograms[histogramId];
   1.130 +    let snapshot = Services.telemetry.getHistogramById(histogramId).snapshot();
   1.131 +    validate(snapshot.sum);
   1.132 +    do_check_true(snapshot.counts.reduce(function(a, b) a + b) > 0);
   1.133 +  }
   1.134 +});
   1.135 +
   1.136 +add_test(function test_healthreport_callback() {
   1.137 +  PlacesDBUtils.telemetry(null, function onResult(data) {
   1.138 +    do_check_neq(data, null);
   1.139 +
   1.140 +    do_check_eq(Object.keys(data).length, 2);
   1.141 +    do_check_eq(data.PLACES_PAGES_COUNT, 1);
   1.142 +    do_check_eq(data.PLACES_BOOKMARKS_COUNT, 1);
   1.143 +
   1.144 +    run_next_test();
   1.145 +  });
   1.146 +});
   1.147 +

mercurial