gecko/toolkit/components/aboutmemory/tests/test_memoryReporters.xul
2013-04-11 20:52:32 -07:00

156 lines
6.0 KiB
XML

<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="chrome://global/skin"?>
<?xml-stylesheet type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>
<window title="Memory reporter"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
<!-- This file tests (in a rough fashion) whether the memory reporters are
producing sensible results. test_aboutmemory.xul tests the
presentation of memory reports in about:memory. -->
<!-- test results are displayed in the html:body -->
<body xmlns="http://www.w3.org/1999/xhtml">
<!-- In bug 773533, <marquee> elements crashed the JS memory reporter -->
<marquee>Marquee</marquee>
</body>
<!-- test code goes here -->
<script type="application/javascript">
<![CDATA[
// Nb: this test is all JS and so should be done with an xpcshell test,
// but bug 671753 is preventing the memory-reporter-manager from being
// accessed from xpcshell.
"use strict";
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cr = Components.results;
const kUnknown = -1;
const NONHEAP = Ci.nsIMemoryReporter.KIND_NONHEAP;
const HEAP = Ci.nsIMemoryReporter.KIND_HEAP;
const OTHER = Ci.nsIMemoryReporter.KIND_OTHER;
const BYTES = Ci.nsIMemoryReporter.UNITS_BYTES;
const COUNT = Ci.nsIMemoryReporter.UNITS_COUNT;
const COUNT_CUMULATIVE = Ci.nsIMemoryReporter.UNITS_COUNT_CUMULATIVE;
const PERCENTAGE = Ci.nsIMemoryReporter.UNITS_PERCENTAGE;
let vsizeAmounts = [];
let residentAmounts = [];
let jsGcHeapAmounts = [];
let heapAllocatedAmounts = [];
let storageSqliteAmounts = [];
let areJsNonWindowCompartmentsPresent = false;
let areWindowObjectsJsCompartmentsPresent = false;
let isSandboxLocationShown = false;
let isPlacesPresent = false;
let isImagesPresent = false;
let isXptiWorkingSetPresent = false;
let isAtomTablePresent = false;
let mySandbox = Components.utils.Sandbox(document.nodePrincipal,
{ sandboxName: "this-is-a-sandbox-name" });
function handleReport(aProcess, aPath, aKind, aUnits, aAmount, aDescription)
{
// Record the values of some notable reporters.
if (aPath === "vsize") {
vsizeAmounts.push(aAmount);
} else if (aPath === "resident") {
residentAmounts.push(aAmount);
} else if (aPath === "js-main-runtime-gc-heap-committed/used/gc-things") {
jsGcHeapAmounts.push(aAmount);
} else if (aPath === "heap-allocated") {
heapAllocatedAmounts.push(aAmount);
} else if (aPath === "storage-sqlite") {
storageSqliteAmounts.push(aAmount);
// Check the presence of some other notable reporters.
} else if (aPath.search(/^explicit\/js-non-window\/.*compartment\(/) >= 0) {
areJsNonWindowCompartmentsPresent = true;
} else if (aPath.search(/^explicit\/window-objects\/top\(.*\/js-compartment\(/) >= 0) {
areWindowObjectsJsCompartmentsPresent = true;
} else if (aPath.search(/^explicit\/storage\/sqlite\/places.sqlite/) >= 0) {
isPlacesPresent = true;
} else if (aPath.search(/^explicit\/images/) >= 0) {
isImagesPresent = true;
} else if (aPath.search(/^explicit\/xpti-working-set$/) >= 0) {
isXptiWorkingSetPresent = true;
} else if (aPath.search(/^explicit\/atom-tables$/) >= 0) {
isAtomTablePresent = true;
} else if (/\[System Principal\].*this-is-a-sandbox-name/.test(aPath)) {
// A system compartment with a location (such as a sandbox) should
// show that location.
isSandboxLocationShown = true;
}
}
let mgr = Cc["@mozilla.org/memory-reporter-manager;1"].
getService(Ci.nsIMemoryReporterManager);
// Access mgr.explicit and mgr.resident just to make sure they don't crash.
// We can't check their actual values because they're non-deterministic.
//
// Nb: mgr.explicit will throw NS_ERROR_NOT_AVAILABLE if this is a
// --enable-trace-malloc build. Allow for that exception, but *only* that
// exception.
let dummy;
let haveExplicit = true;
try {
dummy = mgr.explicit;
} catch (ex) {
is(ex.result, Cr.NS_ERROR_NOT_AVAILABLE, "mgr.explicit exception");
haveExplicit = false;
}
dummy = mgr.resident;
let e = mgr.enumerateReporters();
while (e.hasMoreElements()) {
let r = e.getNext().QueryInterface(Ci.nsIMemoryReporter);
handleReport(r.process, r.path, r.kind, r.units, r.amount, r.description);
}
e = mgr.enumerateMultiReporters();
while (e.hasMoreElements()) {
let r = e.getNext().QueryInterface(Ci.nsIMemoryMultiReporter);
r.collectReports(handleReport, null);
// Access |name| to make sure it doesn't crash or assert.
dummy = r.name;
}
function checkSpecialReport(aName, aAmounts)
{
ok(aAmounts.length == 1, aName + " has " + aAmounts.length + " report");
let n = aAmounts[0];
// Check the size is reasonable -- i.e. not ridiculously large or small.
ok(n === kUnknown || (100 * 1000 <= n && n <= 10 * 1000 * 1000 * 1000),
aName + "'s size is reasonable");
}
// If mgr.explicit failed, we won't have "heap-allocated" either.
if (haveExplicit) {
checkSpecialReport("heap-allocated", heapAllocatedAmounts);
}
checkSpecialReport("vsize", vsizeAmounts);
checkSpecialReport("resident", residentAmounts);
checkSpecialReport("js-main-runtime-gc-heap-committed/used/gc-things", jsGcHeapAmounts);
checkSpecialReport("storage-sqlite", storageSqliteAmounts);
ok(areJsNonWindowCompartmentsPresent, "js-non-window compartments are present");
ok(areWindowObjectsJsCompartmentsPresent, "window-objects/.../js compartments are present");
ok(isSandboxLocationShown, "sandbox locations are present");
ok(isPlacesPresent, "places is present");
ok(isImagesPresent, "images is present");
ok(isXptiWorkingSetPresent, "xpti-working-set is present");
ok(isAtomTablePresent, "atom-table is present");
]]>
</script>
</window>