DEV: Print usedJSHeapSize to the console after QUnit run (#14462)

DEV: Print usedJSHeapSize to the console after QUnit run (#14462)

This is console.log'd to the browser console. run-qunit will print this to stdout. testem will not, so a custom reporter is implemented to print this message.

The --enable-precise-memory-info is added so that chrome provides high-resolution memory information. This API is not supported by firefox. The logic will degrade gracefully.

diff --git a/app/assets/javascripts/discourse/testem.js b/app/assets/javascripts/discourse/testem.js
index 9a69697..e1ef38c 100644
--- a/app/assets/javascripts/discourse/testem.js
+++ b/app/assets/javascripts/discourse/testem.js
@@ -1,3 +1,27 @@
+const TapReporter = require("testem/lib/reporters/tap_reporter");
+
+class Reporter {
+  constructor() {
+    this._tapReporter = new TapReporter(...arguments);
+  }
+
+  reportMetadata(tag, metadata) {
+    if (tag === "summary-line") {
+      process.stdout.write(`\n${metadata.message}\n`);
+    } else {
+      this._tapReporter.reportMetadata(...arguments);
+    }
+  }
+
+  report(prefix, data) {
+    this._tapReporter.report(prefix, data);
+  }
+
+  finish() {
+    this._tapReporter.finish();
+  }
+}
+
 module.exports = {
   test_page: "tests/index.html?hidepassed",
   disable_watching: true,
@@ -15,6 +39,7 @@ module.exports = {
       "--mute-audio",
       "--remote-debugging-port=4201",
       "--window-size=1440,900",
+      "--enable-precise-memory-info",
     ].filter(Boolean),
     Firefox: ["-headless", "--width=1440", "--height=900"],
     "Headless Firefox": ["--width=1440", "--height=900"],
@@ -22,4 +47,5 @@ module.exports = {
   browser_paths: {
     "Headless Firefox": "/opt/firefox-evergreen/firefox",
   },
+  reporter: Reporter,
 };
diff --git a/app/assets/javascripts/discourse/tests/setup-tests.js b/app/assets/javascripts/discourse/tests/setup-tests.js
index beaa09c..a929fdb 100644
--- a/app/assets/javascripts/discourse/tests/setup-tests.js
+++ b/app/assets/javascripts/discourse/tests/setup-tests.js
@@ -127,6 +127,32 @@ function setupToolbar() {
   });
 }
 
+function reportMemoryUsageAfterTests() {
+  QUnit.done(() => {
+    const usageBytes = performance.memory?.usedJSHeapSize;
+    let result;
+    if (usageBytes) {
+      result = `${(usageBytes / Math.pow(2, 30)).toFixed(3)}GB`;
+    } else {
+      result = "(performance.memory api unavailable)";
+    }
+
+    writeSummaryLine(`Used JS Heap Size: ${result}`);
+  });
+}
+
+function writeSummaryLine(message) {
+  // eslint-disable-next-line no-console
+  console.log(`\n${message}\n`);
+  if (window.Testem) {
+    window.Testem.useCustomAdapter(function (socket) {
+      socket.emit("test-metadata", "summary-line", {
+        message: message,
+      });
+    });
+  }
+}
+
 function setupTestsCommon(application, container, config) {
   QUnit.config.hidepassed = true;
 
@@ -353,6 +379,7 @@ function setupTestsCommon(application, container, config) {
   jQuery.fx.off = true;
 
   setupToolbar();
+  reportMemoryUsageAfterTests();
   setApplication(application);
   setDefaultOwner(application.__container__);
   resetSite();
diff --git a/test/run-qunit.js b/test/run-qunit.js
index 584e3f6..5c22125 100644
--- a/test/run-qunit.js
+++ b/test/run-qunit.js
@@ -42,6 +42,7 @@ async function runAllTests() {
         "--disable-dev-shm-usage",
         "--mute-audio",
         "--window-size=1440,900",
+        "--enable-precise-memory-info",
       ],
     };
 

GitHub sha: 4fa296c3ec69d7d73eef5badd8384ae556f2a1c9

This commit appears in #14462 which was approved by CvX. It was merged by davidtaylorhq.