summaryrefslogtreecommitdiffstats
path: root/devtools/server/tests/chrome/framerate-helpers.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
commit2aa4a82499d4becd2284cdb482213d541b8804dd (patch)
treeb80bf8bf13c3766139fbacc530efd0dd9d54394c /devtools/server/tests/chrome/framerate-helpers.js
parentInitial commit. (diff)
downloadfirefox-upstream.tar.xz
firefox-upstream.zip
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r--devtools/server/tests/chrome/framerate-helpers.js63
1 files changed, 63 insertions, 0 deletions
diff --git a/devtools/server/tests/chrome/framerate-helpers.js b/devtools/server/tests/chrome/framerate-helpers.js
new file mode 100644
index 0000000000..1539e8b517
--- /dev/null
+++ b/devtools/server/tests/chrome/framerate-helpers.js
@@ -0,0 +1,63 @@
+/* exported getTargetForSelectedTab, waitFor, plotFPS */
+"use strict";
+const { require } = ChromeUtils.import("resource://devtools/shared/Loader.jsm");
+const { TargetFactory } = require("devtools/client/framework/target");
+const Services = require("Services");
+
+// Always log packets when running tests.
+Services.prefs.setBoolPref("devtools.debugger.log", true);
+SimpleTest.registerCleanupFunction(function() {
+ Services.prefs.clearUserPref("devtools.debugger.log");
+});
+
+SimpleTest.waitForExplicitFinish();
+
+/**
+ * Add a new test tab in the browser and load the given url.
+ * @return Promise a promise that resolves to the new target representing
+ * the page currently opened.
+ */
+function getTargetForSelectedTab() {
+ // Get the target and get the necessary front
+ const { gBrowser } = Services.wm.getMostRecentWindow("navigator:browser");
+ return TargetFactory.forTab(gBrowser.selectedTab);
+}
+
+function waitFor(time) {
+ return new Promise(resolve => setTimeout(resolve, time));
+}
+
+function plotFPS(ticks, interval = 100, clamp = 60) {
+ const timeline = [];
+ const totalTicks = ticks.length;
+
+ // If the refresh driver didn't get a chance to tick before the
+ // recording was stopped, assume framerate was 0.
+ if (totalTicks == 0) {
+ timeline.push({ delta: 0, value: 0 });
+ timeline.push({ delta: interval, value: 0 });
+ return timeline;
+ }
+
+ let frameCount = 0;
+ let prevTime = ticks[0];
+
+ for (let i = 1; i < totalTicks; i++) {
+ const currTime = ticks[i];
+ frameCount++;
+
+ const elapsedTime = currTime - prevTime;
+ if (elapsedTime < interval) {
+ continue;
+ }
+
+ const framerate = Math.min(1000 / (elapsedTime / frameCount), clamp);
+ timeline.push({ delta: prevTime, value: framerate });
+ timeline.push({ delta: currTime, value: framerate });
+
+ frameCount = 0;
+ prevTime = currTime;
+ }
+
+ return timeline;
+}