summaryrefslogtreecommitdiff
path: root/jstests/core/views/views_stats.js
blob: 6c1b4b976d610af8b9aea0d4e86e70ac89f6b9fe (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
// Test that top and latency histogram statistics are recorded for views.
//
// This test attempts to perform write operations and get latency statistics using the $collStats
// stage. The former operation must be routed to the primary in a replica set, whereas the latter
// may be routed to a secondary.
//
// @tags: [
//     assumes_read_preference_unchanged,
//
//     # top command is not available on embedded
//     incompatible_with_embedded,
// ]

(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);
}());