From 43a97878ce14b72f0981164f87f2e35e14151312 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 11:22:09 +0200 Subject: Adding upstream version 110.0.1. Signed-off-by: Daniel Baumann --- .../content/test/performance/PerfTestHelpers.jsm | 78 ++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 browser/base/content/test/performance/PerfTestHelpers.jsm (limited to 'browser/base/content/test/performance/PerfTestHelpers.jsm') diff --git a/browser/base/content/test/performance/PerfTestHelpers.jsm b/browser/base/content/test/performance/PerfTestHelpers.jsm new file mode 100644 index 0000000000..bf5a1543f7 --- /dev/null +++ b/browser/base/content/test/performance/PerfTestHelpers.jsm @@ -0,0 +1,78 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +var EXPORTED_SYMBOLS = ["PerfTestHelpers"]; + +const lazy = {}; + +ChromeUtils.defineModuleGetter( + lazy, + "NetUtil", + "resource://gre/modules/NetUtil.jsm" +); + +var PerfTestHelpers = { + /** + * Maps the entries in the given iterable to the given + * promise-returning task function, and waits for all returned + * promises to have resolved. At most `limit` promises may remain + * unresolved at a time. When the limit is reached, the function will + * wait for some to resolve before spawning more tasks. + */ + async throttledMapPromises(iterable, task, limit = 64) { + let pending = new Set(); + let promises = []; + for (let data of iterable) { + while (pending.size >= limit) { + await Promise.race(pending); + } + + let promise = task(data); + promises.push(promise); + if (promise) { + promise.finally(() => pending.delete(promise)); + pending.add(promise); + } + } + + return Promise.all(promises); + }, + + /** + * Returns a promise which resolves to true if the resource at the + * given URI exists, false if it doesn't. This should only be used + * with local resources, such as from resource:/chrome:/jar:/file: + * URIs. + */ + checkURIExists(uri) { + return new Promise(resolve => { + try { + let channel = lazy.NetUtil.newChannel({ + uri, + loadUsingSystemPrincipal: true, + }); + + channel.asyncOpen({ + onStartRequest(request) { + resolve(Components.isSuccessCode(request.status)); + request.cancel(Cr.NS_BINDING_ABORTED); + }, + + onStopRequest(request, status) { + // We should have already resolved from `onStartRequest`, but + // we resolve again here just as a failsafe. + resolve(Components.isSuccessCode(status)); + }, + }); + } catch (e) { + if ( + e.result != Cr.NS_ERROR_FILE_NOT_FOUND && + e.result != Cr.NS_ERROR_NOT_AVAILABLE + ) { + throw e; + } + resolve(false); + } + }); + }, +}; -- cgit v1.2.3