summaryrefslogtreecommitdiff
path: root/jstests/core/views/views_stats.js
blob: 6f098955a003bf8760da2dba78dd7e1409cc2fa4 (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
// 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);
if (lastTop === undefined) {
    return;
}

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);
if (lastTop === undefined) {
    return;
}

lastHistogram = getHistogramStats(view);
assert.commandWorked(coll.insert({}));
assert.commandWorked(coll.update({}, {$set: {x: 1}}));
coll.aggregate([{$match: {}}]);
assert.commandWorked(coll.remove({}));

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