summaryrefslogtreecommitdiffstats
path: root/testing/awsy/mach_commands.py
blob: 7b17c636296aecbbadfda017297e2e3cbce4f449 (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
# 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 argparse
import logging
import os
import sys

import mozinfo
import six
from mach.decorators import Command, CommandArgument, CommandArgumentGroup
from mozbuild.base import BinaryNotFoundException
from mozbuild.base import MachCommandConditions as conditions


def setup_awsy_argument_parser():
    from marionette_harness.runtests import MarionetteArguments
    from mozlog.structured import commandline

    parser = MarionetteArguments()
    commandline.add_logging_group(parser)

    return parser


from awsy import ITERATIONS, MAX_TABS, PER_TAB_PAUSE, SETTLE_WAIT_TIME


def run_awsy(command_context, tests, binary=None, **kwargs):
    import json

    from marionette_harness.runtests import MarionetteHarness, MarionetteTestRunner
    from mozlog.structured import commandline

    parser = setup_awsy_argument_parser()

    awsy_source_dir = os.path.join(command_context.topsrcdir, "testing", "awsy")
    if not tests:
        tests = [os.path.join(awsy_source_dir, "awsy", "test_memory_usage.py")]

    args = argparse.Namespace(tests=tests)

    args.binary = binary

    if kwargs["quick"]:
        kwargs["entities"] = 3
        kwargs["iterations"] = 1
        kwargs["perTabPause"] = 1
        kwargs["settleWaitTime"] = 1

    runtime_testvars = {}
    for arg in (
        "webRootDir",
        "pageManifest",
        "resultsDir",
        "entities",
        "iterations",
        "perTabPause",
        "settleWaitTime",
        "maxTabs",
        "dmd",
        "tp6",
    ):
        if arg in kwargs and kwargs[arg] is not None:
            runtime_testvars[arg] = kwargs[arg]

    if "webRootDir" not in runtime_testvars:
        awsy_tests_dir = os.path.join(command_context.topobjdir, "_tests", "awsy")
        web_root_dir = os.path.join(awsy_tests_dir, "html")
        runtime_testvars["webRootDir"] = web_root_dir
    else:
        web_root_dir = runtime_testvars["webRootDir"]
        awsy_tests_dir = os.path.dirname(web_root_dir)

    if "resultsDir" not in runtime_testvars:
        runtime_testvars["resultsDir"] = os.path.join(awsy_tests_dir, "results")

    runtime_testvars["bin"] = binary
    runtime_testvars["run_local"] = True

    page_load_test_dir = os.path.join(web_root_dir, "page_load_test")
    if not os.path.isdir(page_load_test_dir):
        os.makedirs(page_load_test_dir)

    if not os.path.isdir(runtime_testvars["resultsDir"]):
        os.makedirs(runtime_testvars["resultsDir"])

    runtime_testvars_path = os.path.join(awsy_tests_dir, "runtime-testvars.json")
    if kwargs["testvars"]:
        kwargs["testvars"].append(runtime_testvars_path)
    else:
        kwargs["testvars"] = [runtime_testvars_path]

    runtime_testvars_file = open(runtime_testvars_path, "wb" if six.PY2 else "w")
    runtime_testvars_file.write(json.dumps(runtime_testvars, indent=2))
    runtime_testvars_file.close()

    manifest_file = os.path.join(awsy_source_dir, "tp5n-pageset.manifest")
    tooltool_args = {
        "args": [
            sys.executable,
            os.path.join(command_context.topsrcdir, "mach"),
            "artifact",
            "toolchain",
            "-v",
            "--tooltool-manifest=%s" % manifest_file,
            "--cache-dir=%s"
            % os.path.join(command_context.topsrcdir, "tooltool-cache"),
        ]
    }
    command_context.run_process(cwd=page_load_test_dir, **tooltool_args)
    tp5nzip = os.path.join(page_load_test_dir, "tp5n.zip")
    tp5nmanifest = os.path.join(page_load_test_dir, "tp5n", "tp5n.manifest")
    if not os.path.exists(tp5nmanifest):
        unzip_args = {"args": ["unzip", "-q", "-o", tp5nzip, "-d", page_load_test_dir]}
        try:
            command_context.run_process(**unzip_args)
        except Exception as exc:
            troubleshoot = ""
            if mozinfo.os == "win":
                troubleshoot = (
                    " Try using --web-root to specify a "
                    "directory closer to the drive root."
                )

            command_context.log(
                logging.ERROR,
                "awsy",
                {"directory": page_load_test_dir, "exception": exc},
                "Failed to unzip `tp5n.zip` into "
                "`{directory}` with `{exception}`." + troubleshoot,
            )
            raise exc

    # If '--preferences' was not specified supply our default set.
    if not kwargs["prefs_files"]:
        kwargs["prefs_files"] = [os.path.join(awsy_source_dir, "conf", "prefs.json")]

    # Setup DMD env vars if necessary.
    if kwargs["dmd"]:
        bin_dir = os.path.dirname(binary)

        if "DMD" not in os.environ:
            os.environ["DMD"] = "1"

        # Work around a startup crash with DMD on windows
        if mozinfo.os == "win":
            kwargs["pref"] = "security.sandbox.content.level:0"
            command_context.log(
                logging.WARNING,
                "awsy",
                {},
                "Forcing 'security.sandbox.content.level' = 0 because DMD is enabled.",
            )
        elif mozinfo.os == "mac":
            # On mac binary is in MacOS and dmd.py is in Resources, ie:
            #   Name.app/Contents/MacOS/libdmd.dylib
            #   Name.app/Contents/Resources/dmd.py
            bin_dir = os.path.join(bin_dir, "../Resources/")

        # Also add the bin dir to the python path so we can use dmd.py
        if bin_dir not in sys.path:
            sys.path.append(bin_dir)

    for k, v in six.iteritems(kwargs):
        setattr(args, k, v)

    parser.verify_usage(args)

    args.logger = commandline.setup_logging(
        "Are We Slim Yet Tests", args, {"mach": sys.stdout}
    )
    failed = MarionetteHarness(MarionetteTestRunner, args=vars(args)).run()
    if failed > 0:
        return 1
    else:
        return 0


@Command(
    "awsy-test",
    category="testing",
    description="Run Are We Slim Yet (AWSY) memory usage testing using marionette.",
    parser=setup_awsy_argument_parser,
)
@CommandArgumentGroup("AWSY")
@CommandArgument(
    "--web-root",
    group="AWSY",
    action="store",
    type=str,
    dest="webRootDir",
    help="Path to web server root directory. If not specified, "
    "defaults to topobjdir/_tests/awsy/html.",
)
@CommandArgument(
    "--page-manifest",
    group="AWSY",
    action="store",
    type=str,
    dest="pageManifest",
    help="Path to page manifest text file containing a list "
    "of urls to test. The urls must be served from localhost. If not "
    "specified, defaults to page_load_test/tp5n/tp5n.manifest under "
    "the web root.",
)
@CommandArgument(
    "--results",
    group="AWSY",
    action="store",
    type=str,
    dest="resultsDir",
    help="Path to results directory. If not specified, defaults "
    "to the parent directory of the web root.",
)
@CommandArgument(
    "--quick",
    group="AWSY",
    action="store_true",
    dest="quick",
    default=False,
    help="Set --entities=3, --iterations=1, --per-tab-pause=1, "
    "--settle-wait-time=1 for a quick test. Overrides any explicit "
    "argument settings.",
)
@CommandArgument(
    "--entities",
    group="AWSY",
    action="store",
    type=int,
    dest="entities",
    help="Number of urls to load. Defaults to the total number of urls.",
)
@CommandArgument(
    "--max-tabs",
    group="AWSY",
    action="store",
    type=int,
    dest="maxTabs",
    help="Maximum number of tabs to open. Defaults to %s." % MAX_TABS,
)
@CommandArgument(
    "--iterations",
    group="AWSY",
    action="store",
    type=int,
    dest="iterations",
    help="Number of times to run through the test suite. "
    "Defaults to %s." % ITERATIONS,
)
@CommandArgument(
    "--per-tab-pause",
    group="AWSY",
    action="store",
    type=int,
    dest="perTabPause",
    help="Seconds to wait in between opening tabs. Defaults to %s." % PER_TAB_PAUSE,
)
@CommandArgument(
    "--settle-wait-time",
    group="AWSY",
    action="store",
    type=int,
    dest="settleWaitTime",
    help="Seconds to wait for things to settled down. "
    "Defaults to %s." % SETTLE_WAIT_TIME,
)
@CommandArgument(
    "--dmd",
    group="AWSY",
    action="store_true",
    dest="dmd",
    default=False,
    help="Enable DMD during testing. Requires a DMD-enabled build.",
)
@CommandArgument(
    "--tp6",
    group="AWSY",
    action="store_true",
    dest="tp6",
    default=False,
    help="Use the tp6 pageset during testing.",
)
def run_awsy_test(command_context, tests, **kwargs):
    """mach awsy-test runs the in-tree version of the Are We Slim Yet
    (AWSY) tests.

    awsy-test is implemented as a marionette test and marionette
    test arguments also apply although they are not necessary
    since reasonable defaults will be chosen.

    The AWSY specific arguments can be found in the Command
    Arguments for AWSY section below.

    awsy-test will automatically download the tp5n.zip talos
    pageset from tooltool and install it under
    topobjdir/_tests/awsy/html. You can specify your own page set
    by specifying --web-root and --page-manifest.

    The results of the test will be placed in the results
    directory specified by the --results argument.

    On Windows, you may experience problems due to path length
    errors when extracting the tp5n.zip file containing the
    test pages or when attempting to write checkpoints to the
    results directory. In that case, you should specify both
    the --web-root and --results arguments pointing to a location
    with a short path. For example:

    --web-root=c:\\\\tmp\\\\html --results=c:\\\\tmp\\\\results

    Note that the double backslashes are required.
    """
    kwargs["logger_name"] = "Awsy Tests"
    if "test_objects" in kwargs:
        tests = []
        for obj in kwargs["test_objects"]:
            tests.append(obj["file_relpath"])
        del kwargs["test_objects"]

    if not kwargs.get("binary") and conditions.is_firefox(command_context):
        try:
            kwargs["binary"] = command_context.get_binary_path("app")
        except BinaryNotFoundException as e:
            command_context.log(
                logging.ERROR, "awsy", {"error": str(e)}, "ERROR: {error}"
            )
            command_context.log(logging.INFO, "awsy", {"help": e.help()}, "{help}")
            return 1
    return run_awsy(command_context, tests, **kwargs)