summaryrefslogtreecommitdiffstats
path: root/testing/raptor/browsertime/utils
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
commit26a029d407be480d791972afb5975cf62c9360a6 (patch)
treef435a8308119effd964b339f76abb83a57c29483 /testing/raptor/browsertime/utils
parentInitial commit. (diff)
downloadfirefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz
firefox-26a029d407be480d791972afb5975cf62c9360a6.zip
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'testing/raptor/browsertime/utils')
-rw-r--r--testing/raptor/browsertime/utils/NetworkThrottlingUtils.js85
1 files changed, 85 insertions, 0 deletions
diff --git a/testing/raptor/browsertime/utils/NetworkThrottlingUtils.js b/testing/raptor/browsertime/utils/NetworkThrottlingUtils.js
new file mode 100644
index 0000000000..8a0f78150e
--- /dev/null
+++ b/testing/raptor/browsertime/utils/NetworkThrottlingUtils.js
@@ -0,0 +1,85 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+const lazy = {};
+
+ChromeUtils.defineESModuleGetters(lazy, {
+ NetworkObserver:
+ "resource://devtools/shared/network-observer/NetworkObserver.sys.mjs",
+});
+
+/**
+ * The NetworkThrottler uses the dev tools NetworkObserver to provide api to throttle all network activity.
+ * This can be used to fix network conditions in browsertime pageload tests.
+ *
+ */
+
+// A minimal struct for onNetworkEvent handling
+class NetworkEventRecord {
+ addRequestPostData() {}
+ addResponseStart() {}
+ addSecurityInfo() {}
+ addEventTimings() {}
+ addResponseCache() {}
+ addResponseContent() {}
+ addServerTimings() {}
+ addServiceWorkerTimings() {}
+}
+
+class NetworkThrottler {
+ #devtoolsNetworkObserver;
+ #throttling;
+
+ constructor() {
+ this.#throttling = false;
+ }
+
+ destroy() {
+ this.stop();
+ }
+
+ start(throttleData) {
+ if (this.#throttling) {
+ console.error("NetworkThrottler already started");
+ return;
+ }
+
+ this.#devtoolsNetworkObserver = new lazy.NetworkObserver({
+ ignoreChannelFunction: this.#ignoreChannelFunction,
+ onNetworkEvent: this.#onNetworkEvent,
+ });
+
+ this.#devtoolsNetworkObserver.setThrottleData(throttleData);
+
+ this.#throttling = true;
+ }
+
+ stop() {
+ if (!this.#throttling) {
+ return;
+ }
+
+ this.#devtoolsNetworkObserver.destroy();
+ this.#devtoolsNetworkObserver = null;
+
+ this.#throttling = false;
+ }
+
+ #ignoreChannelFunction = channel => {
+ // Ignore chrome-privileged or DevTools-initiated requests
+ if (
+ channel.loadInfo?.loadingDocument === null &&
+ (channel.loadInfo.loadingPrincipal ===
+ Services.scriptSecurityManager.getSystemPrincipal() ||
+ channel.loadInfo.isInDevToolsContext)
+ ) {
+ return true;
+ }
+ return false;
+ };
+
+ #onNetworkEvent = (networkEvent, channel) => {
+ return new NetworkEventRecord(networkEvent, channel, this);
+ };
+}