Bug 1407826 - minimizeMemoryUsage and flush the event loop after reload step and teardown. r=bgrins,jmaher draft
authorAlexandre Poirot <poirot.alex@gmail.com>
Mon, 16 Oct 2017 03:09:25 -0700
changeset 693824 e50ecdacda26048d61ebe819e7f6a2edf29944d5
parent 693347 e64846245b002c204144ac412fbd874e32e47edf
child 739149 7c82c3d436fdf14ea3e87fd49dbef8d0406b3671
push id87929
push userbmo:poirot.alex@gmail.com
push dateMon, 06 Nov 2017 23:00:13 +0000
reviewersbgrins, jmaher
bugs1407826
milestone58.0a1
Bug 1407826 - minimizeMemoryUsage and flush the event loop after reload step and teardown. r=bgrins,jmaher MozReview-Commit-ID: E9weZ4Yycz0
testing/talos/talos/tests/devtools/addon/content/damp.js
--- a/testing/talos/talos/tests/devtools/addon/content/damp.js
+++ b/testing/talos/talos/tests/devtools/addon/content/damp.js
@@ -1,11 +1,51 @@
 const { Services } = Components.utils.import("resource://gre/modules/Services.jsm", {});
 const { Task } = Cu.import("resource://gre/modules/Task.jsm", {});
 const { XPCOMUtils } = Cu.import("resource://gre/modules/XPCOMUtils.jsm", {});
+const gMgr = Cc['@mozilla.org/memory-reporter-manager;1'].getService(Ci.nsIMemoryReporterManager);
+
+// Flush any action pending on the event loop
+// Return the number of processed events, if any.
+function processPendingEvents() {
+  let loops = 0;
+  while (Services.tm.currentThread.hasPendingEvents()) {
+    Services.tm.currentThread.processNextEvent(true);
+    loops++;
+  }
+  return loops;
+}
+// Garbage collect everything.
+function garbageCollect() {
+  dump("Garbage collect\n");
+
+  // Minimize memory usage
+  Services.obs.notifyObservers(null, "child-mmu-request");
+  return new Promise(done => {
+    gMgr.minimizeMemoryUsage(done);
+  });
+}
+// Function to help waiting for all previous asynchronous action to be finished,
+// and also garbage collect things to avoid having a GC cause by previous test.
+async function settle() {
+  let start = performance.now();
+  await garbageCollect();
+
+  dump("Flush event loop\n");
+  // Wait for the event loop to be empty 10 times in a row for 500ms
+  let wait = 10;
+  while(wait > 0) {
+    while(processPendingEvents() > 0) {
+      wait = 10;
+    }
+    await new Promise(done => setTimeout(done, 50));
+    wait--;
+  }
+  dump("Settled in " + Math.round(performance.now() - start) + "ms\n");
+}
 
 XPCOMUtils.defineLazyGetter(this, "require", function() {
   let { require } =
     Components.utils.import("resource://devtools/shared/Loader.jsm", {});
   return require;
 });
 XPCOMUtils.defineLazyGetter(this, "gDevTools", function() {
   let { gDevTools } = require("devtools/client/framework/devtools");
@@ -442,16 +482,17 @@ async _consoleOpenWithCachedMessagesTest
     let {time} = await this.closeToolbox();
     this._results.push({name: name + ".close.DAMP", value: time });
   },
 
   async reloadPageAndLog(name, onReload) {
     dump("Reload page on '" + name + "'\n");
     let {time} = await this.reloadPage(onReload);
     this._results.push({name: name + ".reload.DAMP", value: time });
+    await settle();
   },
 
   async _coldInspectorOpen() {
     await this.testSetup(SIMPLE_URL);
     await this.openToolboxAndLog("cold.inspector", "inspector");
     await this.closeToolbox();
     await this.testTeardown();
   },
@@ -578,20 +619,21 @@ async _consoleOpenWithCachedMessagesTest
   testSetup: Task.async(function* (url) {
     let tab = yield this.addTab(url);
     yield new Promise(resolve => {
       setTimeout(resolve, this._config.rest);
     });
     return tab;
   }),
 
-  testTeardown: Task.async(function* (url) {
+  async testTeardown(url) {
     this.closeCurrentTab();
+    await settle();
     this._nextCommand();
-  }),
+  },
 
   // Everything below here are common pieces needed for the test runner to function,
   // just copy and pasted from Tart with /s/TART/DAMP
 
   _win: undefined,
   _dampTab: undefined,
   _results: [],
   _config: {subtests: [], repeat: 1, rest: 100},