summaryrefslogtreecommitdiffstats
path: root/testing/jsshell/benchmark.py
blob: 70903f447f8d824f562969556dc34e6a622b975f (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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# 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/.

import six
import json
import os
import re
import shutil
import sys
from abc import ABCMeta, abstractmethod, abstractproperty
from argparse import ArgumentParser
from collections import defaultdict

from mozbuild.base import MozbuildObject, BuildEnvironmentNotFoundException
from mozprocess import ProcessHandler

here = os.path.abspath(os.path.dirname(__file__))
build = MozbuildObject.from_environment(cwd=here)

JSSHELL_NOT_FOUND = """
Could not detect a JS shell. Either make sure you have a non-artifact build
with `ac_add_options --enable-js-shell` or specify it with `--binary`.
""".strip()


@six.add_metaclass(ABCMeta)
class Benchmark(object):
    lower_is_better = True
    should_alert = True

    def __init__(self, shell, args=None, shell_name=None):
        self.shell = shell
        self.args = args
        self.shell_name = shell_name

    @abstractproperty
    def unit(self):
        """Returns the unit of measurement of the benchmark."""

    @abstractproperty
    def name(self):
        """Returns the string name of the benchmark."""

    @abstractproperty
    def path(self):
        """Return the path to the benchmark relative to topsrcdir."""

    @abstractmethod
    def process_line(self, line):
        """Process a line of stdout from the benchmark."""

    @abstractmethod
    def collect_results(self):
        """Build the result after the process has finished."""

    @property
    def command(self):
        """Returns the command to run as a list."""
        cmd = [self.shell]
        if self.args:
            cmd += self.args
        return cmd

    @property
    def version(self):
        if self._version:
            return self._version

        with open(os.path.join(self.path, "VERSION"), "r") as fh:
            self._version = fh.read().strip("\r\n\r\n \t")
        return self._version

    def reset(self):
        """Resets state between runs."""
        name = self.name
        if self.shell_name:
            name = "{}-{}".format(name, self.shell_name)

        self.perfherder_data = {
            "framework": {
                "name": "js-bench",
            },
            "suites": [
                {
                    "lowerIsBetter": self.lower_is_better,
                    "name": name,
                    "shouldAlert": self.should_alert,
                    "subtests": [],
                    "unit": self.unit,
                    "value": None,
                },
            ],
        }
        self.suite = self.perfherder_data["suites"][0]

    def _provision_benchmark_script(self):
        if os.path.isdir(self.path):
            return

        # Some benchmarks may have been downloaded from a fetch task, make
        # sure they get copied over.
        fetches_dir = os.environ.get("MOZ_FETCHES_DIR")
        if fetches_dir and os.path.isdir(fetches_dir):
            fetchdir = os.path.join(fetches_dir, self.name)
            if os.path.isdir(fetchdir):
                shutil.copytree(fetchdir, self.path)

    def run(self):
        self.reset()

        # Update the environment variables
        env = os.environ.copy()

        process_args = {
            "cmd": self.command,
            "cwd": self.path,
            "onFinish": self.collect_results,
            "processOutputLine": self.process_line,
            "stream": sys.stdout,
            "env": env,
            "universal_newlines": True,
        }
        proc = ProcessHandler(**process_args)
        proc.run()
        return proc.wait()


class RunOnceBenchmark(Benchmark):
    def collect_results(self):
        bench_total = 0
        # NOTE: for this benchmark we run the test once, so we have a single value array
        for bench, scores in self.scores.items():
            for score, values in scores.items():
                test_name = "{}-{}".format(self.name, score)
                # pylint --py3k W1619
                mean = sum(values) / len(values)
                self.suite["subtests"].append({"name": test_name, "value": mean})
                bench_total += int(sum(values))
        self.suite["value"] = bench_total


class Ares6(Benchmark):
    name = "ares6"
    path = os.path.join("third_party", "webkit", "PerformanceTests", "ARES-6")
    unit = "ms"

    @property
    def command(self):
        cmd = super(Ares6, self).command
        return cmd + ["cli.js"]

    def reset(self):
        super(Ares6, self).reset()

        self.bench_name = None
        self.last_summary = None
        # Scores are of the form:
        # {<bench_name>: {<score_name>: [<values>]}}
        self.scores = defaultdict(lambda: defaultdict(list))

    def _try_find_score(self, score_name, line):
        m = re.search(score_name + ":\s*(\d+\.?\d*?) (\+-)?.+", line)
        if not m:
            return False

        score = m.group(1)
        self.scores[self.bench_name][score_name].append(float(score))
        return True

    def process_line(self, line):
        m = re.search("Running... (.+) \(.+\)", line)
        if m:
            self.bench_name = m.group(1)
            return

        if self._try_find_score("firstIteration", line):
            return

        if self._try_find_score("averageWorstCase", line):
            return

        if self._try_find_score("steadyState", line):
            return

        m = re.search("summary:\s*(\d+\.?\d*?) (\+-)?.+", line)
        if m:
            self.last_summary = float(m.group(1))

    def collect_results(self):
        for bench, scores in self.scores.items():
            for score, values in scores.items():
                # pylint --py3k W1619
                mean = sum(values) / len(values)
                test_name = "{}-{}".format(bench, score)
                self.suite["subtests"].append({"name": test_name, "value": mean})

        if self.last_summary:
            self.suite["value"] = self.last_summary


class SixSpeed(RunOnceBenchmark):
    name = "six-speed"
    path = os.path.join("third_party", "webkit", "PerformanceTests", "six-speed")
    unit = "ms"

    @property
    def command(self):
        cmd = super(SixSpeed, self).command
        return cmd + ["test.js"]

    def reset(self):
        super(SixSpeed, self).reset()

        # Scores are of the form:
        # {<bench_name>: {<score_name>: [<values>]}}
        self.scores = defaultdict(lambda: defaultdict(list))

    def process_line(self, output):
        m = re.search("(.+): (\d+)", output)
        if not m:
            return
        subtest = m.group(1)
        score = m.group(2)
        if subtest not in self.scores[self.name]:
            self.scores[self.name][subtest] = []
        self.scores[self.name][subtest].append(int(score))


class SunSpider(RunOnceBenchmark):
    name = "sunspider"
    path = os.path.join(
        "third_party", "webkit", "PerformanceTests", "SunSpider", "sunspider-0.9.1"
    )
    unit = "ms"

    @property
    def command(self):
        cmd = super(SunSpider, self).command
        return cmd + ["sunspider-standalone-driver.js"]

    def reset(self):
        super(SunSpider, self).reset()

        # Scores are of the form:
        # {<bench_name>: {<score_name>: [<values>]}}
        self.scores = defaultdict(lambda: defaultdict(list))

    def process_line(self, output):
        m = re.search("(.+): (\d+)", output)
        if not m:
            return
        subtest = m.group(1)
        score = m.group(2)
        if subtest not in self.scores[self.name]:
            self.scores[self.name][subtest] = []
        self.scores[self.name][subtest].append(int(score))


class WebToolingBenchmark(Benchmark):
    name = "web-tooling-benchmark"
    path = os.path.join(
        "third_party", "webkit", "PerformanceTests", "web-tooling-benchmark"
    )
    main_js = "cli.js"
    unit = "score"
    lower_is_better = False
    subtests_lower_is_better = False

    @property
    def command(self):
        cmd = super(WebToolingBenchmark, self).command
        return cmd + [self.main_js]

    def reset(self):
        super(WebToolingBenchmark, self).reset()

        # Scores are of the form:
        # {<bench_name>: {<score_name>: [<values>]}}
        self.scores = defaultdict(lambda: defaultdict(list))

    def process_line(self, output):
        m = re.search(" +([a-zA-Z].+): +([.0-9]+) +runs/sec", output)
        if not m:
            return
        subtest = m.group(1)
        score = m.group(2)
        if subtest not in self.scores[self.name]:
            self.scores[self.name][subtest] = []
        self.scores[self.name][subtest].append(float(score))

    def collect_results(self):
        # NOTE: for this benchmark we run the test once, so we have a single value array
        bench_mean = None
        for bench, scores in self.scores.items():
            for score_name, values in scores.items():
                test_name = "{}-{}".format(self.name, score_name)
                # pylint --py3k W1619
                mean = sum(values) / len(values)
                self.suite["subtests"].append(
                    {
                        "lowerIsBetter": self.subtests_lower_is_better,
                        "name": test_name,
                        "value": mean,
                    }
                )
                if score_name == "mean":
                    bench_mean = mean
        self.suite["value"] = bench_mean

    def run(self):
        self._provision_benchmark_script()
        return super(WebToolingBenchmark, self).run()


class Octane(RunOnceBenchmark):
    name = "octane"
    path = os.path.join("third_party", "webkit", "PerformanceTests", "octane")
    unit = "score"
    lower_is_better = False

    @property
    def command(self):
        cmd = super(Octane, self).command
        return cmd + ["run.js"]

    def reset(self):
        super(Octane, self).reset()

        # Scores are of the form:
        # {<bench_name>: {<score_name>: [<values>]}}
        self.scores = defaultdict(lambda: defaultdict(list))

    def process_line(self, output):
        m = re.search("(.+): (\d+)", output)
        if not m:
            return
        subtest = m.group(1)
        score = m.group(2)
        if subtest.startswith("Score"):
            subtest = "score"
        if subtest not in self.scores[self.name]:
            self.scores[self.name][subtest] = []
        self.scores[self.name][subtest].append(int(score))

    def collect_results(self):
        bench_score = None
        # NOTE: for this benchmark we run the test once, so we have a single value array
        for bench, scores in self.scores.items():
            for score_name, values in scores.items():
                test_name = "{}-{}".format(self.name, score_name)
                # pylint --py3k W1619
                mean = sum(values) / len(values)
                self.suite["subtests"].append({"name": test_name, "value": mean})
                if score_name == "score":
                    bench_score = mean
        self.suite["value"] = bench_score

    def run(self):
        self._provision_benchmark_script()
        return super(Octane, self).run()


all_benchmarks = {
    "ares6": Ares6,
    "six-speed": SixSpeed,
    "sunspider": SunSpider,
    "web-tooling-benchmark": WebToolingBenchmark,
    "octane": Octane,
}


def run(benchmark, binary=None, extra_args=None, perfherder=None):
    if not binary:
        try:
            binary = os.path.join(build.bindir, "js" + build.substs["BIN_SUFFIX"])
        except BuildEnvironmentNotFoundException:
            binary = None

        if not binary or not os.path.isfile(binary):
            print(JSSHELL_NOT_FOUND)
            return 1

    bench = all_benchmarks.get(benchmark)(
        binary, args=extra_args, shell_name=perfherder
    )
    res = bench.run()

    if perfherder:
        print("PERFHERDER_DATA: {}".format(json.dumps(bench.perfherder_data)))
    return res


def get_parser():
    parser = ArgumentParser()
    parser.add_argument(
        "benchmark",
        choices=list(all_benchmarks),
        help="The name of the benchmark to run.",
    )
    parser.add_argument(
        "-b", "--binary", default=None, help="Path to the JS shell binary to use."
    )
    parser.add_argument(
        "--arg",
        dest="extra_args",
        action="append",
        default=None,
        help="Extra arguments to pass to the JS shell.",
    )
    parser.add_argument(
        "--perfherder",
        default=None,
        help="Log PERFHERDER_DATA to stdout using the given suite name.",
    )
    return parser


def cli(args=sys.argv[1:]):
    parser = get_parser()
    args = parser.parser_args(args)
    return run(**vars(args))


if __name__ == "__main__":
    sys.exit(cli())