PerfTestHelpers.sys.mjs (2330B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 const lazy = {}; 5 6 ChromeUtils.defineESModuleGetters(lazy, { 7 NetUtil: "resource://gre/modules/NetUtil.sys.mjs", 8 }); 9 10 export var PerfTestHelpers = { 11 /** 12 * Maps the entries in the given iterable to the given 13 * promise-returning task function, and waits for all returned 14 * promises to have resolved. At most `limit` promises may remain 15 * unresolved at a time. When the limit is reached, the function will 16 * wait for some to resolve before spawning more tasks. 17 */ 18 async throttledMapPromises(iterable, task, limit = 64) { 19 let pending = new Set(); 20 let promises = []; 21 for (let data of iterable) { 22 while (pending.size >= limit) { 23 await Promise.race(pending); 24 } 25 26 let promise = task(data); 27 promises.push(promise); 28 if (promise) { 29 promise.finally(() => pending.delete(promise)); 30 pending.add(promise); 31 } 32 } 33 34 return Promise.all(promises); 35 }, 36 37 /** 38 * Returns a promise which resolves to true if the resource at the 39 * given URI exists, false if it doesn't. This should only be used 40 * with local resources, such as from resource:/chrome:/jar:/file: 41 * URIs. 42 */ 43 checkURIExists(uri) { 44 return new Promise(resolve => { 45 try { 46 let channel = lazy.NetUtil.newChannel({ 47 uri, 48 loadUsingSystemPrincipal: true, 49 // Avoid crashing for non-existant files. If the file not existing 50 // is bad, we can deal with it in the test instead. 51 contentPolicyType: Ci.nsIContentPolicy.TYPE_FETCH, 52 }); 53 54 channel.asyncOpen({ 55 onStartRequest(request) { 56 resolve(Components.isSuccessCode(request.status)); 57 request.cancel(Cr.NS_BINDING_ABORTED); 58 }, 59 60 onStopRequest(request, status) { 61 // We should have already resolved from `onStartRequest`, but 62 // we resolve again here just as a failsafe. 63 resolve(Components.isSuccessCode(status)); 64 }, 65 }); 66 } catch (e) { 67 if ( 68 e.result != Cr.NS_ERROR_FILE_NOT_FOUND && 69 e.result != Cr.NS_ERROR_NOT_AVAILABLE 70 ) { 71 throw e; 72 } 73 resolve(false); 74 } 75 }); 76 }, 77 };