summaryrefslogtreecommitdiff
path: root/jstests/core/profile_find.js
blob: 353eb5c3d704b092f4cc298306a9c1254cd34222 (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// @tags: [does_not_support_stepdowns, requires_profiling]

// 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 testDB = db.getSiblingDB("profile_find");
assert.commandWorked(testDB.dropDatabase());
var coll = testDB.getCollection("test");
var isLegacyReadMode = (testDB.getMongo().readMode() === "legacy");

testDB.setProfilingLevel(2);
const profileEntryFilter = {
    op: "query"
};

//
// Confirm most metrics on single document read.
//
var i;
for (i = 0; i < 3; ++i) {
    assert.commandWorked(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, profileEntryFilter);

assert.eq(profileObj.ns, coll.getFullName(), 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 }", tojson(profileObj));
assert(profileObj.execStats.hasOwnProperty("stage"), tojson(profileObj));
assert.eq(profileObj.command.filter, {a: 1}, tojson(profileObj));
if (isLegacyReadMode) {
    assert.eq(profileObj.command.ntoreturn, -1, tojson(profileObj));
} else {
    assert.eq(profileObj.command.limit, 1, tojson(profileObj));
    assert.eq(profileObj.protocol,
              getProfilerProtocolStringForCommand(testDB.getMongo()),
              tojson(profileObj));
}

if (!isLegacyReadMode) {
    assert.eq(profileObj.command.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.commandWorked(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, profileEntryFilter);

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.commandWorked(coll.insert({a: i, b: i}));
}

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

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.commandWorked(coll.insert({a: 5, b: i}));
    assert.commandWorked(coll.insert({a: i, b: 10}));
}

// Until we get the failpoint described in the above comment (regarding SERVER-23620), we must
// run the query twice. The first time will create an inactive cache entry. The second run will
// take the same number of works, and create an active cache entry.
assert.neq(coll.findOne({a: 5, b: 15}), null);
assert.neq(coll.findOne({a: 5, b: 15}), null);

// Run a query with the same shape, but with different parameters. The plan cached for the
// query above will perform poorly (since the selectivities are different) and we will be
// forced to replan.
assert.neq(coll.findOne({a: 15, b: 10}), null);
profileObj = getLatestProfilerEntry(testDB, profileEntryFilter);

assert.eq(profileObj.replanned, true, profileObj);
assert(profileObj.hasOwnProperty('replanReason'), profileObj);
assert(
    profileObj.replanReason.match(
        /cached plan was less efficient than expected: expected trial execution to take [0-9]+ works but it took at least [0-9]+ works/),
    profileObj);
assert.eq(profileObj.appName, "MongoDB Shell", profileObj);

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

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

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

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

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

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

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

//
// Confirm that queries are truncated in the profiler as { $truncated: <string>, comment:
// <string> }
//
let queryPredicate = {};

for (let i = 0; i < 501; i++) {
    queryPredicate[i] = "a".repeat(150);
}

assert.eq(coll.find(queryPredicate).comment("profile_find").itcount(), 0);
profileObj = getLatestProfilerEntry(testDB, profileEntryFilter);
assert.eq((typeof profileObj.command.$truncated), "string", tojson(profileObj));
assert.eq(profileObj.command.comment, "profile_find", tojson(profileObj));

//
// Confirm that a query whose filter contains a field named 'query' appears as expected in the
// profiler. This test ensures that upconverting a legacy query correctly identifies this as a
// user field rather than a wrapped filter spec.
//
coll.find({query: "foo"}).itcount();
profileObj = getLatestProfilerEntry(testDB, profileEntryFilter);
assert.eq(profileObj.command.filter, {query: "foo"}, tojson(profileObj));
})();