summaryrefslogtreecommitdiff
path: root/jstests/core/profile_find.js
blob: 43f99e97019768300c0b47d93d0bb7a15d6abbc3 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Confirms that profiled find execution contains all expected metrics with proper values.

(function() {
    "use strict";

    // For getLatestProfilerEntry and getProfilerProtocolStringForCommand
    load("jstests/libs/profiler.js");

    var conn = new Mongo(db.getMongo().host);
    var testDB = conn.getDB("profile_find");
    assert.commandWorked(testDB.dropDatabase());
    var coll = testDB.getCollection("test");
    var isLegacyReadMode = (db.getMongo().readMode() === "legacy");

    testDB.setProfilingLevel(2);

    //
    // Confirm most metrics on single document read.
    //
    var i;
    for (i = 0; i < 3; ++i) {
        assert.writeOK(coll.insert({a: i, b: i}));
    }
    assert.commandWorked(coll.createIndex({a: 1}, {collation: {locale: "fr"}}));

    if (!isLegacyReadMode) {
        assert.eq(coll.find({a: 1}).collation({locale: "fr"}).limit(1).itcount(), 1);
    } else {
        assert.neq(coll.findOne({a: 1}), null);
    }

    var profileObj = getLatestProfilerEntry(testDB);

    assert.eq(profileObj.ns, coll.getFullName(), tojson(profileObj));
    assert.eq(profileObj.op, "query", tojson(profileObj));
    assert.eq(profileObj.keysExamined, 1, tojson(profileObj));
    assert.eq(profileObj.docsExamined, 1, tojson(profileObj));
    assert.eq(profileObj.nreturned, 1, tojson(profileObj));
    assert.eq(profileObj.planSummary, "IXSCAN { a: 1.0 }", tojson(profileObj));
    assert(profileObj.execStats.hasOwnProperty("stage"), tojson(profileObj));
    assert.eq(profileObj.query.filter, {a: 1}, tojson(profileObj));
    if (isLegacyReadMode) {
        assert.eq(profileObj.query.ntoreturn, -1, tojson(profileObj));
    } else {
        assert.eq(profileObj.query.limit, 1, tojson(profileObj));
        assert.eq(
            profileObj.protocol, getProfilerProtocolStringForCommand(conn), tojson(profileObj));
    }

    if (!isLegacyReadMode) {
        assert.eq(profileObj.query.collation, {locale: "fr"});
    }
    assert.eq(profileObj.cursorExhausted, true, tojson(profileObj));
    assert(!profileObj.hasOwnProperty("cursorid"), tojson(profileObj));
    assert(profileObj.hasOwnProperty("responseLength"), tojson(profileObj));
    assert(profileObj.hasOwnProperty("millis"), tojson(profileObj));
    assert(profileObj.hasOwnProperty("numYield"), tojson(profileObj));
    assert(profileObj.hasOwnProperty("locks"), tojson(profileObj));
    assert(profileObj.locks.hasOwnProperty("Global"), tojson(profileObj));
    assert(profileObj.locks.hasOwnProperty("Database"), tojson(profileObj));
    assert(profileObj.locks.hasOwnProperty("Collection"), tojson(profileObj));
    assert.eq(profileObj.appName, "MongoDB Shell", tojson(profileObj));

    //
    // Confirm "cursorId" and "hasSortStage" metrics.
    //
    coll.drop();
    for (i = 0; i < 3; ++i) {
        assert.writeOK(coll.insert({a: i, b: i}));
    }
    assert.commandWorked(coll.createIndex({a: 1}));

    assert.neq(coll.findOne({a: 1}), null);

    assert.neq(coll.find({a: {$gte: 0}}).sort({b: 1}).batchSize(1).next(), null);
    profileObj = getLatestProfilerEntry(testDB);

    assert.eq(profileObj.hasSortStage, true, tojson(profileObj));
    assert(profileObj.hasOwnProperty("cursorid"), tojson(profileObj));
    assert(!profileObj.hasOwnProperty("cursorExhausted"), tojson(profileObj));
    assert.eq(profileObj.appName, "MongoDB Shell", tojson(profileObj));

    //
    // Confirm "fromMultiPlanner" metric.
    //
    coll.drop();
    assert.commandWorked(coll.createIndex({a: 1}));
    assert.commandWorked(coll.createIndex({b: 1}));
    for (i = 0; i < 5; ++i) {
        assert.writeOK(coll.insert({a: i, b: i}));
    }

    assert.neq(coll.findOne({a: 3, b: 3}), null);
    profileObj = getLatestProfilerEntry(testDB);

    assert.eq(profileObj.fromMultiPlanner, true, tojson(profileObj));
    assert.eq(profileObj.appName, "MongoDB Shell", tojson(profileObj));

    //
    // Confirm "replanned" metric.
    // We should ideally be using a fail-point to trigger "replanned" rather than relying on
    // current query planner behavior knowledge to setup a scenario. SERVER-23620 has been entered
    // to add this fail-point and to update appropriate tests.
    //
    coll.drop();
    assert.commandWorked(coll.createIndex({a: 1}));
    assert.commandWorked(coll.createIndex({b: 1}));
    for (i = 0; i < 20; ++i) {
        assert.writeOK(coll.insert({a: 5, b: i}));
        assert.writeOK(coll.insert({a: i, b: 10}));
    }
    assert.neq(coll.findOne({a: 5, b: 15}), null);
    assert.neq(coll.findOne({a: 15, b: 10}), null);
    profileObj = getLatestProfilerEntry(testDB);

    assert.eq(profileObj.replanned, true, tojson(profileObj));
    assert.eq(profileObj.appName, "MongoDB Shell", tojson(profileObj));

    //
    // Confirm that query modifiers such as "hint" are in the profiler document.
    //
    coll.drop();
    assert.writeOK(coll.insert({_id: 2}));

    assert.eq(coll.find().hint({_id: 1}).itcount(), 1);
    profileObj = getLatestProfilerEntry(testDB);
    assert.eq(profileObj.query.hint, {_id: 1}, tojson(profileObj));

    assert.eq(coll.find().comment("a comment").itcount(), 1);
    profileObj = getLatestProfilerEntry(testDB);
    assert.eq(profileObj.query.comment, "a comment", tojson(profileObj));

    assert.eq(coll.find().maxScan(3000).itcount(), 1);
    profileObj = getLatestProfilerEntry(testDB);
    assert.eq(profileObj.query.maxScan, 3000, tojson(profileObj));

    var maxTimeMS = 100000;
    assert.eq(coll.find().maxTimeMS(maxTimeMS).itcount(), 1);
    profileObj = getLatestProfilerEntry(testDB);
    assert.eq(profileObj.query.maxTimeMS, maxTimeMS, tojson(profileObj));

    assert.eq(coll.find().max({_id: 3}).itcount(), 1);
    profileObj = getLatestProfilerEntry(testDB);
    assert.eq(profileObj.query.max, {_id: 3}, tojson(profileObj));

    assert.eq(coll.find().min({_id: 0}).itcount(), 1);
    profileObj = getLatestProfilerEntry(testDB);
    assert.eq(profileObj.query.min, {_id: 0}, tojson(profileObj));

    assert.eq(coll.find().returnKey().itcount(), 1);
    profileObj = getLatestProfilerEntry(testDB);
    assert.eq(profileObj.query.returnKey, true, tojson(profileObj));

    assert.eq(coll.find().snapshot().itcount(), 1);
    profileObj = getLatestProfilerEntry(testDB);
    assert.eq(profileObj.query.snapshot, true, tojson(profileObj));
})();