summaryrefslogtreecommitdiffstats
path: root/taskcluster/taskgraph/transforms/perftest.py
blob: dcbb9c5bcc2f9e83813c32cb840144949517babc (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
# 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/.
"""
This transform passes options from `mach perftest` to the corresponding task.
"""

from __future__ import absolute_import, print_function, unicode_literals

from copy import deepcopy
from datetime import date, timedelta
import json

from six import ensure_text, text_type

from voluptuous import (
    Any,
    Optional,
    Extra,
)

from taskgraph.transforms.base import TransformSequence
from taskgraph.util.schema import optionally_keyed_by, resolve_keyed_by, Schema
from taskgraph.util.treeherder import split_symbol, join_symbol


transforms = TransformSequence()


perftest_description_schema = Schema(
    {
        # The test names and the symbols to use for them: [test-symbol, test-path]
        Optional("perftest"): [[text_type]],
        # Metrics to gather for the test. These will be merged
        # with options specified through perftest-perfherder-global
        Optional("perftest-metrics"): optionally_keyed_by(
            "perftest",
            Any(
                [text_type],
                {text_type: Any(None, {text_type: Any(None, text_type, [text_type])})},
            ),
        ),
        # Perfherder data options that will be applied to
        # all metrics gathered.
        Optional("perftest-perfherder-global"): optionally_keyed_by(
            "perftest", {text_type: Any(None, text_type, [text_type])}
        ),
        # Extra options to add to the test's command
        Optional("perftest-extra-options"): optionally_keyed_by(
            "perftest", [text_type]
        ),
        # Variants of the test to make based on extra browsertime
        # arguments. Expecting:
        #    [variant-suffix, options-to-use]
        # If variant-suffix is `null` then the options will be added
        # to the existing task. Otherwise, a new variant is created
        # with the given suffix and with its options replaced.
        Optional("perftest-btime-variants"): optionally_keyed_by(
            "perftest", [[Any(None, text_type)]]
        ),
        # These options will be parsed in the next schemas
        Extra: object,
    }
)


transforms.add_validate(perftest_description_schema)


@transforms.add
def split_tests(config, jobs):
    for job in jobs:
        if job.get("perftest") is None:
            yield job
            continue

        for test_symbol, test_name in job.pop("perftest"):
            job_new = deepcopy(job)

            job_new["perftest"] = test_symbol
            job_new["name"] += "-" + test_symbol
            job_new["treeherder"]["symbol"] = job["treeherder"]["symbol"].format(
                symbol=test_symbol
            )
            job_new["run"]["command"] = job["run"]["command"].replace(
                "{perftest_testname}", test_name
            )

            yield job_new


@transforms.add
def handle_keyed_by_perftest(config, jobs):
    fields = ["perftest-metrics", "perftest-extra-options", "perftest-btime-variants"]
    for job in jobs:
        if job.get("perftest") is None:
            yield job
            continue

        for field in fields:
            resolve_keyed_by(job, field, item_name=job["name"])

        job.pop("perftest")
        yield job


@transforms.add
def parse_perftest_metrics(config, jobs):
    """Parse the metrics into a dictionary immediately.

    This way we can modify the extraOptions field (and others) entry through the
    transforms that come later. The metrics aren't formatted until the end of the
    transforms.
    """
    for job in jobs:
        if job.get("perftest-metrics") is None:
            yield job
            continue
        perftest_metrics = job.pop("perftest-metrics")

        # If perftest metrics is a string, split it up first
        if isinstance(perftest_metrics, list):
            new_metrics_info = [{"name": metric} for metric in perftest_metrics]
        else:
            new_metrics_info = []
            for metric, options in perftest_metrics.items():
                entry = {"name": metric}
                entry.update(options)
                new_metrics_info.append(entry)

        job["perftest-metrics"] = new_metrics_info
        yield job


@transforms.add
def split_perftest_variants(config, jobs):
    for job in jobs:
        if job.get("variants") is None:
            yield job
            continue

        for variant in job.pop("variants"):
            job_new = deepcopy(job)

            group, symbol = split_symbol(job_new["treeherder"]["symbol"])
            group += "-" + variant
            job_new["treeherder"]["symbol"] = join_symbol(group, symbol)
            job_new["name"] += "-" + variant
            job_new.setdefault("perftest-perfherder-global", {}).setdefault(
                "extraOptions", []
            ).append(variant)
            job_new[variant] = True

            yield job_new

        yield job


@transforms.add
def split_btime_variants(config, jobs):
    for job in jobs:
        if job.get("perftest-btime-variants") is None:
            yield job
            continue

        variants = job.pop("perftest-btime-variants")
        if not variants:
            yield job
            continue

        yield_existing = False
        for suffix, options in variants:
            if suffix is None:
                # Append options to the existing job
                job.setdefault("perftest-btime-variants", []).append(options)
                yield_existing = True
            else:
                job_new = deepcopy(job)
                group, symbol = split_symbol(job_new["treeherder"]["symbol"])
                symbol += "-" + suffix
                job_new["treeherder"]["symbol"] = join_symbol(group, symbol)
                job_new["name"] += "-" + suffix
                job_new.setdefault("perftest-perfherder-global", {}).setdefault(
                    "extraOptions", []
                ).append(suffix)
                # Replace the existing options with the new ones
                job_new["perftest-btime-variants"] = [options]
                yield job_new

        # The existing job has been modified so we should also return it
        if yield_existing:
            yield job


@transforms.add
def setup_http3_tests(config, jobs):
    for job in jobs:
        if job.get("http3") is None or not job.pop("http3"):
            yield job
            continue
        job.setdefault("perftest-btime-variants", []).append(
            "firefox.preference=network.http.http3.enabled:true"
        )
        yield job


@transforms.add
def setup_perftest_metrics(config, jobs):
    for job in jobs:
        if job.get("perftest-metrics") is None:
            yield job
            continue
        perftest_metrics = job.pop("perftest-metrics")

        # Options to apply to each metric
        global_options = job.pop("perftest-perfherder-global", {})
        for metric_info in perftest_metrics:
            for opt, val in global_options.items():
                if isinstance(val, list) and opt in metric_info:
                    metric_info[opt].extend(val)
                elif not (isinstance(val, list) and len(val) == 0):
                    metric_info[opt] = val

        quote_escape = '\\"'
        if "win" in job.get("platform", ""):
            # Escaping is a bit different on windows platforms
            quote_escape = '\\\\\\"'

        job["run"]["command"] = job["run"]["command"].replace(
            "{perftest_metrics}",
            " ".join(
                [
                    ",".join(
                        [
                            ":".join(
                                [
                                    option,
                                    str(value)
                                    .replace(" ", "")
                                    .replace("'", quote_escape),
                                ]
                            )
                            for option, value in metric_info.items()
                        ]
                    )
                    for metric_info in perftest_metrics
                ]
            ),
        )

        yield job


@transforms.add
def setup_perftest_browsertime_variants(config, jobs):
    for job in jobs:
        if job.get("perftest-btime-variants") is None:
            yield job
            continue

        job["run"]["command"] += " --browsertime-extra-options %s" % ",".join(
            [opt.strip() for opt in job.pop("perftest-btime-variants")]
        )

        yield job


@transforms.add
def setup_perftest_extra_options(config, jobs):
    for job in jobs:
        if job.get("perftest-extra-options") is None:
            yield job
            continue
        job["run"]["command"] += " " + " ".join(job.pop("perftest-extra-options"))
        yield job


@transforms.add
def pass_perftest_options(config, jobs):
    for job in jobs:
        env = job.setdefault("worker", {}).setdefault("env", {})
        env["PERFTEST_OPTIONS"] = ensure_text(
            json.dumps(config.params["try_task_config"].get("perftest-options"))
        )
        yield job


@transforms.add
def setup_perftest_test_date(config, jobs):
    for job in jobs:
        if (
            job.get("attributes", {}).get("batch", False)
            and "--test-date" not in job["run"]["command"]
        ):
            yesterday = (date.today() - timedelta(1)).strftime("%Y.%m.%d")
            job["run"]["command"] += " --test-date %s" % yesterday
        yield job