summaryrefslogtreecommitdiff
path: root/jstests/core/profile_getmore.js
blob: e6fc3edb66f59df89ac3f4369ca164dc990f9b61 (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
// Confirms that profiled getMore execution contains all expected metrics with proper values.
// TODO SERVER-23259: Add planSummary.
// TODO SERVER-23264: Add execStats.

(function() {
    "use strict";

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

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

    testDB.setProfilingLevel(2);

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

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

    var cursorId = getLatestProfilerEntry(testDB).cursorid;  // Save cursorid from find.

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

    var profileObj = getLatestProfilerEntry(testDB);

    assert.eq(profileObj.ns, coll.getFullName(), tojson(profileObj));
    assert.eq(profileObj.op, "getmore", tojson(profileObj));
    assert.eq(profileObj.keysExamined, 2, tojson(profileObj));
    assert.eq(profileObj.docsExamined, 2, tojson(profileObj));
    assert.eq(profileObj.cursorid, cursorId, tojson(profileObj));
    assert.eq(profileObj.nreturned, 2, tojson(profileObj));
    assert.eq(profileObj.query.batchSize, 2, tojson(profileObj));
    assert(profileObj.hasOwnProperty("responseLength"), 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(profileObj.hasOwnProperty("millis"), tojson(profileObj));
    assert(!profileObj.hasOwnProperty("cursorExhausted"), tojson(profileObj));

    //
    // Confirm hasSortStage on getMore with a not-exhausted cursor and in-memory sort.
    //
    coll.drop();
    for (i = 0; i < 10; ++i) {
        assert.writeOK(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);

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

    //
    // Confirm "cursorExhausted" metric.
    //
    coll.drop();
    for (i = 0; i < 3; ++i) {
        assert.writeOK(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);

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