tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

head.js (3190B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 /**
      6 * Create an interface to measure iterations for a micro benchmark. These iterations
      7 * will then be reported to the perftest runner.
      8 *
      9 * @param {string} metricName
     10 */
     11 function measureIterations(metricName) {
     12  let accumulatedTime = 0;
     13  let iterations = 0;
     14  let now = 0;
     15  return {
     16    /**
     17     * Start a measurement.
     18     */
     19    start() {
     20      now = ChromeUtils.now();
     21    },
     22    /**
     23     * Stop a measurement, and record the elapsed time.
     24     */
     25    stop() {
     26      accumulatedTime += ChromeUtils.now() - now;
     27      iterations++;
     28    },
     29    /**
     30     * Report the metrics to perftest after finishing the microbenchmark.
     31     */
     32    reportMetrics() {
     33      const metrics = {};
     34      metrics[metricName + " iterations"] = iterations;
     35      metrics[metricName + " accumulatedTime"] = accumulatedTime;
     36      metrics[metricName + " perCallTime"] = accumulatedTime / iterations;
     37 
     38      info("perfMetrics", metrics);
     39    },
     40  };
     41 }
     42 
     43 let _seed = 123456;
     44 /**
     45 * A cheap and simple pseudo-random number generator that avoids adding new dependencies.
     46 * This function ensures tests are repeatable, but can be fed random configurations.
     47 *
     48 * https://en.wikipedia.org/wiki/Linear_congruential_generator
     49 *
     50 * It has the following distribution for the first 100,000 runs:
     51 *
     52 *    0.0 - 0.1: 9948
     53 *    0.1 - 0.2: 10037
     54 *    0.2 - 0.3: 10049
     55 *    0.3 - 0.4: 10041
     56 *    0.4 - 0.5: 10036
     57 *    0.5 - 0.6: 10085
     58 *    0.6 - 0.7: 9987
     59 *    0.7 - 0.8: 9872
     60 *    0.8 - 0.9: 10007
     61 *    0.9 - 1.0: 9938
     62 *
     63 * @returns {number} float values ranged 0-1
     64 */
     65 function prng() {
     66  _seed = Math.imul(_seed, 22695477) + 1;
     67  return (_seed >> 1) / 0x7fffffff + 0.5;
     68 }
     69 
     70 /**
     71 * The distribution of locales. The number represents the ratio of total users in that
     72 * locale. The numbers should add up to ~1.0.
     73 *
     74 * https://sql.telemetry.mozilla.org/dashboard/firefox-localization
     75 */
     76 const localeDistribution = {
     77  "en-US": 0.373,
     78  de: 0.129,
     79  fr: 0.084,
     80  "zh-CN": 0.053,
     81  ru: 0.048,
     82  "es-ES": 0.047,
     83  pl: 0.041,
     84  "pt-BR": 0.034,
     85  it: 0.028,
     86  "en-GB": 0.027,
     87  ja: 0.019,
     88  "es-MX": 0.014,
     89  nl: 0.01,
     90  cs: 0.009,
     91  hu: 0.008,
     92  id: 0.006,
     93  "en-CA": 0.006,
     94  "es-AR": 0.006,
     95  tr: 0.005,
     96  el: 0.005,
     97  "zh-TW": 0.005,
     98  fi: 0.005,
     99  "sv-SE": 0.004,
    100  "pt-PT": 0.004,
    101  sk: 0.003,
    102  ar: 0.003,
    103  vi: 0.003,
    104  "es-CL": 0.002,
    105  th: 0.002,
    106  da: 0.002,
    107  bg: 0.002,
    108  ro: 0.002,
    109  "nb-NO": 0.002,
    110  ko: 0.002,
    111 };
    112 
    113 /**
    114 * Go through the top Firefox locales, and pick one at random that is representative
    115 * of the Firefox population as of 2021-06-03. It uses a pseudo-random number generator
    116 * to make the results repeatable.
    117 *
    118 * @returns {string} locale
    119 */
    120 function pickRepresentativeLocale() {
    121  const n = prng();
    122  let ratio = 1;
    123  for (const [locale, representation] of Object.entries(localeDistribution)) {
    124    ratio -= representation;
    125    if (n > ratio) {
    126      return locale;
    127    }
    128  }
    129  // In case we fall through the "for" loop, return the most common locale.
    130  return "en-US";
    131 }