testing/mochitest/tests/SimpleTest/MemoryStats.js

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:93b4f5e41146
1 /* -*- js-indent-level: 4; tab-width: 4; indent-tabs-mode: nil -*- */
2 /* vim:set ts=4 sw=4 sts=4 et: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 var MemoryStats = {};
8
9 /**
10 * Statistics that we want to retrieve and display after every test is
11 * done. The keys of this table are intended to be identical to the
12 * relevant attributes of nsIMemoryReporterManager. However, since
13 * nsIMemoryReporterManager doesn't necessarily support all these
14 * statistics in all build configurations, we also use this table to
15 * tell us whether statistics are supported or not.
16 */
17 var MEM_STAT_UNKNOWN = 0;
18 var MEM_STAT_UNSUPPORTED = 1;
19 var MEM_STAT_SUPPORTED = 2;
20
21 MemoryStats._hasMemoryStatistics = {}
22 MemoryStats._hasMemoryStatistics.vsize = MEM_STAT_UNKNOWN;
23 MemoryStats._hasMemoryStatistics.vsizeMaxContiguous = MEM_STAT_UNKNOWN;
24 MemoryStats._hasMemoryStatistics.residentFast = MEM_STAT_UNKNOWN;
25 MemoryStats._hasMemoryStatistics.heapAllocated = MEM_STAT_UNKNOWN;
26
27 MemoryStats._getService = function (className, interfaceName) {
28 var service;
29 try {
30 service = Cc[className].getService(Ci[interfaceName]);
31 } catch (e) {
32 service = SpecialPowers.Cc[className]
33 .getService(SpecialPowers.Ci[interfaceName]);
34 }
35 return service;
36 }
37
38 MemoryStats._nsIFile = function (pathname) {
39 var f;
40 var contractID = "@mozilla.org/file/local;1";
41 try {
42 f = Cc[contractID].createInstance(Ci.nsIFile);
43 } catch(e) {
44 f = SpecialPowers.Cc[contractID].createInstance(SpecialPowers.Ci.nsIFile);
45 }
46 f.initWithPath(pathname);
47 return f;
48 }
49
50 MemoryStats.constructPathname = function (directory, basename) {
51 var d = MemoryStats._nsIFile(directory);
52 d.append(basename);
53 return d.path;
54 }
55
56 MemoryStats.dump = function (dumpFn,
57 testNumber,
58 testURL,
59 dumpOutputDirectory,
60 dumpAboutMemory,
61 dumpDMD) {
62 var mrm = MemoryStats._getService("@mozilla.org/memory-reporter-manager;1",
63 "nsIMemoryReporterManager");
64 for (var stat in MemoryStats._hasMemoryStatistics) {
65 var supported = MemoryStats._hasMemoryStatistics[stat];
66 var firstAccess = false;
67 if (supported == MEM_STAT_UNKNOWN) {
68 firstAccess = true;
69 try {
70 var value = mrm[stat];
71 supported = MEM_STAT_SUPPORTED;
72 } catch (e) {
73 supported = MEM_STAT_UNSUPPORTED;
74 }
75 MemoryStats._hasMemoryStatistics[stat] = supported;
76 }
77 if (supported == MEM_STAT_SUPPORTED) {
78 dumpFn("TEST-INFO | MEMORY STAT " + stat + " after test: " + mrm[stat]);
79 } else if (firstAccess) {
80 dumpFn("TEST-INFO | MEMORY STAT " + stat + " not supported in this build configuration.");
81 }
82 }
83
84 if (dumpAboutMemory) {
85 var basename = "about-memory-" + testNumber + ".json.gz";
86 var dumpfile = MemoryStats.constructPathname(dumpOutputDirectory,
87 basename);
88 dumpFn("TEST-INFO | " + testURL + " | MEMDUMP-START " + dumpfile);
89 var md = MemoryStats._getService("@mozilla.org/memory-info-dumper;1",
90 "nsIMemoryInfoDumper");
91 md.dumpMemoryReportsToNamedFile(dumpfile, function () {
92 dumpFn("TEST-INFO | " + testURL + " | MEMDUMP-END");
93 }, null);
94
95 }
96
97 if (dumpDMD && typeof(DMDReportAndDump) != undefined) {
98 var basename = "dmd-" + testNumber + ".txt";
99 var dumpfile = MemoryStats.constructPathname(dumpOutputDirectory,
100 basename);
101 dumpFn("TEST-INFO | " + testURL + " | DMD-DUMP " + dumpfile);
102 DMDReportAndDump(dumpfile);
103 }
104 };

mercurial