Bug 699307 - Add test_memoryReporters.xul. r=jlebar.
authorNicholas Nethercote <nnethercote@mozilla.com>
Thu, 01 Mar 2012 15:08:38 -0800
changeset 88319 46a1c807df2b929348214f023d766b9ba951ea84
parent 88318 35b1c94b8b073e17dbc65fe864d65ef62b82d0f9
child 88320 fed586e905ec0a8712ca6b9f8127e5133e690b30
push id157
push userMs2ger@gmail.com
push dateWed, 07 Mar 2012 19:27:10 +0000
reviewersjlebar
bugs699307
milestone13.0a1
Bug 699307 - Add test_memoryReporters.xul. r=jlebar.
toolkit/components/aboutmemory/tests/Makefile.in
toolkit/components/aboutmemory/tests/test_aboutcompartments.xul
toolkit/components/aboutmemory/tests/test_aboutmemory.xul
toolkit/components/aboutmemory/tests/test_aboutmemory2.xul
toolkit/components/aboutmemory/tests/test_memoryReporters.xul
--- a/toolkit/components/aboutmemory/tests/Makefile.in
+++ b/toolkit/components/aboutmemory/tests/Makefile.in
@@ -43,14 +43,15 @@ relativesrcdir  = toolkit/components/abo
 
 include $(DEPTH)/config/autoconf.mk
 include $(topsrcdir)/config/rules.mk
 
 _CHROME_FILES	= \
 		test_aboutcompartments.xul \
 		test_aboutmemory.xul \
 		test_aboutmemory2.xul \
+		test_memoryReporters.xul \
 		test_sqliteMultiReporter.xul \
 		$(NULL)
 
 libs:: $(_CHROME_FILES)
 	$(INSTALL) $(foreach f,$^,"$f") $(DEPTH)/_tests/testing/mochitest/chrome/$(relativesrcdir)
 
--- a/toolkit/components/aboutmemory/tests/test_aboutcompartments.xul
+++ b/toolkit/components/aboutmemory/tests/test_aboutcompartments.xul
@@ -1,22 +1,29 @@
 <?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="about:compartments"
         xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
   <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
   <script type="text/javascript" src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script>
 
+  <!-- This file uses fake memory reporters to test the presentation of
+       compartment memory reports in about:memory.  test_memoryReporters.xul
+       uses the real memory reporters to test whether the memory reporters
+       are producing sensible results. -->
+
   <!-- test results are displayed in the html:body -->
   <body xmlns="http://www.w3.org/1999/xhtml"></body>
 
   <!-- test code goes here -->
-  <script type="application/javascript;version=1.8">
+  <script type="application/javascript">
   <![CDATA[
+  "use strict";
+
   const Cc = Components.classes;
   const Ci = Components.interfaces;
   let mgr = Cc["@mozilla.org/memory-reporter-manager;1"].
             getService(Ci.nsIMemoryReporterManager);
 
   // Remove all the real reporters and multi-reporters;  save them to
   // restore at the end.
   var e = mgr.enumerateReporters();
--- a/toolkit/components/aboutmemory/tests/test_aboutmemory.xul
+++ b/toolkit/components/aboutmemory/tests/test_aboutmemory.xul
@@ -1,55 +1,47 @@
 <?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="about:memory"
         xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
   <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
   <script type="text/javascript" src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script>
 
+  <!-- This file uses fake memory reporters to test the presentation of memory 
+       reports in about:memory.  test_memoryReporters.xul uses the real
+       memory reporters to test whether the memory reporters are producing
+       sensible results. -->
+
   <!-- test results are displayed in the html:body -->
   <body xmlns="http://www.w3.org/1999/xhtml"></body>
 
   <!-- test code goes here -->
   <script type="application/javascript">
   <![CDATA[
+  "use strict";
+
   const Cc = Components.classes;
   const Ci = Components.interfaces;
   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.
-  let dummy = mgr.explicit;
-  dummy = mgr.resident;
-
   // Remove all the real reporters and multi-reporters;  save them to
   // restore at the end.
   let e = mgr.enumerateReporters();
   let realReporters = [];
-  dummy = 0;
   while (e.hasMoreElements()) {
     let r = e.getNext().QueryInterface(Ci.nsIMemoryReporter);
-    // Get the |amount| field, even though we don't use it, just to test
-    // that the reporter doesn't crash or anything.
-    dummy += r.amount;
     mgr.unregisterReporter(r);
     realReporters.push(r);
   }
   e = mgr.enumerateMultiReporters();
   let realMultiReporters = [];
-  let dummy = 0;
   while (e.hasMoreElements()) {
     let r = e.getNext().QueryInterface(Ci.nsIMemoryMultiReporter);
-    // Call collectReports, even though we don't use its results, just to
-    // test that the multi-reporter doesn't crash or anything.  And likewise
-    // for the |explicitNonHeap| field.
-    r.collectReports(function(){}, null);
-    dummy += r.explicitNonHeap;
     mgr.unregisterMultiReporter(r);
     realMultiReporters.push(r);
   }
 
   // Setup various fake-but-deterministic reporters.
   const KB = 1024;
   const MB = KB * KB;
   const kUnknown = -1;
--- a/toolkit/components/aboutmemory/tests/test_aboutmemory2.xul
+++ b/toolkit/components/aboutmemory/tests/test_aboutmemory2.xul
@@ -10,16 +10,18 @@
        about:memory. -->
 
   <!-- test results are displayed in the html:body -->
   <body xmlns="http://www.w3.org/1999/xhtml"></body>
 
   <!-- test code goes here -->
   <script type="application/javascript">
   <![CDATA[
+  "use strict";
+
   const Cc = Components.classes;
   const Ci = Components.interfaces;
   let mgr = Cc["@mozilla.org/memory-reporter-manager;1"].
             getService(Ci.nsIMemoryReporterManager);
 
   // Remove all the real reporters and multi-reporters;  save them to
   // restore at the end.
   let e = mgr.enumerateReporters();
new file mode 100644
--- /dev/null
+++ b/toolkit/components/aboutmemory/tests/test_memoryReporters.xul
@@ -0,0 +1,147 @@
+<?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="about:memory"
+        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"></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 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 explicitAmounts = [];
+  let vsizeAmounts = [];
+  let residentAmounts = [];
+  let jsGcHeapAmounts = [];
+  let heapAllocatedAmounts = [];
+  let storageSqliteAmounts = [];
+
+  let areJsCompartmentsPresent = false;
+  let isSandboxLocationShown = false;
+  let areLayoutShellsPresent = false;
+  let areDomInnerWindowsPresent = 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 === "explicit") {
+      explicitAmounts.push(aAmount);
+    } else if (aPath === "vsize") {
+      vsizeAmounts.push(aAmount);
+    } else if (aPath === "resident") {
+      residentAmounts.push(aAmount);
+    } else if (aPath === "js-gc-heap") {
+      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\/compartment\(/) >= 0) {
+      areJsCompartmentsPresent = true;
+    } else if (aPath.search(/^explicit\/layout\/shell\(/) >= 0) {
+      areLayoutShellsPresent = true;
+    } else if (aPath.search(/^explicit\/dom\+style\/window-objects\/.*inner-window\(/) >= 0) {
+      areDomInnerWindowsPresent = 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-table$/) >= 0) {
+      isAtomTablePresent = true;
+    } else if (aPath === "compartments\/system\/[System Principal], this-is-a-sandbox-name") {
+      // 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.
+  let dummy = mgr.explicit;
+  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| and |explicitNonHeap| to make sure they don't crash or
+    // assert.
+    dummy = r.name;
+    dummy = r.explicitNonHeap;
+  }
+
+  function checkSpecialReport(aName, aAmounts)
+  {
+    ok(aAmounts.length == 1, aName + " has exactly 1 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");
+  }
+
+  checkSpecialReport("explicit",       explicitAmounts);
+  checkSpecialReport("vsize",          vsizeAmounts);
+  checkSpecialReport("resident",       residentAmounts);
+  checkSpecialReport("js-gc-heap",     jsGcHeapAmounts);
+  checkSpecialReport("heap-allocated", heapAllocatedAmounts);
+  checkSpecialReport("storage-sqlite", storageSqliteAmounts);
+
+  ok(areJsCompartmentsPresent,  "js compartments are present");
+  ok(isSandboxLocationShown,    "sandbox locations are present");
+  ok(areLayoutShellsPresent,    "layout shells are present");
+  ok(areDomInnerWindowsPresent, "dom inner-windows 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>
+