Bug 1496649 Use modern MozAfterPaint goodness in tresize r=mconley
authorAndrew Swan <aswan@mozilla.com>
Fri, 05 Oct 2018 20:32:36 -0700
changeset 488779 8d9338c622944658d6737ff7831d1c14aa789a43
parent 488778 3c7e04f374581d49a8c710b03fbfb8fa0909051a
child 488780 8d392a740646e3e7e2e173c97b70fc19ba121e21
push id246
push userfmarier@mozilla.com
push dateSat, 13 Oct 2018 00:15:40 +0000
reviewersmconley
bugs1496649, 1264409, 1264798
milestone64.0a1
Bug 1496649 Use modern MozAfterPaint goodness in tresize r=mconley Some new features were added to MozAfterPaint events in bugs 1264409 and 1264798 to allow tests to make more accurate measurements. Updating tresize to use these features is not directly related to the main goal of this bug, but lets do it anyway while we're touching tresize...
testing/talos/talos/tests/tresize/addon/content/tresize.js
--- a/testing/talos/talos/tests/tresize/addon/content/tresize.js
+++ b/testing/talos/talos/tests/tresize/addon/content/tresize.js
@@ -1,74 +1,67 @@
 /*
  * tresize-test - A purer form of paint measurement than tpaint. This
  * test opens a single window positioned at 10,10 and sized to 425,425,
  * then resizes the window outward |max| times measuring the amount of
- * time it takes to repaint each resize. Dumps the resulting dataset
- * and average to stdout or logfile.
+ * time it takes to repaint each resize.  Calls the provided callback
+ * with an array of resize times in milliseconds.
  */
 
-var dataSet = [];
-var windowSize = 425;
-var resizeIncrement = 2;
-var count = 0;
-var max = 300;
-
-var dumpDataSet = false;
-var doneCallback = null;
+async function runTest(callback, locationSearch) {
+  const INCREMENT = 2;
+  const MAX = 300;
 
-function painted() {
-  window.removeEventListener("MozAfterPaint", painted, true);
-  var paintTime = window.performance.now();
-  Profiler.pause("resize " + count);
-  dataSet[count].end = paintTime;
-  setTimeout(resizeCompleted, 20);
-}
-
-function resizeTest() {
-  try {
-    windowSize += resizeIncrement;
-    window.addEventListener("MozAfterPaint", painted, true);
-    Profiler.resume("resize " + count);
-    dataSet[count] = {"start": window.performance.now()};
-    window.resizeTo(windowSize, windowSize);
-  } catch (ex) { finish({error: ex.message}); }
-}
+  // Measure the time it take for the provided action to trigger a
+  // MozAfterPaint event.  This function ensures that the event being
+  // measured is really a result of the given action.
+  function measurePaintTime(action, marker) {
+    return new Promise(resolve => {
+      let startTime;
+      let lastTransaction = window.windowUtils.lastTransactionId;
+      function painted(event) {
+        if (event.transactionId <= lastTransaction) {
+          return;
+        }
 
-function testCompleted() {
-  try {
-    Profiler.finishTest();
-    var total = 0;
-    var diffs = [];
-    for (var idx = 0; idx < count; idx++) {
-      var diff = dataSet[idx].end - dataSet[idx].start;
-      total += diff;
-      diffs.push(diff);
-    }
-    var average = (total / count);
-    finish({diffs, average});
-  } catch (ex) { finish({error: ex.message}); }
-}
+        if (marker) {
+          Profiler.pause(marker);
+        }
+        window.removeEventListener("MozAfterPaint", painted, true);
+        let time = event.paintTimeStamp - startTime;
+        resolve(time);
+      }
+      window.addEventListener("MozAfterPaint", painted, true);
+      if (marker) {
+        Profiler.resume(marker);
+      }
+      startTime = window.performance.now();
+      action();
+    });
+  }
 
-function resizeCompleted() {
-  count++;
-  if (count >= max) {
-    testCompleted();
-  } else {
-    resizeTest();
-  }
-}
+  // Position the intial window and set up profiling...
+  let windowSize = 425;
+  await measurePaintTime(() => {
+    window.moveTo(10, 10);
+    window.resizeTo(windowSize, windowSize);
+  });
 
-function runTest(callback, locationSearch) {
-  doneCallback = callback;
-  window.moveTo(10, 10);
-  window.resizeTo(windowSize, windowSize);
   Profiler.initFromURLQueryParams(locationSearch);
   Profiler.beginTest("tresize");
-  resizeTest();
-}
+
+  let times = [];
+  for (let i = 0; i < MAX; i++) {
+    const marker = `resize ${i}`;
+    windowSize += INCREMENT;
 
-function finish(msgs) {
-  if (doneCallback) {
-    doneCallback(msgs);
+    let time = await measurePaintTime(() => {
+      window.resizeTo(windowSize, windowSize);
+    }, marker);
+    times.push(time);
+
+    await new Promise(resolve => setTimeout(resolve, 20));
   }
+
+  let total = times.reduce((a, b) => a + b);
+  let average = total / times.length;
+  callback({average});
 }
-