summaryrefslogtreecommitdiff
path: root/jstests/core/profile4.js
blob: a1e99e6ce596a4b21107b536f00c109c5a7fea55 (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/**
 * Check debug information recorded for a query.
 */

function profileCursor() {
    var userStr = username + "@" + db.getName();
    return db.system.profile.find({user: userStr});
}

function getLastOp() {
    return profileCursor().sort({$natural: -1}).next();
}

// Special db so that it can be run in parallel tests. Also need to create a user so that
// operations run by this test (and not other tests running in parallel) can be identified.
var stddb = db;
var db = db.getSisterDB("profile4");

db.dropAllUsers();
var coll = db.profile4;
coll.drop();

var username = "jstests_profile4_user";
db.createUser({user: username, pwd: "password", roles: jsTest.basicUserRoles});
db.auth(username, "password");

try {
    var lastOp;

    // Clear the profiling collection.
    db.setProfilingLevel(0);
    db.system.profile.drop();
    assert.eq(0, profileCursor().count());

    // Enable profiling. It will be disabled again at the end of the test, or if the test fails.
    db.setProfilingLevel(2);

    coll.find().itcount();
    lastOp = getLastOp();
    assert.eq(lastOp.op, "query");
    assert.eq(lastOp.query.find, coll.getName());
    assert.eq(lastOp.ns, coll.getFullName());
    assert.eq(lastOp.keysExamined, 0);
    assert.eq(lastOp.keyUpdates, 0);
    assert.eq(lastOp.nreturned, 0);
    assert.eq(lastOp.cursorExhausted, true);

    // Check write lock stats are set.
    coll.save({});
    lastOp = getLastOp();
    assert.eq(lastOp.op, "insert");
    assert.lt(0, Object.keys(lastOp.locks).length);

    // Check read lock stats are set.
    coll.find();
    lastOp = getLastOp();
    assert.eq(lastOp.op, "query");
    assert.lt(0, Object.keys(lastOp.locks).length);

    coll.save({});
    coll.save({});
    coll.find().skip(1).limit(4).itcount();
    lastOp = getLastOp();
    assert.eq(lastOp.query.skip, 1);
    assert.eq(lastOp.docsExamined, 3);
    assert.eq(lastOp.nreturned, 2);
    // Find command will use "limit", OP_QUERY will use ntoreturn.
    var expectedField = db.getMongo().useReadCommands() ? "limit" : "ntoreturn";
    assert.eq(lastOp.query[expectedField], 4);

    coll.find().batchSize(2).next();
    lastOp = getLastOp();
    assert.lt(0, lastOp.cursorid);

    coll.find({a: 1}).itcount();
    lastOp = getLastOp();
    assert.eq(lastOp.query.filter, {a: 1});

    coll.find({_id: 0}).itcount();
    lastOp = getLastOp();
    assert.eq(lastOp.idhack, true);

    coll.find().sort({a: 1}).itcount();
    lastOp = getLastOp();
    assert.eq(lastOp.hasSortStage, true);

    coll.ensureIndex({a: 1});
    coll.find({a: 1}).itcount();
    lastOp = getLastOp();
    assert.eq("FETCH", lastOp.execStats.stage, tojson(lastOp.execStats));
    assert.eq("IXSCAN", lastOp.execStats.inputStage.stage, tojson(lastOp.execStats));

    // For queries with a lot of stats data, the execution stats in the profile is replaced by
    // the plan summary.
    var orClauses = 32;
    var bigOrQuery = {
        $or: []
    };
    for (var i = 0; i < orClauses; ++i) {
        var indexSpec = {};
        indexSpec["a" + i] = 1;
        coll.ensureIndex(indexSpec);
        bigOrQuery["$or"].push(indexSpec);
    }
    coll.find(bigOrQuery).itcount();
    lastOp = getLastOp();
    assert.neq(undefined, lastOp.execStats.summary, tojson(lastOp.execStats));

    // Confirm "cursorExhausted" not set when cursor is open.
    coll.drop();
    coll.insert([{_id: 0}, {_id: 1}, {_id: 2}, {_id: 3}, {_id: 4}]);
    coll.find().batchSize(2).next();  // Query performed leaving open cursor
    lastOp = getLastOp();
    assert.eq(lastOp.op, "query");
    assert(!("cursorExhausted" in lastOp));

    var cursor = coll.find().batchSize(2);
    cursor.next();  // Perform initial query and consume first of 2 docs returned.
    cursor.next();  // Consume second of 2 docs from initial query.
    cursor.next();  // getMore performed, leaving open cursor.
    lastOp = getLastOp();
    assert.eq(lastOp.op, "getmore");
    assert(!("cursorExhausted" in lastOp));

    // Exhaust cursor and confirm getMore has "cursorExhausted:true".
    cursor.itcount();
    lastOp = getLastOp();
    assert.eq(lastOp.cursorExhausted, true);

    // OP_QUERY-specific test for the "wrapped" query predicate form.
    if (!db.getMongo().useReadCommands()) {
        // Must accept non-dollar-prefixed forms of "query" and "orderby".
        coll.find({query: {_id: {$gte: 0}}, orderby: {_id: 1}}).itcount();
        lastOp = getLastOp();
        assert.eq(lastOp.op, "query");
        assert.eq(lastOp.query.find, coll.getName());
        assert.eq(lastOp.query.filter, {_id: {$gte: 0}});
        assert.eq(lastOp.query.sort, {_id: 1});

        // Should also work with dollar-prefixed forms.
        coll.find({$query: {_id: {$gte: 0}}, $orderby: {_id: 1}}).itcount();
        lastOp = getLastOp();
        assert.eq(lastOp.op, "query");
        assert.eq(lastOp.query.find, coll.getName());
        assert.eq(lastOp.query.filter, {_id: {$gte: 0}});
        assert.eq(lastOp.query.sort, {_id: 1});

        // Negative ntoreturn.
        coll.find().limit(-3).itcount();
        lastOp = getLastOp();
        assert.eq(lastOp.query.ntoreturn, -3);
    }

    // getMore command should show up as a getMore operation, not a command.
    var cmdRes = db.runCommand({find: coll.getName(), batchSize: 1});
    assert.commandWorked(cmdRes);
    db.runCommand({getMore: cmdRes.cursor.id, collection: coll.getName()});
    lastOp = getLastOp();
    assert.eq(lastOp.op, "getmore");
    assert.eq(lastOp.ns, coll.getFullName());

    // getMore entry created by iterating the cursor should have the same format, regardless of
    // readMode.
    coll.find().batchSize(3).itcount();
    lastOp = getLastOp();
    assert.eq(lastOp.op, "getmore");
    assert.eq(lastOp.ns, coll.getFullName());
    assert("getMore" in lastOp.query);
    assert.eq(lastOp.query.getMore, lastOp.cursorid);
    assert.eq(lastOp.query.collection, coll.getName());
    assert.eq(lastOp.query.batchSize, 3);
    assert.eq(lastOp.cursorExhausted, true);
    assert.eq(lastOp.nreturned, 2);
    assert("responseLength" in lastOp);

    // Ensure that special $-prefixed OP_QUERY options like $hint and $returnKey get added to the
    // profiler entry correctly.
    coll.find().hint({_id: 1}).itcount();
    lastOp = getLastOp();
    assert.eq(lastOp.query.hint, {_id: 1});

    coll.find().comment("a comment").itcount();
    lastOp = getLastOp();
    assert.eq(lastOp.query.comment, "a comment");

    coll.find().maxScan(3000).itcount();
    lastOp = getLastOp();
    assert.eq(lastOp.query.maxScan, 3000);

    coll.find().maxTimeMS(4000).itcount();
    lastOp = getLastOp();
    assert.eq(lastOp.query.maxTimeMS, 4000);

    coll.find().max({_id: 3}).itcount();
    lastOp = getLastOp();
    assert.eq(lastOp.query.max, {_id: 3});

    coll.find().min({_id: 0}).itcount();
    lastOp = getLastOp();
    assert.eq(lastOp.query.min, {_id: 0});

    coll.find().returnKey().itcount();
    lastOp = getLastOp();
    assert.eq(lastOp.query.returnKey, true);

    coll.find().snapshot().itcount();
    lastOp = getLastOp();
    assert.eq(lastOp.query.snapshot, true);

    // Tests for profiling findAndModify.
    coll.drop();
    for (var i = 0; i < 3; i++) {
        assert.writeOK(coll.insert({_id: i, a: i}));
    }

    // Update as findAndModify.
    assert.eq({_id: 2, a: 2}, coll.findAndModify({query: {a: 2}, update: {$inc: {b: 1}}}));
    lastOp = getLastOp();
    assert.eq(lastOp.op, "command");
    assert.eq(lastOp.ns, coll.getFullName());
    assert.eq(lastOp.command.query, {a: 2});
    assert.eq(lastOp.command.update, {$inc: {b: 1}});
    assert.eq(lastOp.updateobj, {$inc: {b: 1}});
    assert.eq(lastOp.keysExamined, 0);
    assert.eq(lastOp.docsExamined, 3);
    assert.eq(lastOp.nMatched, 1);
    assert.eq(lastOp.nModified, 1);

    // Delete as findAndModify.
    assert.eq({_id: 2, a: 2, b: 1}, coll.findAndModify({query: {a: 2}, remove: true}));
    lastOp = getLastOp();
    assert.eq(lastOp.op, "command");
    assert.eq(lastOp.ns, coll.getFullName());
    assert.eq(lastOp.command.query, {a: 2});
    assert.eq(lastOp.command.remove, true);
    assert(!("updateobj" in lastOp));
    assert.eq(lastOp.ndeleted, 1);

    // Update with {upsert: true} as findAndModify.
    assert.eq({_id: 2, a: 2, b: 1},
              coll.findAndModify(
                  {query: {_id: 2, a: 2}, update: {$inc: {b: 1}}, upsert: true, new: true}));
    lastOp = getLastOp();
    assert.eq(lastOp.op, "command");
    assert.eq(lastOp.ns, coll.getFullName());
    assert.eq(lastOp.command.query, {_id: 2, a: 2});
    assert.eq(lastOp.command.update, {$inc: {b: 1}});
    assert.eq(lastOp.command.upsert, true);
    assert.eq(lastOp.command.new, true);
    assert.eq(lastOp.updateobj, {$inc: {b: 1}});
    assert.eq(lastOp.keysExamined, 0);
    assert.eq(lastOp.docsExamined, 0);
    assert.eq(lastOp.nMatched, 1);
    assert.eq(lastOp.nModified, 1);
    assert.eq(lastOp.upsert, true);

    // Idhack update as findAndModify.
    assert.eq({_id: 2, a: 2, b: 1}, coll.findAndModify({query: {_id: 2}, update: {$inc: {b: 1}}}));
    lastOp = getLastOp();
    assert.eq(lastOp.keysExamined, 1);
    assert.eq(lastOp.docsExamined, 1);
    assert.eq(lastOp.nMatched, 1);
    assert.eq(lastOp.nModified, 1);

    // Update as findAndModify with projection.
    assert.eq({a: 2},
              coll.findAndModify({query: {a: 2}, update: {$inc: {b: 1}}, fields: {_id: 0, a: 1}}));
    lastOp = getLastOp();
    assert.eq(lastOp.op, "command");
    assert.eq(lastOp.ns, coll.getFullName());
    assert.eq(lastOp.command.query, {a: 2});
    assert.eq(lastOp.command.update, {$inc: {b: 1}});
    assert.eq(lastOp.command.fields, {_id: 0, a: 1});
    assert.eq(lastOp.updateobj, {$inc: {b: 1}});
    assert.eq(lastOp.keysExamined, 0);
    assert.eq(lastOp.docsExamined, 3);
    assert.eq(lastOp.nMatched, 1);
    assert.eq(lastOp.nModified, 1);

    // Delete as findAndModify with projection.
    assert.eq({a: 2}, coll.findAndModify({query: {a: 2}, remove: true, fields: {_id: 0, a: 1}}));
    lastOp = getLastOp();
    assert.eq(lastOp.op, "command");
    assert.eq(lastOp.ns, coll.getFullName());
    assert.eq(lastOp.command.query, {a: 2});
    assert.eq(lastOp.command.remove, true);
    assert.eq(lastOp.command.fields, {_id: 0, a: 1});
    assert(!("updateobj" in lastOp));
    assert.eq(lastOp.ndeleted, 1);

    db.setProfilingLevel(0);
    db.system.profile.drop();
} finally {
    db.setProfilingLevel(0);
    db = stddb;
}