fune/docshell/test/browser/browser_timelineMarkers-frame-05.js
Logan Smyth 25d491b792 Bug 1601179 - Enable async stacks but limit captured async stacks to debuggees. r=jorendorff,smaug
The 'asyncStack' flag on JS execution contexts is used as a general switch
to enable async stack capture across all locations in SpiderMonkey, but
this causes problems because it can at times be too much of a performance
burden to general and track all of these stacks.

Since the introduction of this option, we have only enabled it on Nightly
and DevEdition for non-mobile builds, which has left a lot of users unable
to take advantage of this data while debugging.

This patch enables async stack traces across all of Firefox, but introduces
a new pref to toggle the scope of the actual expensive part of async stacks,
which is _capturing_ them and keeping them alive in memory. The new pref
limits the capturing of async stack traces to only debuggees, unless an
explicit pref is flipped to capture async traces for all cases.

This means that while async stacks are technically enabled, and code could
manually capture a stack and pass it back to SpiderMonkey and see that stack
reflected in later captured stacks, SpiderMonkey itself and related async
DOM APIs, among others, will not capture stacks or pass them to SpiderMonkey,
so there should be no general change in performance by enabling the broader
feature itself, unless the user is actively debugging the page.

One effect of this patch is that if you have the debugger open and then close
it, objects that have async stacks associated with them will retain those
stacks and they will continue to show up in stack traces, no _new_ stacks
will be captured. jorendorff and I have decided that this is okay because
the expectation that the debugger fully revert every possible effect that it
could have on a page is a nice goal but not a strict requirement.

Differential Revision: https://phabricator.services.mozilla.com/D68503
2020-06-14 02:41:45 +00:00

150 lines
3.8 KiB
JavaScript

/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
// This file expects frame-head.js to be loaded in the environment.
/* import-globals-from frame-head.js */
"use strict";
function forceSyncReflow(div) {
div.setAttribute("class", "resize-change-color");
// Force a reflow.
return div.offsetWidth;
}
function testSendingEvent() {
content.document.body.dispatchEvent(new content.Event("dog"));
}
function testConsoleTime() {
content.console.time("cats");
}
function testConsoleTimeEnd() {
content.console.timeEnd("cats");
}
function makePromise() {
let resolver;
new Promise(function(resolve, reject) {
testConsoleTime();
resolver = resolve;
}).then(function(val) {
testConsoleTimeEnd();
});
return resolver;
}
function resolvePromise(resolver) {
resolver(23);
}
var TESTS = [
{
desc: "Stack trace on sync reflow",
searchFor: "Reflow",
setup(docShell) {
let div = content.document.querySelector("div");
forceSyncReflow(div);
},
check(markers) {
markers = markers.filter(m => m.name == "Reflow");
ok(markers.length > 0, "Reflow marker includes stack");
ok(markers[0].stack.functionDisplayName == "forceSyncReflow");
},
},
{
desc: "Stack trace on DOM event",
searchFor: "DOMEvent",
setup(docShell) {
content.document.body.addEventListener(
"dog",
function(e) {
console.log("hi");
},
true
);
testSendingEvent();
},
check(markers) {
markers = markers.filter(m => m.name == "DOMEvent");
ok(markers.length > 0, "DOMEvent marker includes stack");
ok(
markers[0].stack.functionDisplayName == "testSendingEvent",
"testSendingEvent is on the stack"
);
},
},
{
desc: "Stack trace on console event",
searchFor: "ConsoleTime",
setup(docShell) {
testConsoleTime();
testConsoleTimeEnd();
},
check(markers) {
markers = markers.filter(m => m.name == "ConsoleTime");
ok(markers.length > 0, "ConsoleTime marker includes stack");
ok(
markers[0].stack.functionDisplayName == "testConsoleTime",
"testConsoleTime is on the stack"
);
ok(
markers[0].endStack.functionDisplayName == "testConsoleTimeEnd",
"testConsoleTimeEnd is on the stack"
);
},
},
];
if (
!Services.prefs.getBoolPref(
"javascript.options.asyncstack_capture_debuggee_only"
)
) {
TESTS.push({
desc: "Async stack trace on Promise",
searchFor: "ConsoleTime",
setup(docShell) {
let resolver = makePromise();
resolvePromise(resolver);
},
check(markers) {
markers = markers.filter(m => m.name == "ConsoleTime");
ok(markers.length > 0, "Promise marker includes stack");
ok(
markers[0].stack.functionDisplayName == "testConsoleTime",
"testConsoleTime is on the stack"
);
let frame = markers[0].endStack;
ok(
frame.functionDisplayName == "testConsoleTimeEnd",
"testConsoleTimeEnd is on the stack"
);
frame = frame.parent;
ok(
frame.functionDisplayName == "makePromise/<",
"makePromise/< is on the stack"
);
let asyncFrame = frame.asyncParent;
ok(asyncFrame !== null, "Frame has async parent");
is(
asyncFrame.asyncCause,
"promise callback",
"Async parent has correct cause"
);
// Skip over self-hosted parts of our Promise implementation.
while (asyncFrame.source === "self-hosted") {
asyncFrame = asyncFrame.parent;
}
is(
asyncFrame.functionDisplayName,
"makePromise",
"Async parent has correct function name"
);
},
});
}
timelineContentTest(TESTS);