summaryrefslogtreecommitdiff
path: root/jstests/core/dbstats.js
blob: 324f5dd7603a743715dcc2999b65857ae390fa80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Confirms that the dbStats command returns expected content.
//
// @tags: [requires_dbstats]

(function() {
    "use strict";

    function serverIsMongos() {
        const res = db.runCommand("ismaster");
        assert.commandWorked(res);
        return res.msg === "isdbgrid";
    }

    function serverUsingPersistentStorage() {
        const res = db.runCommand("serverStatus");
        assert.commandWorked(res);
        return res.storageEngine.persistent === true;
    }

    const isMongoS = serverIsMongos();
    const isMMAPv1 = jsTest.options().storageEngine === "mmapv1";
    const isUsingPersistentStorage = !isMongoS && serverUsingPersistentStorage();

    let testDB = db.getSiblingDB("dbstats_js");
    assert.commandWorked(testDB.dropDatabase());

    let coll = testDB["testColl"];
    assert.commandWorked(coll.createIndex({x: 1}));
    const doc = {_id: 1, x: 1};
    assert.writeOK(coll.insert(doc));

    let dbStats = testDB.runCommand({dbStats: 1});
    assert.commandWorked(dbStats);

    if (isMMAPv1) {
        if (isMongoS) {
            // When this test is run against mongoS with the mmapV1 storage engine the 'objects' and
            // 'indexes' counts will vary depending on whether 'testColl' is sharded and on the # of
            // shards (due to inclusion of system.indexes & system.namespaces counts).
            assert(dbStats.hasOwnProperty("objects"), tojson(dbStats));
            assert(dbStats.hasOwnProperty("indexes"), tojson(dbStats));
        } else {
            assert.eq(7,
                      dbStats.objects,
                      tojson(dbStats));  // Includes testColl, system.indexes & system.namespaces
            assert.eq(2, dbStats.indexes, tojson(dbStats));
        }
        // 'dataSize' and 'avgObjSize' include document padding space under MMAPv1.
        assert(dbStats.hasOwnProperty("dataSize"), tojson(dbStats));
        assert(dbStats.hasOwnProperty("avgObjSize"), tojson(dbStats));
    } else {
        assert.eq(1, dbStats.objects, tojson(dbStats));  // Includes testColl only
        const dataSize = Object.bsonsize(doc);
        assert.eq(dataSize, dbStats.avgObjSize, tojson(dbStats));
        assert.eq(dataSize, dbStats.dataSize, tojson(dbStats));

        // Index count will vary on mongoS if an additional index is needed to support sharding.
        if (isMongoS) {
            assert(dbStats.hasOwnProperty("indexes"), tojson(dbStats));
        } else {
            assert.eq(2, dbStats.indexes, tojson(dbStats));
        }
    }

    assert(dbStats.hasOwnProperty("storageSize"), tojson(dbStats));
    assert(dbStats.hasOwnProperty("numExtents"), tojson(dbStats));
    assert(dbStats.hasOwnProperty("indexSize"), tojson(dbStats));

    if (isUsingPersistentStorage) {
        assert(dbStats.hasOwnProperty("fsUsedSize"), tojson(dbStats));
        assert(dbStats.hasOwnProperty("fsTotalSize"), tojson(dbStats));
    }

    // Confirm extentFreeList field existence. Displayed for mongoD running MMAPv1 and for mongoS
    // regardless of storage engine.
    if (isMMAPv1 || isMongoS) {
        assert(dbStats.hasOwnProperty("extentFreeList"), tojson(dbStats));
        assert(dbStats.extentFreeList.hasOwnProperty("num"), tojson(dbStats));
        assert(dbStats.extentFreeList.hasOwnProperty("totalSize"), tojson(dbStats));
    }

    // Confirm collection and view counts on mongoD
    if (!isMongoS) {
        assert.eq(testDB.getName(), dbStats.db, tojson(dbStats));

        // We wait to add a view until this point as it allows more exact testing of avgObjSize for
        // WiredTiger above. Having more than 1 document would require floating point comparison.
        assert.commandWorked(testDB.createView("testView", "testColl", []));

        dbStats = testDB.runCommand({dbStats: 1});
        assert.commandWorked(dbStats);

        if (isMMAPv1) {
            assert.eq(
                4,
                dbStats.collections,
                tojson(dbStats));  // testColl + system.views + system.indexes + system.namespaces
        } else {
            assert.eq(2, dbStats.collections, tojson(dbStats));  // testColl + system.views
        }

        assert.eq(1, dbStats.views, tojson(dbStats));
    }

})();