tor-browser

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

perftest_pluralRules.js (3478B)


      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 // @ts-check
      5 
      6 var perfMetadata = {
      7  owner: "Internationalization Team",
      8  name: "Intl.PluralRules",
      9  description: "Test the speed of the Intl.PluralRules implementation.",
     10  options: {
     11    default: {
     12      perfherder: true,
     13      perfherder_metrics: [
     14        {
     15          name: "Intl.PluralRules constructor iterations",
     16          unit: "iterations",
     17        },
     18        { name: "Intl.PluralRules constructor accumulatedTime", unit: "ms" },
     19        { name: "Intl.PluralRules constructor perCallTime", unit: "ms" },
     20 
     21        {
     22          name: "Intl.PluralRules.prototype.select iterations",
     23          unit: "iterations",
     24        },
     25        {
     26          name: "Intl.PluralRules.prototype.select accumulatedTime",
     27          unit: "ms",
     28        },
     29        {
     30          name: "Intl.PluralRules.prototype.select perCallTime",
     31          unit: "ms",
     32        },
     33 
     34        {
     35          name: "Intl.PluralRules pluralCategories iterations",
     36          unit: "iterations",
     37        },
     38        {
     39          name: "Intl.PluralRules pluralCategories accumulatedTime",
     40          unit: "ms",
     41        },
     42        {
     43          name: "Intl.PluralRules pluralCategories perCallTime",
     44          unit: "ms",
     45        },
     46      ],
     47      verbose: true,
     48    },
     49  },
     50  tags: ["intl", "ecma402"],
     51 };
     52 
     53 add_task(function measure_pluralrules() {
     54  const measureConstructor = measureIterations("Intl.PluralRules constructor");
     55  const measureSelect = measureIterations("Intl.PluralRules.prototype.select");
     56  const measurePluralCategories = measureIterations(
     57    "Intl.PluralRules pluralCategories"
     58  );
     59 
     60  // Re-use the config between runs.
     61 
     62  const fieldOptions = {
     63    type: ["cardinal", "ordinal"],
     64  };
     65 
     66  const config = {};
     67  function randomizeConfig(name, chance) {
     68    const option = fieldOptions[name];
     69    if (prng() < chance) {
     70      config[name] = option[Math.floor(option.length * prng())];
     71    } else {
     72      delete config[name];
     73    }
     74  }
     75 
     76  // Split each step of the benchmark into separate JS functions so that performance
     77  // profiles are easy to analyze.
     78 
     79  function benchmarkPluralRulesConstructor() {
     80    for (let i = 0; i < 1000; i++) {
     81      // Create a random configuration powered by a pseudo-random number generator. This
     82      // way the configurations will be the same between 2 different runs.
     83      const locale = pickRepresentativeLocale();
     84      randomizeConfig("type", 0.5);
     85 
     86      // Measure the constructor.
     87      measureConstructor.start();
     88      const pr = new Intl.PluralRules(locale, config);
     89      measureConstructor.stop();
     90 
     91      benchmarkSelectOperation(pr);
     92      benchmarkPluralCategories(pr);
     93    }
     94  }
     95 
     96  function benchmarkSelectOperation(pr) {
     97    // Measure the select operation.
     98    for (let j = 0; j < 1000; j++) {
     99      // TODO: We may want to extend this to non-integer values in the future.
    100      const num = Math.floor(prng() * 10000);
    101      measureSelect.start();
    102      pr.select(num);
    103      measureSelect.stop();
    104    }
    105  }
    106 
    107  function benchmarkPluralCategories(pr) {
    108    measurePluralCategories.start();
    109    pr.resolvedOptions().pluralCategories;
    110    measurePluralCategories.stop();
    111  }
    112 
    113  benchmarkPluralRulesConstructor();
    114  measureConstructor.reportMetrics();
    115  measureSelect.reportMetrics();
    116  measurePluralCategories.reportMetrics();
    117 
    118  ok(true);
    119 });