summaryrefslogtreecommitdiffstats
path: root/mobile/android/tests/browser/chrome/test_awsy_lite.html
blob: 066aaf2ea236401f7f13525532fe056df3c5e4d7 (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
<!DOCTYPE HTML>
<html>
<!--
    This test reports Firefox memory use to Perfherder.

    Inspired by https://areweslimyet.com/mobile

    https://bugzilla.mozilla.org/show_bug.cgi?id=1233220
-->
<head>
  <meta charset="utf-8">
  <title>Test for Bug 1233220</title>
  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/MemoryStats.js"></script>
  <link rel="stylesheet" type="text/css" href="chrome://global/skin"/>
  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
  <script type="application/javascript;version=1.8" src="head.js"></script>
  <script type="application/javascript;version=1.8">

  "use strict";

  const { interfaces: Ci, utils: Cu, classes: Cc } = Components;

  var kUrls = [
    "http://mochi.test:8888/chrome/mobile/android/tests/browser/chrome/tp5/baidu.com/www.baidu.com/s@wd=mozilla.html",
    "http://mochi.test:8888/chrome/mobile/android/tests/browser/chrome/tp5/twitter.com/twitter.com/ICHCheezburger.html",
    "http://mochi.test:8888/chrome/mobile/android/tests/browser/chrome/tp5/msn.com/www.msn.com/index.html",
    "http://mochi.test:8888/chrome/mobile/android/tests/browser/chrome/tp5/163.com/www.163.com/index.html",
    "http://mochi.test:8888/chrome/mobile/android/tests/browser/chrome/tp5/bbc.co.uk/www.bbc.co.uk/news/index.html"
  ];

  var gTabsOpened = 0;
  var gWindow = null;
  var gLastTab = null;
  var gResults = [];

  Cu.import("resource://gre/modules/Services.jsm");

  var BrowserApp = Services.wm.getMostRecentWindow("navigator:browser").BrowserApp;
  SimpleTest.waitForExplicitFinish();
  SimpleTest.requestLongerTimeout(3);  // several long waits and GCs make for a long-running test
  SimpleTest.requestCompleteLog();  // so that "PERFHERDER_DATA" can be scraped from the log

  function checkpoint(aName) {
    var mrm = Cc["@mozilla.org/memory-reporter-manager;1"].getService(Ci.nsIMemoryReporterManager);
    gResults.push( { name: aName, resident: mrm.resident } );
    info(`${aName} | Resident Memory: ${mrm.resident}`);
  }

  var browserListener = {
    onOpenWindow: function(aWindow) {
        var win = aWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindow);
        win.addEventListener("UIReady", function listener(aEvent) {
            win.removeEventListener("UIReady", listener, false);
            attachTo(win);
        }, false);
    },

    onCloseWindow: function(aWindow) {
        detachFrom(aWindow);
    },

    onWindowTitleChange: function(aWindow, aTitle) {
    }
  };

  function doFullGc(aCallback, aIterations) {
    var threadMan = Cc["@mozilla.org/thread-manager;1"].getService(Ci.nsIThreadManager);
    var domWindowUtils = gWindow.QueryInterface(Components.interfaces.nsIInterfaceRequestor).getInterface(Components.interfaces.nsIDOMWindowUtils);

    function runSoon(f) {
        threadMan.mainThread.dispatch({ run: f }, Ci.nsIThread.DISPATCH_NORMAL);
    }

    function cc() {
        if (domWindowUtils.cycleCollect) {
            domWindowUtils.cycleCollect();
        }
        Services.obs.notifyObservers(null, "child-cc-request", null);
    }

    function minimizeInner() {
        // In order of preference: schedulePreciseShrinkingGC, schedulePreciseGC
        // garbageCollect
        if (++j <= aIterations) {
            var schedGC = Cu.schedulePreciseShrinkingGC;
            if (!schedGC) {
                schedGC = Cu.schedulePreciseGC;
            }

            Services.obs.notifyObservers(null, "child-gc-request", null);

            if (schedGC) {
                schedGC.call(Cu, { callback: function () {
                    runSoon(function () { cc(); runSoon(minimizeInner); });
                } });
            } else {
                if (domWindowUtils.garbageCollect) {
                    domWindowUtils.garbageCollect();
                }
                runSoon(function () { cc(); runSoon(minimizeInner); });
            }
        } else {
            runSoon(aCallback);
        }
    }

    var j = 0;
    minimizeInner();
  }

  function attachTo(aWindow) {
    if (gWindow != null) {
        info("attempting to attach to a second window [" + aWindow + "] while already attached to one window [" + gWindow + "]");
        return;
    }
    gWindow = aWindow;
    setTimeout(startTest, 0);
  }

  function detachFrom(aWindow) {
    if (gWindow == aWindow) {
      gWindow = null;
    }
  }

  function startup() {
    var enumerator = Services.wm.getEnumerator("navigator:browser");
    while (enumerator.hasMoreElements()) {
        // potential race condition here - the window may not be ready yet at
        // this point, so ideally we would test for that. but i can't find a
        // property that reflects whether or not UIReady has been fired, so
        // for now just assume the window is ready
        attachTo(enumerator.getNext().QueryInterface(Ci.nsIDOMWindow));
    }
    Services.wm.addListener(browserListener);
  }

  function startTest() {
    checkpoint("Fresh start");
    setTimeout(settle, 30000);
  }

  function settle() {
    checkpoint("Fresh start [+30s]");
    openTab();
  }

  function openTab() {
    var urlIndex = gTabsOpened++;
    if (urlIndex >= kUrls.length) {
        checkpoint("After tabs");
        setTimeout(postOpening, 30000);
        return;
    }

    info("opening tab with url [" + kUrls[urlIndex] + "]");
    gLastTab = BrowserApp.addTab(kUrls[urlIndex], { selected: true });
    setTimeout(waitForTab, 10000);
  }

  function waitForTab() {
    if (gLastTab.browser.contentDocument.readyState === "complete") {
        gLastTab = null;
        openTab();
    } else {
        setTimeout(waitForTab, 10000);
    }
  }

  function postOpening() {
    checkpoint("After tabs [+30s]");
    doFullGc(() => closeTabs());
  }

  function closeTabs() {
    checkpoint("After tabs [+30s, forced GC]");
    var tabCount = BrowserApp.tabs.length;
    for (var i = 1; i < tabCount; i++) {
        BrowserApp.closeTab(BrowserApp.tabs[i]);
    }

    var closeListener = {
        observe: function(aSubject, aTopic, aData) {
            tabCount--;
            dump("tab count dropped to [" + tabCount + "]");
            if (tabCount == 1) {
                Services.obs.removeObserver(this, "Tab:Closed", false);
                setTimeout(tabsClosed, 0);
            }
        }
    };
    Services.obs.addObserver(closeListener, "Tab:Closed", false);
  }

  function tabsClosed() {
    checkpoint("Tabs closed");
    setTimeout(postClosing, 30000);
  }

  function postClosing() {
    checkpoint("Tabs closed [+30s]");
    doFullGc(() => {
        checkpoint("Tabs closed [+30s, forced GC]");
        finalReport();
        ok(true, "memory logging complete -- view results in Perfherder");
        SimpleTest.finish();
    });
  }

  function geomean(aProperty) {
    // https://en.wikipedia.org/wiki/Geometric_mean#Relationship_with_arithmetic_mean_of_logarithms
    var logsum = 0;
    var i;
    for (i = 0; i < gResults.length; i++) {
        var result = gResults[i];
        logsum += Math.log(result[aProperty]);
    }
    return Math.round(Math.exp(logsum/gResults.length));
  }

  function finalReport() {
    var i;
    var perfherder = "PERFHERDER_DATA: ";
    perfherder += "{\"framework\": {\"name\": \"awsy\"}, ";
    perfherder += "\"suites\": [";
    perfherder += "{\"name\": \"Resident Memory\", ";
    perfherder += "\"subtests\": [";
    for (i = 0; i < gResults.length; i++) {
        var result = gResults[i];
        if (i > 0) {
            perfherder += ", ";
        }
        perfherder += `{\"name\": \"${result.name}\", \"value\": ${result.resident}}`;
    }
    perfherder += "], "; // end subtests
    perfherder += "\"value\": "+geomean("resident");
    perfherder += "}"; // end Resident Memory suite
    perfherder += "]"; // end suites
    perfherder += "}"; // end PERFHERDER_DATA
    info(perfherder);
  }

  startup();

  </script>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1233220">Mozilla Bug 1233220</a>
<br>
<p id="display"></p>
<div id="content" style="display: none">

</div>
<pre id="test">
</pre>
</body>
</html>