Skip to content

Instantly share code, notes, and snippets.

@fitzgen
Forked from robcee/gist:9061250
Last active April 26, 2024 20:04
Show Gist options
  • Save fitzgen/9076806 to your computer and use it in GitHub Desktop.
Save fitzgen/9076806 to your computer and use it in GitHub Desktop.
let { devtools } = Cu.import("resource://gre/modules/devtools/Loader.jsm", {});
function openToolbox() {
let target = devtools.TargetFactory.forTab(gBrowser.selectedTab);
return gDevTools.showToolbox(target, "jsdebugger");
}
function closeToolbox() {
let target = devtools.TargetFactory.forTab(gBrowser.selectedTab);
return gDevTools.closeToolbox(target);
}
function ok(thing, msg) {
console.log(thing, msg);
}
///////////////// Stolen & modified from debugger tests' head.js ///////////////////////////
function waitForDebuggerEvents(aWindow, aEventName, aEventRepeat = 1) {
let deferred = Promise.defer();
let count = 0;
aWindow.on(aEventName, function onEvent(aEventName, ...aArgs) {
if (++count == aEventRepeat) {
ok(true, "Enough '" + aEventName + "' panel events have been fired.");
aWindow.off(aEventName, onEvent);
deferred.resolve.apply(deferred, aArgs);
}
});
return deferred.promise;
}
function waitForSourceShown(aWindow, aUrl) {
return waitForDebuggerEvents(aWindow, aWindow.EVENTS.SOURCE_SHOWN).then(aSource => {
let sourceUrl = aSource.url;
if (!sourceUrl.contains(aUrl)) {
return waitForSourceShown(aWindow, aUrl);
} else {
ok(true, "The correct source has been shown.");
}
});
}
function ensureSourceIs(aWindow, aUrl, aWaitFlag = false) {
if (aWindow.DebuggerView.Sources.selectedValue.contains(aUrl)) {
return Promise.resolve(null);
}
if (aWaitFlag) {
return waitForSourceShown(aWindow, aUrl);
}
return Promise.reject(null);
}
////////////////////////////////////////////////////////////////////////////////////////////
let results = [];
let ITERATIONS = 100;
(function loop() {
if (results.length == ITERATIONS) {
finish();
return;
}
const startTime = Date.now();
openToolbox()
.then(toolbox => {
let panel = toolbox.getCurrentPanel();
return ensureSourceIs(panel.panelWin, ".js", true);
})
.then(() => {
results.push(Date.now() - startTime);
})
.then(closeToolbox)
.then(loop)
.then(null, console.error);
}());
function finish () {
results.sort((a, b) => a - b);
const sum = results.reduce((a, b) => a + b, 0);
const mean = sum / results.length;
const median = results[Math.floor(ITERATIONS / 2)];
console.log("==================================================================");
console.log("mean = %d", mean);
console.log("median = %d", median);
console.log("==================================================================");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment