summaryrefslogtreecommitdiff
path: root/jstests/indexStatsCommand.js
blob: 9c055e37e2668b7302037c4c1cc54b7c535130c0 (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
db.jstests_commands.drop();
db.createCollection("jstests_commands");

t = db.jstests_commands;

for (var i = 0; i < 3000; ++i) {
    t.insert({i: i, d: i % 13});
}

function textWithIndexVersion(version) {
    var indexName = 'test_d_' + version;
    t.ensureIndex({d: 1}, {v: version, name: indexName});

    var result = t.indexStats({index: indexName});
    if (result["bad cmd"]) {
        print("storageDetails command not available: skipping");
        return;
    }

    assert.commandWorked(result);

    assert(result.index === indexName);
    assert(result.isIdIndex === false);
    assert(isObject(result.keyPattern));
    assert.neq(result.keyPattern, null);
    assert(isString(result.storageNs));
    assert(isNumber(result.bucketBodyBytes));
    assert.eq(result.depth, 1);
    assert(isObject(result.overall));
    assert.neq(result.overall, null);

    function checkStats(data) {
        assert(data.count instanceof NumberLong);
        assert(isNumber(data.mean));
        assert(isNumber(data.stddev));
        assert(isNumber(data.min));
        assert(isNumber(data.max));
    }

    function checkAreaStats(data) {
        assert(isNumber(data.numBuckets));

        assert(isObject(data.keyCount));
        assert.neq(data.keyCount, null);
        checkStats(data.keyCount);

        assert(isObject(data.usedKeyCount));
        assert.neq(data.usedKeyCount, null);
        checkStats(data.usedKeyCount);

        assert(isObject(data.bsonRatio));
        assert.neq(data.bsonRatio, null);
        checkStats(data.bsonRatio);

        assert(isObject(data.keyNodeRatio));
        assert.neq(data.keyNodeRatio, null);
        checkStats(data.keyNodeRatio);

        assert(isObject(data.fillRatio));
        assert.neq(data.fillRatio, null);
        checkStats(data.fillRatio);
    }

    assert(isObject(result.overall));
    checkAreaStats(result.overall);

    assert(result.perLevel instanceof Array);
    for (var i = 0; i < result.perLevel.length; ++i) {
        assert(isObject(result.perLevel[i]));
        checkAreaStats(result.perLevel[i]);
    }

    result = t.indexStats();
    assert.commandFailed(result);
    assert(result.errmsg.match(/index name is required/));

    result = t.indexStats({index: "nonexistent"})
    assert.commandFailed(result);
    assert(result.errmsg.match(/index does not exist/));

    result = t.indexStats({index: "_id_", expandNodes: ['string']})
    assert.commandFailed(result);
    assert(result.errmsg.match(/expandNodes.*numbers/));

    t.dropIndex(indexName);
}

[0, 1].map(textWithIndexVersion);