summaryrefslogtreecommitdiffstats
path: root/testing/awsy/awsy/awsy_test_case.py
blob: 4a2c2361bdbb7d6a1af503342e2cf2185f47065f (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
# -*- Mode: python; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40 -*-
# vim: set filetype=python:
# 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 fnmatch
import glob
import gzip
import json
import os
import shutil
import sys
import tempfile
import time

import mozlog.structured
from marionette_driver import Wait
from marionette_driver.errors import JavascriptException, ScriptTimeoutException
from marionette_driver.keys import Keys
from marionette_harness import MarionetteTestCase

AWSY_PATH = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
if AWSY_PATH not in sys.path:
    sys.path.append(AWSY_PATH)

from awsy import (
    ITERATIONS,
    MAX_TABS,
    PER_TAB_PAUSE,
    SETTLE_WAIT_TIME,
    process_perf_data,
)


class AwsyTestCase(MarionetteTestCase):
    """
    Base test case for AWSY tests.
    """

    def urls(self):
        raise NotImplementedError()

    def perf_suites(self):
        raise NotImplementedError()

    def perf_checkpoints(self):
        raise NotImplementedError()

    def perf_extra_opts(self):
        return None

    def iterations(self):
        return self._iterations

    def pages_to_load(self):
        return self._pages_to_load if self._pages_to_load else len(self.urls())

    def settle(self):
        """
        Pauses for the settle time.
        """
        time.sleep(self._settleWaitTime)

    def setUp(self):
        MarionetteTestCase.setUp(self)

        self.logger = mozlog.structured.structuredlog.get_default_logger()
        self.marionette.set_context("chrome")
        self._resultsDir = self.testvars["resultsDir"]

        self._binary = self.testvars["bin"]
        self._run_local = self.testvars.get("run_local", False)

        # Cleanup our files from previous runs.
        for patt in (
            "memory-report-*.json.gz",
            "perfherder-data.json",
            "dmd-*.json.gz",
        ):
            for f in glob.glob(os.path.join(self._resultsDir, patt)):
                os.unlink(f)

        # Optional testvars.
        self._pages_to_load = self.testvars.get("entities", 0)
        self._iterations = self.testvars.get("iterations", ITERATIONS)
        self._perTabPause = self.testvars.get("perTabPause", PER_TAB_PAUSE)
        self._settleWaitTime = self.testvars.get("settleWaitTime", SETTLE_WAIT_TIME)
        self._maxTabs = self.testvars.get("maxTabs", MAX_TABS)
        self._dmd = self.testvars.get("dmd", False)

        self.logger.info(
            "areweslimyet run by %d pages, %d iterations,"
            " %d perTabPause, %d settleWaitTime"
            % (
                self._pages_to_load,
                self._iterations,
                self._perTabPause,
                self._settleWaitTime,
            )
        )
        self.reset_state()

    def tearDown(self):
        MarionetteTestCase.tearDown(self)

        try:
            self.logger.info("processing data in %s!" % self._resultsDir)
            perf_blob = process_perf_data.create_perf_data(
                self._resultsDir,
                self.perf_suites(),
                self.perf_checkpoints(),
                self.perf_extra_opts(),
            )
            self.logger.info("PERFHERDER_DATA: %s" % json.dumps(perf_blob))

            perf_file = os.path.join(self._resultsDir, "perfherder-data.json")
            with open(perf_file, "w") as fp:
                json.dump(perf_blob, fp, indent=2)
            self.logger.info("Perfherder data written to %s" % perf_file)
        except Exception:
            raise
        finally:
            # Make sure we cleanup and upload any existing files even if there
            # were errors processing the perf data.
            if self._dmd:
                self.cleanup_dmd()

            # copy it to moz upload dir if set
            if "MOZ_UPLOAD_DIR" in os.environ:
                for file in os.listdir(self._resultsDir):
                    file = os.path.join(self._resultsDir, file)
                    if os.path.isfile(file):
                        shutil.copy2(file, os.environ["MOZ_UPLOAD_DIR"])

    def cleanup_dmd(self):
        """
        Handles moving DMD reports from the temp dir to our resultsDir.
        """
        from dmd import fixStackTraces

        # Move DMD files from temp dir to resultsDir.
        tmpdir = tempfile.gettempdir()
        tmp_files = os.listdir(tmpdir)
        for f in fnmatch.filter(tmp_files, "dmd-*.json.gz"):
            f = os.path.join(tmpdir, f)
            # We don't fix stacks on Windows, even though we could, due to the
            # tale of woe in bug 1626272.
            if not sys.platform.startswith("win"):
                self.logger.info("Fixing stacks for %s, this may take a while" % f)
                isZipped = True
                fixStackTraces(f, isZipped, gzip.open)
            shutil.move(f, self._resultsDir)

        # Also attempt to cleanup the unified memory reports.
        for f in fnmatch.filter(tmp_files, "unified-memory-report-*.json.gz"):
            try:
                os.remove(f)
            except OSError:
                self.logger.info("Unable to remove %s" % f)

    def reset_state(self):
        self._pages_loaded = 0

        # Close all tabs except one
        for x in self.marionette.window_handles[1:]:
            self.logger.info("closing window: %s" % x)
            self.marionette.switch_to_window(x)
            self.marionette.close()

        self._tabs = self.marionette.window_handles
        self.marionette.switch_to_window(self._tabs[0])

    def do_memory_report(self, checkpointName, iteration, minimize=False):
        """Creates a memory report for all processes and and returns the
        checkpoint.

        This will block until all reports are retrieved or a timeout occurs.
        Returns the checkpoint or None on error.

        :param checkpointName: The name of the checkpoint.

        :param minimize: If true, minimize memory before getting the report.
        """
        self.logger.info("starting checkpoint %s..." % checkpointName)

        checkpoint_file = "memory-report-%s-%d.json.gz" % (checkpointName, iteration)
        checkpoint_path = os.path.join(self._resultsDir, checkpoint_file)
        # On Windows, replace / with the Windows directory
        # separator \ and escape it to prevent it from being
        # interpreted as an escape character.
        if sys.platform.startswith("win"):
            checkpoint_path = checkpoint_path.replace("\\", "\\\\").replace("/", "\\\\")

        checkpoint_script = r"""
            let [resolve] = arguments;
            let dumper =
            Cc["@mozilla.org/memory-info-dumper;1"].getService(
            Ci.nsIMemoryInfoDumper);
            dumper.dumpMemoryReportsToNamedFile(
                "%s",
                () => resolve("memory report done!"),
                null,
                /* anonymize */ false,
                /* minimize memory usage */ %s);
            """ % (
            checkpoint_path,
            "true" if minimize else "false",
        )

        checkpoint = None
        try:
            finished = self.marionette.execute_async_script(
                checkpoint_script, script_timeout=60000
            )
            if finished:
                checkpoint = checkpoint_path
        except JavascriptException as e:
            self.logger.error("Checkpoint JavaScript error: %s" % e)
        except ScriptTimeoutException:
            self.logger.error("Memory report timed out")
        except Exception:
            self.logger.error("Unexpected error: %s" % sys.exc_info()[0])
        else:
            self.logger.info("checkpoint created, stored in %s" % checkpoint_path)

        # Now trigger a DMD report if requested.
        if self._dmd:
            self.do_dmd(checkpointName, iteration)

        return checkpoint

    def do_dmd(self, checkpointName, iteration):
        """
        Triggers DMD reports that are used to help identify sources of
        'heap-unclassified'.

        NB: This will dump DMD reports to the temp dir. Unfortunately it also
        dumps memory reports, but that's all we have to work with right now.
        """
        self.logger.info("Starting %s DMD reports..." % checkpointName)

        ident = "%s-%d" % (checkpointName, iteration)

        # TODO(ER): This actually takes a minimize argument. We could use that
        # rather than have a separate `do_gc` function. Also it generates a
        # memory report so we could combine this with `do_checkpoint`. The main
        # issue would be moving everything out of the temp dir.
        #
        # Generated files have the form:
        #   dmd-<checkpoint>-<iteration>-pid.json.gz, ie:
        #   dmd-TabsOpenForceGC-0-10885.json.gz
        #
        # and for the memory report:
        #   unified-memory-report-<checkpoint>-<iteration>.json.gz
        dmd_script = (
            r"""
            let dumper =
            Cc["@mozilla.org/memory-info-dumper;1"].getService(
            Ci.nsIMemoryInfoDumper);
            dumper.dumpMemoryInfoToTempDir(
                "%s",
                /* anonymize = */ false,
                /* minimize = */ false);
            """
            % ident
        )

        try:
            # This is async and there's no callback so we use the existence
            # of an incomplete memory report to check if it hasn't finished yet.
            self.marionette.execute_script(dmd_script, script_timeout=60000)
            tmpdir = tempfile.gettempdir()
            prefix = "incomplete-unified-memory-report-%s-%d-*" % (
                checkpointName,
                iteration,
            )
            max_wait = 240
            elapsed = 0
            while fnmatch.filter(os.listdir(tmpdir), prefix) and elapsed < max_wait:
                self.logger.info("Waiting for memory report to finish")
                time.sleep(1)
                elapsed += 1

            incomplete = fnmatch.filter(os.listdir(tmpdir), prefix)
            if incomplete:
                # The memory reports never finished.
                self.logger.error("Incomplete memory reports leftover.")
                for f in incomplete:
                    os.remove(os.path.join(tmpdir, f))

        except JavascriptException as e:
            self.logger.error("DMD JavaScript error: %s" % e)
        except ScriptTimeoutException:
            self.logger.error("DMD timed out")
        except Exception:
            self.logger.error("Unexpected error: %s" % sys.exc_info()[0])
        else:
            self.logger.info("DMD started, prefixed with %s" % ident)

    def open_and_focus(self):
        """Opens the next URL in the list and focuses on the tab it is opened in.

        A new tab will be opened if |_maxTabs| has not been exceeded, otherwise
        the URL will be loaded in the next tab.
        """
        page_to_load = self.urls()[self._pages_loaded % len(self.urls())]
        tabs_loaded = len(self._tabs)
        open_tab_script = r"""
            gBrowser.addTab("about:blank", {
                inBackground: false,
                triggeringPrincipal: Services.scriptSecurityManager.getSystemPrincipal(),
            });
        """

        if tabs_loaded < self._maxTabs and tabs_loaded <= self._pages_loaded:
            full_tab_list = self.marionette.window_handles

            self.marionette.execute_script(open_tab_script, script_timeout=60000)

            Wait(self.marionette).until(
                lambda mn: len(mn.window_handles) == tabs_loaded + 1,
                message="No new tab has been opened",
            )

            # NB: The tab list isn't sorted, so we do a set diff to determine
            #     which is the new tab
            new_tab_list = self.marionette.window_handles
            new_tabs = list(set(new_tab_list) - set(full_tab_list))

            self._tabs.append(new_tabs[0])
            tabs_loaded += 1

        tab_idx = self._pages_loaded % self._maxTabs

        tab = self._tabs[tab_idx]

        # Tell marionette which tab we're on
        # NB: As a work-around for an e10s marionette bug, only select the tab
        #     if we're really switching tabs.
        if tabs_loaded > 1:
            self.logger.info("switching to tab")
            self.marionette.switch_to_window(tab)
            self.logger.info("switched to tab")

        with self.marionette.using_context("content"):
            self.logger.info("loading %s" % page_to_load)
            self.marionette.navigate(page_to_load)
            self.logger.info("loaded!")

        # The tab handle can change after actually loading content
        # First build a set up w/o the current tab
        old_tabs = set(self._tabs)
        old_tabs.remove(tab)
        # Perform a set diff to get the (possibly) new handle
        new_tabs = set(self.marionette.window_handles) - old_tabs
        # Update the tab list at the current index to preserve the tab
        # ordering
        if new_tabs:
            self._tabs[tab_idx] = list(new_tabs)[0]

        # give the page time to settle
        time.sleep(self._perTabPause)

        self._pages_loaded += 1

    def signal_user_active(self):
        """Signal to the browser that the user is active.

        Normally when being driven by marionette the browser thinks the
        user is inactive the whole time because user activity is
        detected by looking at key and mouse events.

        This would be a problem for this test because user inactivity is
        used to schedule some GCs (in particular shrinking GCs), so it
        would make this unrepresentative of real use.

        Instead we manually cause some inconsequential activity (a press
        and release of the shift key) to make the browser think the user
        is active.  Then when we sleep to allow things to settle the
        browser will see the user as becoming inactive and trigger
        appropriate GCs, as would have happened in real use.
        """
        try:
            action = self.marionette.actions.sequence("key", "keyboard_id")
            action.key_down(Keys.SHIFT)
            action.key_up(Keys.SHIFT)
            action.perform()
        finally:
            self.marionette.actions.release()

    def open_pages(self):
        """
        Opens all pages with our given configuration.
        """
        for _ in range(self.pages_to_load()):
            self.open_and_focus()
            self.signal_user_active()