summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/performance/browser_preferences_usage.js
blob: d62cb2316bed11d8a00a7e05826070422245a920 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

if (SpecialPowers.useRemoteSubframes) {
  requestLongerTimeout(2);
}

const DEFAULT_PROCESS_COUNT = Services.prefs
  .getDefaultBranch(null)
  .getIntPref("dom.ipc.processCount");

/**
 * A test that checks whether any preference getter from the given list
 * of stats was called more often than the max parameter.
 *
 * @param {Array}  stats - an array of [prefName, accessCount] tuples
 * @param {Number} max - the maximum number of times any of the prefs should
 *                 have been called.
 * @param {Object} knownProblematicPrefs (optional) - an object that defines
 *                 prefs that should be exempt from checking the
 *                 maximum access. It looks like the following:
 *
 *                 pref_name: {
 *                   min: [Number] the minimum amount of times this should have
 *                                 been called (to avoid keeping around dead items)
 *                   max: [Number] the maximum amount of times this should have
 *                                 been called (to avoid this creeping up further)
 *                 }
 */
function checkPrefGetters(stats, max, knownProblematicPrefs = {}) {
  let getterStats = Object.entries(stats).sort(
    ([, val1], [, val2]) => val2 - val1
  );

  // Clone the list to be able to delete entries to check if we
  // forgot any later on.
  knownProblematicPrefs = Object.assign({}, knownProblematicPrefs);

  for (let [pref, count] of getterStats) {
    let prefLimits = knownProblematicPrefs[pref];
    if (!prefLimits) {
      Assert.lessOrEqual(
        count,
        max,
        `${pref} should not be accessed more than ${max} times.`
      );
    } else {
      // Still record how much this pref was accessed even if we don't do any real assertions.
      if (!prefLimits.min && !prefLimits.max) {
        info(
          `${pref} should not be accessed more than ${max} times and was accessed ${count} times.`
        );
      }

      if (prefLimits.min) {
        Assert.lessOrEqual(
          prefLimits.min,
          count,
          `${pref} should be accessed at least ${prefLimits.min} times.`
        );
      }
      if (prefLimits.max) {
        Assert.lessOrEqual(
          count,
          prefLimits.max,
          `${pref} should be accessed at most ${prefLimits.max} times.`
        );
      }
      delete knownProblematicPrefs[pref];
    }
  }

  // This pref will be accessed by mozJSComponentLoader when loading modules,
  // which fails TV runs since they run the test multiple times without restarting.
  // We just ignore this pref, since it's for testing only anyway.
  if (knownProblematicPrefs["browser.startup.record"]) {
    delete knownProblematicPrefs["browser.startup.record"];
  }

  let unusedPrefs = Object.keys(knownProblematicPrefs);
  is(
    unusedPrefs.length,
    0,
    `Should have accessed all known problematic prefs. Remaining: ${unusedPrefs}`
  );
}

/**
 * A helper function to read preference access data
 * using the Services.prefs.readStats() function.
 */
function getPreferenceStats() {
  let stats = {};
  Services.prefs.readStats((key, value) => (stats[key] = value));
  return stats;
}

add_task(async function debug_only() {
  ok(AppConstants.DEBUG, "You need to run this test on a debug build.");
});

// Just checks how many prefs were accessed during startup.
add_task(async function startup() {
  let max = 40;

  let knownProblematicPrefs = {
    "browser.startup.record": {
      // This pref is accessed in Nighly and debug builds only.
      min: 200,
      max: 400,
    },
    "network.loadinfo.skip_type_assertion": {
      // This is accessed in debug only.
    },
    "chrome.override_package.global": {
      min: 0,
      max: 50,
    },
  };

  let startupRecorder =
    Cc["@mozilla.org/test/startuprecorder;1"].getService().wrappedJSObject;
  await startupRecorder.done;

  ok(startupRecorder.data.prefStats, "startupRecorder has prefStats");

  checkPrefGetters(startupRecorder.data.prefStats, max, knownProblematicPrefs);
});

// This opens 10 tabs and checks pref getters.
add_task(async function open_10_tabs() {
  // This is somewhat arbitrary. When we had a default of 4 content processes
  // the value was 15. We need to scale it as we increase the number of
  // content processes so we approximate with 4 * process_count.
  const max = 4 * DEFAULT_PROCESS_COUNT;

  let knownProblematicPrefs = {
    "browser.startup.record": {
      max: 20,
    },
    "browser.tabs.remote.logSwitchTiming": {
      max: 35,
    },
    "network.loadinfo.skip_type_assertion": {
      // This is accessed in debug only.
    },
  };

  Services.prefs.resetStats();

  let tabs = [];
  while (tabs.length < 10) {
    tabs.push(
      await BrowserTestUtils.openNewForegroundTab(
        gBrowser,
        // eslint-disable-next-line @microsoft/sdl/no-insecure-url
        "http://example.com",
        true,
        true
      )
    );
  }

  for (let tab of tabs) {
    await BrowserTestUtils.removeTab(tab);
  }

  checkPrefGetters(getPreferenceStats(), max, knownProblematicPrefs);
});

// This navigates to 50 sites and checks pref getters.
add_task(async function navigate_around() {
  await SpecialPowers.pushPrefEnv({
    set: [
      // Disable bfcache so that we can measure more accurately the number of
      // pref accesses in the child processes.
      // If bfcache is enabled on Fission
      // dom.ipc.keepProcessesAlive.webIsolated.perOrigin and
      // security.sandbox.content.force-namespace are accessed only a couple of
      // times.
      ["browser.sessionhistory.max_total_viewers", 0],
    ],
  });

  let max = 40;

  let knownProblematicPrefs = {
    "network.loadinfo.skip_type_assertion": {
      // This is accessed in debug only.
    },
  };

  if (Services.prefs.getBoolPref("browser.translations.enable")) {
    // The translations pref logs the translation decision on each DOMContentLoaded,
    // and only shows the log by the preferences set in the console.createInstance.
    // See Bug 1835693. This means that it is invoked on each page load.
    knownProblematicPrefs["browser.translations.logLevel"] = {
      min: 50,
      max: 50,
    };
  }

  if (SpecialPowers.useRemoteSubframes) {
    // We access this when considering starting a new content process.
    // Because there is no complete list of content process types,
    // caching this is not trivial. Opening 50 different content
    // processes and throwing them away immediately is a bit artificial;
    // we're more likely to keep some around so this shouldn't be quite
    // this bad in practice. Fixing this is
    // https://bugzilla.mozilla.org/show_bug.cgi?id=1600266
    knownProblematicPrefs["dom.ipc.processCount.webIsolated"] = {
      min: 50,
      max: 51,
    };
    // This pref is only accessed in automation to speed up tests.
    knownProblematicPrefs["dom.ipc.keepProcessesAlive.webIsolated.perOrigin"] =
      {
        min: 100,
        max: 102,
      };
    if (AppConstants.platform == "linux") {
      // The following sandbox pref is covered by
      // https://bugzilla.mozilla.org/show_bug.cgi?id=1600189
      knownProblematicPrefs["security.sandbox.content.force-namespace"] = {
        min: 45,
        max: 55,
      };
    } else if (AppConstants.platform == "win") {
      // The following 2 graphics prefs are covered by
      // https://bugzilla.mozilla.org/show_bug.cgi?id=1639497
      knownProblematicPrefs["gfx.canvas.azure.backends"] = {
        min: 90,
        max: 110,
      };
      knownProblematicPrefs["gfx.content.azure.backends"] = {
        min: 90,
        max: 110,
      };
      // The following 2 sandbox prefs are covered by
      // https://bugzilla.mozilla.org/show_bug.cgi?id=1639494
      knownProblematicPrefs["security.sandbox.content.read_path_whitelist"] = {
        min: 47,
        max: 55,
      };
      knownProblematicPrefs["security.sandbox.logging.enabled"] = {
        min: 47,
        max: 55,
      };
    }
  }

  Services.prefs.resetStats();

  let tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    // eslint-disable-next-line @microsoft/sdl/no-insecure-url
    "http://example.com",
    true,
    true
  );

  let urls = [
    // eslint-disable-next-line @microsoft/sdl/no-insecure-url
    "http://example.com/",
    "https://example.com/",
    // eslint-disable-next-line @microsoft/sdl/no-insecure-url
    "http://example.org/",
    "https://example.org/",
  ];

  for (let i = 0; i < 50; i++) {
    let url = urls[i % urls.length];
    info(`Navigating to ${url}...`);
    BrowserTestUtils.loadURIString(tab.linkedBrowser, url);
    await BrowserTestUtils.browserLoaded(tab.linkedBrowser, false, url);
    info(`Loaded ${url}.`);
  }

  await BrowserTestUtils.removeTab(tab);

  checkPrefGetters(getPreferenceStats(), max, knownProblematicPrefs);
});