summaryrefslogtreecommitdiff
path: root/jstests/core/views/views_stats.js
blob: 75feb857c9a48c75880e4980a872248ea6743691 (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
// Test that top and latency histogram statistics are recorded for views.

(function() {
    "use strict";
    load("jstests/libs/stats.js");

    let viewsDB = db.getSiblingDB("views_stats");
    assert.commandWorked(viewsDB.dropDatabase());
    assert.commandWorked(viewsDB.runCommand({create: "view", viewOn: "collection"}));

    let view = viewsDB["view"];
    let coll = viewsDB["collection"];

    // Check the histogram counters.
    let lastHistogram = getHistogramStats(view);
    view.aggregate([{$match: {}}]);
    lastHistogram = assertHistogramDiffEq(view, lastHistogram, 1, 0, 0);

    // Check that failed inserts, updates, and deletes are counted.
    assert.writeError(view.insert({}));
    lastHistogram = assertHistogramDiffEq(view, lastHistogram, 0, 1, 0);

    assert.writeError(view.remove({}));
    lastHistogram = assertHistogramDiffEq(view, lastHistogram, 0, 1, 0);

    assert.writeError(view.update({}, {}));
    lastHistogram = assertHistogramDiffEq(view, lastHistogram, 0, 1, 0);

    let isMasterResponse = assert.commandWorked(viewsDB.runCommand("isMaster"));
    const isMongos = (isMasterResponse.msg === "isdbgrid");
    if (isMongos) {
        jsTest.log("Tests are being run on a mongos; skipping top tests.");
        return;
    }

    // Check the top counters.
    let lastTop = getTop(view);
    view.aggregate([{$match: {}}]);
    lastTop = assertTopDiffEq(view, lastTop, "commands", 1);

    assert.writeError(view.insert({}));
    lastTop = assertTopDiffEq(view, lastTop, "insert", 1);

    assert.writeError(view.remove({}));
    lastTop = assertTopDiffEq(view, lastTop, "remove", 1);

    assert.writeError(view.update({}, {}));
    lastTop = assertTopDiffEq(view, lastTop, "update", 1);

    // Check that operations on the backing collection do not modify the view stats.
    lastTop = getTop(view);
    lastHistogram = getHistogramStats(view);
    assert.writeOK(coll.insert({}));
    assert.writeOK(coll.update({}, {$set: {x: 1}}));
    coll.aggregate([{$match: {}}]);
    assert.writeOK(coll.remove({}));

    assertTopDiffEq(view, lastTop, "insert", 0);
    assertTopDiffEq(view, lastTop, "update", 0);
    assertTopDiffEq(view, lastTop, "remove", 0);
    assertHistogramDiffEq(view, lastHistogram, 0, 0, 0);
}());