summaryrefslogtreecommitdiff
path: root/jstests/core/profile_update.js
blob: f4e04179a53fd6c3dbd2baed5e22f9b20a4be8c1 (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
// Confirms that profiled update execution contains all expected metrics with proper values.

(function() {
    "use strict";

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

    // Setup test db and collection.
    var testDB = db.getSiblingDB("profile_update");
    assert.commandWorked(testDB.dropDatabase());
    var coll = testDB.getCollection("test");

    testDB.setProfilingLevel(2);

    //
    // Confirm metrics for single document update.
    //
    var i;
    for (i = 0; i < 10; ++i) {
        assert.writeOK(coll.insert({a: i}));
    }
    assert.commandWorked(coll.createIndex({a: 1}));

    assert.writeOK(
        coll.update({a: {$gte: 2}},
                    {$set: {c: 1}, $inc: {a: -10}},
                    db.getMongo().writeMode() === "commands" ? {collation: {locale: "fr"}} : {}));

    var profileObj = getLatestProfilerEntry(testDB);

    assert.eq(profileObj.ns, coll.getFullName(), tojson(profileObj));
    assert.eq(profileObj.op, "update", tojson(profileObj));
    if (db.getMongo().writeMode() === "commands") {
        assert.eq(profileObj.collation, {locale: "fr"}, tojson(profileObj));
    }
    assert.eq(profileObj.keysExamined, 1, tojson(profileObj));
    assert.eq(profileObj.docsExamined, 1, tojson(profileObj));
    assert.eq(profileObj.keysInserted, 1, tojson(profileObj));
    assert.eq(profileObj.keysDeleted, 1, tojson(profileObj));
    assert.eq(profileObj.nMatched, 1, tojson(profileObj));
    assert.eq(profileObj.nModified, 1, tojson(profileObj));
    assert.eq(profileObj.planSummary, "IXSCAN { a: 1 }", tojson(profileObj));
    assert(profileObj.execStats.hasOwnProperty("stage"), tojson(profileObj));
    assert(profileObj.hasOwnProperty("millis"), tojson(profileObj));
    assert(profileObj.hasOwnProperty("numYield"), tojson(profileObj));
    assert(profileObj.hasOwnProperty("locks"), tojson(profileObj));
    assert.eq(profileObj.appName, "MongoDB Shell", tojson(profileObj));

    //
    // Confirm metrics for multiple indexed document update.
    //
    coll.drop();
    for (i = 0; i < 10; ++i) {
        assert.writeOK(coll.insert({a: i}));
    }
    assert.commandWorked(coll.createIndex({a: 1}));

    assert.writeOK(coll.update({a: {$gte: 5}}, {$set: {c: 1}, $inc: {a: -10}}, {multi: true}));
    profileObj = getLatestProfilerEntry(testDB);

    assert.eq(profileObj.keysExamined, 5, tojson(profileObj));
    assert.eq(profileObj.docsExamined, 5, tojson(profileObj));
    assert.eq(profileObj.keysInserted, 5, tojson(profileObj));
    assert.eq(profileObj.keysDeleted, 5, tojson(profileObj));
    assert.eq(profileObj.nMatched, 5, tojson(profileObj));
    assert.eq(profileObj.nModified, 5, tojson(profileObj));
    assert.eq(profileObj.planSummary, "IXSCAN { a: 1 }", tojson(profileObj));
    assert(profileObj.execStats.hasOwnProperty("stage"), tojson(profileObj));
    assert.eq(profileObj.appName, "MongoDB Shell", tojson(profileObj));

    //
    // Confirm metrics for insert on update with "upsert: true".
    //
    coll.drop();
    for (i = 0; i < 10; ++i) {
        assert.writeOK(coll.insert({a: i}));
    }
    assert.commandWorked(coll.createIndex({a: 1}));

    assert.writeOK(coll.update({_id: "new value", a: 4}, {$inc: {b: 1}}, {upsert: true}));
    profileObj = getLatestProfilerEntry(testDB);

    assert.eq(profileObj.query, {_id: "new value", a: 4}, tojson(profileObj));
    assert.eq(profileObj.updateobj, {$inc: {b: 1}}, tojson(profileObj));
    assert.eq(profileObj.keysExamined, 0, tojson(profileObj));
    assert.eq(profileObj.docsExamined, 0, tojson(profileObj));
    assert.eq(profileObj.keysInserted, 2, tojson(profileObj));
    assert.eq(profileObj.nMatched, 0, tojson(profileObj));
    assert.eq(profileObj.nModified, 0, tojson(profileObj));
    assert.eq(profileObj.upsert, true, tojson(profileObj));
    assert.eq(profileObj.planSummary, "IXSCAN { _id: 1 }", tojson(profileObj));
    assert(profileObj.execStats.hasOwnProperty("stage"), tojson(profileObj));
    assert.eq(profileObj.appName, "MongoDB Shell", tojson(profileObj));

    //
    // Confirm 'nmoved' for MMAPv1.
    //
    if (db.serverStatus().storageEngine.name === "mmapv1") {
        coll.drop();
        assert.writeOK(coll.insert({_id: 1}));

        assert.writeOK(coll.update({_id: 1}, {$set: {b: new Array(128).toString()}}));
        profileObj = getLatestProfilerEntry(testDB);

        assert.eq(profileObj.keysInserted, 1, tojson(profileObj));
        assert.eq(profileObj.keysDeleted, 1, tojson(profileObj));
        assert.eq(profileObj.nMatched, 1, tojson(profileObj));
        assert.eq(profileObj.nModified, 1, tojson(profileObj));
        assert.eq(profileObj.nmoved, 1, 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.writeOK(coll.update({a: 3, b: 3}, {$set: {c: 1}}));
    profileObj = getLatestProfilerEntry(testDB);

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