testing/mochitest/tests/SimpleTest/MemoryStats.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/testing/mochitest/tests/SimpleTest/MemoryStats.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,104 @@
     1.4 +/* -*- js-indent-level: 4; tab-width: 4; indent-tabs-mode: nil -*- */
     1.5 +/* vim:set ts=4 sw=4 sts=4 et: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +var MemoryStats = {};
    1.11 +
    1.12 +/**
    1.13 + * Statistics that we want to retrieve and display after every test is
    1.14 + * done.  The keys of this table are intended to be identical to the
    1.15 + * relevant attributes of nsIMemoryReporterManager.  However, since
    1.16 + * nsIMemoryReporterManager doesn't necessarily support all these
    1.17 + * statistics in all build configurations, we also use this table to
    1.18 + * tell us whether statistics are supported or not.
    1.19 + */
    1.20 +var MEM_STAT_UNKNOWN = 0;
    1.21 +var MEM_STAT_UNSUPPORTED = 1;
    1.22 +var MEM_STAT_SUPPORTED = 2;
    1.23 +
    1.24 +MemoryStats._hasMemoryStatistics = {}
    1.25 +MemoryStats._hasMemoryStatistics.vsize = MEM_STAT_UNKNOWN;
    1.26 +MemoryStats._hasMemoryStatistics.vsizeMaxContiguous = MEM_STAT_UNKNOWN;
    1.27 +MemoryStats._hasMemoryStatistics.residentFast = MEM_STAT_UNKNOWN;
    1.28 +MemoryStats._hasMemoryStatistics.heapAllocated = MEM_STAT_UNKNOWN;
    1.29 +
    1.30 +MemoryStats._getService = function (className, interfaceName) {
    1.31 +    var service;
    1.32 +    try {
    1.33 +        service = Cc[className].getService(Ci[interfaceName]);
    1.34 +    } catch (e) {
    1.35 +        service = SpecialPowers.Cc[className]
    1.36 +                               .getService(SpecialPowers.Ci[interfaceName]);
    1.37 +    }
    1.38 +    return service;
    1.39 +}
    1.40 +
    1.41 +MemoryStats._nsIFile = function (pathname) {
    1.42 +    var f;
    1.43 +    var contractID = "@mozilla.org/file/local;1";
    1.44 +    try {
    1.45 +        f = Cc[contractID].createInstance(Ci.nsIFile);
    1.46 +    } catch(e) {
    1.47 +        f = SpecialPowers.Cc[contractID].createInstance(SpecialPowers.Ci.nsIFile);
    1.48 +    }
    1.49 +    f.initWithPath(pathname);
    1.50 +    return f;
    1.51 +}
    1.52 +
    1.53 +MemoryStats.constructPathname = function (directory, basename) {
    1.54 +    var d = MemoryStats._nsIFile(directory);
    1.55 +    d.append(basename);
    1.56 +    return d.path;
    1.57 +}
    1.58 +
    1.59 +MemoryStats.dump = function (dumpFn,
    1.60 +                             testNumber,
    1.61 +                             testURL,
    1.62 +                             dumpOutputDirectory,
    1.63 +                             dumpAboutMemory,
    1.64 +                             dumpDMD) {
    1.65 +    var mrm = MemoryStats._getService("@mozilla.org/memory-reporter-manager;1",
    1.66 +                                      "nsIMemoryReporterManager");
    1.67 +    for (var stat in MemoryStats._hasMemoryStatistics) {
    1.68 +        var supported = MemoryStats._hasMemoryStatistics[stat];
    1.69 +        var firstAccess = false;
    1.70 +        if (supported == MEM_STAT_UNKNOWN) {
    1.71 +            firstAccess = true;
    1.72 +            try {
    1.73 +                var value = mrm[stat];
    1.74 +                supported = MEM_STAT_SUPPORTED;
    1.75 +            } catch (e) {
    1.76 +                supported = MEM_STAT_UNSUPPORTED;
    1.77 +            }
    1.78 +            MemoryStats._hasMemoryStatistics[stat] = supported;
    1.79 +        }
    1.80 +        if (supported == MEM_STAT_SUPPORTED) {
    1.81 +            dumpFn("TEST-INFO | MEMORY STAT " + stat + " after test: " + mrm[stat]);
    1.82 +        } else if (firstAccess) {
    1.83 +            dumpFn("TEST-INFO | MEMORY STAT " + stat + " not supported in this build configuration.");
    1.84 +        }
    1.85 +    }
    1.86 +
    1.87 +    if (dumpAboutMemory) {
    1.88 +        var basename = "about-memory-" + testNumber + ".json.gz";
    1.89 +        var dumpfile = MemoryStats.constructPathname(dumpOutputDirectory,
    1.90 +                                                     basename);
    1.91 +        dumpFn("TEST-INFO | " + testURL + " | MEMDUMP-START " + dumpfile);
    1.92 +        var md = MemoryStats._getService("@mozilla.org/memory-info-dumper;1",
    1.93 +                                         "nsIMemoryInfoDumper");
    1.94 +        md.dumpMemoryReportsToNamedFile(dumpfile, function () {
    1.95 +            dumpFn("TEST-INFO | " + testURL + " | MEMDUMP-END");
    1.96 +        }, null);
    1.97 +
    1.98 +    }
    1.99 +
   1.100 +    if (dumpDMD && typeof(DMDReportAndDump) != undefined) {
   1.101 +        var basename = "dmd-" + testNumber + ".txt";
   1.102 +        var dumpfile = MemoryStats.constructPathname(dumpOutputDirectory,
   1.103 +                                                     basename);
   1.104 +        dumpFn("TEST-INFO | " + testURL + " | DMD-DUMP " + dumpfile);
   1.105 +        DMDReportAndDump(dumpfile);
   1.106 +    }
   1.107 +};

mercurial