summaryrefslogtreecommitdiff
path: root/jstests/core/profile_getmore.js
blob: b6f4ecf3ac7104d7957f686b7981c61d41a1b40c (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
// @tags: [
//   does_not_support_stepdowns,
//   requires_getmore,
//   requires_profiling,
//   requires_fcv_51,
// ]

// Confirms that profiled getMore execution contains all expected metrics with proper values.

(function() {
"use strict";

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

const testDB = db.getSiblingDB("profile_getmore");
assert.commandWorked(testDB.dropDatabase());
const coll = testDB.getCollection("test");

testDB.setProfilingLevel(2);

let i;
//
// Confirm basic metrics on getMore with a not-exhausted cursor.
//
for (i = 0; i < 10; ++i) {
    assert.commandWorked(coll.insert({a: i}));
}
assert.commandWorked(coll.createIndex({a: 1}));

let cursor = coll.find({a: {$gt: 0}}).sort({a: 1}).batchSize(2);
cursor.next();  // Perform initial query and consume first of 2 docs returned.

let cursorId = getLatestProfilerEntry(testDB, {op: "query"}).cursorid;  // Save cursorid from find.

cursor.next();  // Consume second of 2 docs from initial query.
cursor.next();  // getMore performed, leaving open cursor.

let profileObj = getLatestProfilerEntry(testDB, {op: "getmore"});

assert.eq(profileObj.ns, coll.getFullName(), profileObj);
assert.eq(profileObj.op, "getmore", profileObj);
assert.eq(profileObj.keysExamined, 2, profileObj);
assert.eq(profileObj.docsExamined, 2, profileObj);
assert.eq(profileObj.cursorid, cursorId, profileObj);
assert.eq(profileObj.nreturned, 2, profileObj);
assert.eq(profileObj.command.getMore, cursorId, profileObj);
assert.eq(profileObj.command.collection, coll.getName(), profileObj);
assert.eq(profileObj.command.batchSize, 2, profileObj);
assert.eq(profileObj.originatingCommand.filter, {a: {$gt: 0}});
assert.eq(profileObj.originatingCommand.sort, {a: 1});
assert.eq(profileObj.planSummary, "IXSCAN { a: 1 }", profileObj);
assert(profileObj.hasOwnProperty("execStats"), profileObj);
assert(profileObj.execStats.hasOwnProperty("stage"), profileObj);
assert(profileObj.hasOwnProperty("responseLength"), profileObj);
assert(profileObj.hasOwnProperty("numYield"), profileObj);
assert(profileObj.hasOwnProperty("locks"), profileObj);
assert(profileObj.locks.hasOwnProperty("Global"), profileObj);
assert(profileObj.hasOwnProperty("millis"), profileObj);
assert(!profileObj.hasOwnProperty("cursorExhausted"), profileObj);
assert.eq(profileObj.appName, "MongoDB Shell", profileObj);
assert(profileObj.hasOwnProperty("queryHash"), profileObj);

//
// Confirm hasSortStage on getMore with a not-exhausted cursor and in-memory sort.
//
coll.drop();
for (i = 0; i < 10; ++i) {
    assert.commandWorked(coll.insert({a: i}));
}

cursor = coll.find({a: {$gt: 0}}).sort({a: 1}).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.

profileObj = getLatestProfilerEntry(testDB, {op: "getmore"});

assert.eq(profileObj.hasSortStage, true, profileObj);

//
// Confirm "cursorExhausted" metric.
//
coll.drop();
for (i = 0; i < 3; ++i) {
    assert.commandWorked(coll.insert({a: i}));
}

cursor = coll.find().batchSize(2);
cursor.next();     // Perform initial query and consume first of 3 docs returned.
cursor.itcount();  // Exhaust the cursor.

profileObj = getLatestProfilerEntry(testDB, {op: "getmore"});

assert(profileObj.hasOwnProperty("cursorid"),
       profileObj);  // cursorid should always be present on getMore.
assert.neq(0, profileObj.cursorid, profileObj);
assert.eq(profileObj.cursorExhausted, true, profileObj);
assert.eq(profileObj.appName, "MongoDB Shell", profileObj);

//
// Confirm getMore on aggregation.
//
coll.drop();
for (i = 0; i < 20; ++i) {
    assert.commandWorked(coll.insert({a: i}));
}
assert.commandWorked(coll.createIndex({a: 1}));

cursor = coll.aggregate([{$match: {a: {$gte: 0}}}], {cursor: {batchSize: 0}, hint: {a: 1}});
cursorId = getLatestProfilerEntry(testDB, {"command.aggregate": coll.getName()}).cursorid;
assert.neq(0, cursorId);

cursor.next();  // Consume the result set.

profileObj = getLatestProfilerEntry(testDB, {op: "getmore"});

assert.eq(profileObj.ns, coll.getFullName(), profileObj);
assert.eq(profileObj.op, "getmore", profileObj);
assert.eq(profileObj.command.getMore, cursorId, profileObj);
assert.eq(profileObj.command.collection, coll.getName(), profileObj);
assert.eq(profileObj.originatingCommand.pipeline[0], {$match: {a: {$gte: 0}}}, profileObj);
assert.eq(profileObj.cursorid, cursorId, profileObj);
assert.eq(profileObj.nreturned, 20, profileObj);
assert.eq(profileObj.planSummary, "IXSCAN { a: 1 }", profileObj);
assert.eq(profileObj.cursorExhausted, true, profileObj);
assert.eq(profileObj.keysExamined, 20, profileObj);
assert.eq(profileObj.docsExamined, 20, profileObj);
assert.eq(profileObj.appName, "MongoDB Shell", profileObj);
assert.eq(profileObj.originatingCommand.hint, {a: 1}, profileObj);

//
// Confirm that originatingCommand is truncated in the profiler as { $truncated: <string>,
// comment: <string> }
//
let docToInsert = {};

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

coll.drop();
for (i = 0; i < 4; i++) {
    assert.commandWorked(coll.insert(docToInsert));
}

cursor = coll.find(docToInsert).comment("profile_getmore").batchSize(2);
assert.eq(cursor.itcount(), 4);  // Consume result set and trigger getMore.

profileObj = getLatestProfilerEntry(testDB, {op: "getmore"});
assert.eq((typeof profileObj.originatingCommand.$truncated), "string", profileObj);
assert.eq(profileObj.originatingCommand.comment, "profile_getmore", profileObj);
})();