summaryrefslogtreecommitdiffstats
path: root/intl/benchmarks/perftest_pluralRules.js
blob: 956dd0956117c7b0d069c100a3110c4c7c7531fc (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// @ts-check

var perfMetadata = {
  owner: "Internationalization Team",
  name: "Intl.PluralRules",
  description: "Test the speed of the Intl.PluralRules implementation.",
  options: {
    default: {
      perfherder: true,
      perfherder_metrics: [
        {
          name: "Intl.PluralRules constructor iterations",
          unit: "iterations",
        },
        { name: "Intl.PluralRules constructor accumulatedTime", unit: "ms" },
        { name: "Intl.PluralRules constructor perCallTime", unit: "ms" },

        {
          name: "Intl.PluralRules.prototype.select iterations",
          unit: "iterations",
        },
        {
          name: "Intl.PluralRules.prototype.select accumulatedTime",
          unit: "ms",
        },
        {
          name: "Intl.PluralRules.prototype.select perCallTime",
          unit: "ms",
        },

        {
          name: "Intl.PluralRules pluralCategories iterations",
          unit: "iterations",
        },
        {
          name: "Intl.PluralRules pluralCategories accumulatedTime",
          unit: "ms",
        },
        {
          name: "Intl.PluralRules pluralCategories perCallTime",
          unit: "ms",
        },
      ],
      verbose: true,
    },
  },
  tags: ["intl", "ecma402"],
};

add_task(function measure_pluralrules() {
  const measureConstructor = measureIterations("Intl.PluralRules constructor");
  const measureSelect = measureIterations("Intl.PluralRules.prototype.select");
  const measurePluralCategories = measureIterations(
    "Intl.PluralRules pluralCategories"
  );

  // Re-use the config between runs.

  const fieldOptions = {
    type: ["cardinal", "ordinal"],
  };

  const config = {};
  function randomizeConfig(name, chance) {
    const option = fieldOptions[name];
    if (prng() < chance) {
      config[name] = option[Math.floor(option.length * prng())];
    } else {
      delete config[name];
    }
  }

  // Split each step of the benchmark into separate JS functions so that performance
  // profiles are easy to analyze.

  function benchmarkPluralRulesConstructor() {
    for (let i = 0; i < 1000; i++) {
      // Create a random configuration powered by a pseudo-random number generator. This
      // way the configurations will be the same between 2 different runs.
      const locale = pickRepresentativeLocale();
      randomizeConfig("type", 0.5);

      // Measure the constructor.
      measureConstructor.start();
      const pr = new Intl.PluralRules(locale, config);
      measureConstructor.stop();

      benchmarkSelectOperation(pr);
      benchmarkPluralCategories(pr);
    }
  }

  function benchmarkSelectOperation(pr) {
    // Measure the select operation.
    for (let j = 0; j < 1000; j++) {
      // TODO: We may want to extend this to non-integer values in the future.
      const num = Math.floor(prng() * 10000);
      measureSelect.start();
      pr.select(num);
      measureSelect.stop();
    }
  }

  function benchmarkPluralCategories(pr) {
    measurePluralCategories.start();
    pr.resolvedOptions().pluralCategories;
    measurePluralCategories.stop();
  }

  benchmarkPluralRulesConstructor();
  measureConstructor.reportMetrics();
  measureSelect.reportMetrics();
  measurePluralCategories.reportMetrics();

  ok(true);
});