summaryrefslogtreecommitdiff
path: root/jstests/core/profile_insert.js
blob: 961e13b1cad15d28a6533d20710e65ed54af1653 (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
// Confirms that profiled insert execution contains all expected metrics with proper values.
//
// @tags: [
//   assumes_write_concern_unchanged,
//   does_not_support_stepdowns,
//   requires_profiling,
// ]

(function() {
"use strict";

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

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

testDB.setProfilingLevel(2);

//
// Test single insert.
//
var doc = {_id: 1};
assert.commandWorked(coll.insert(doc));

var profileObj = getLatestProfilerEntry(testDB);

const collectionIsClustered = ClusteredCollectionUtil.areAllCollectionsClustered(db.getMongo());
// A clustered collection has no actual index on _id.
let expectedKeysInserted = collectionIsClustered ? 0 : 1;

assert.eq(profileObj.ns, coll.getFullName(), tojson(profileObj));
assert.eq(profileObj.op, "insert", tojson(profileObj));
assert.eq(profileObj.ninserted, 1, tojson(profileObj));
assert.eq(profileObj.keysInserted, expectedKeysInserted, tojson(profileObj));
assert.eq(profileObj.command.ordered, true, tojson(profileObj));
assert.eq(profileObj.protocol, "op_msg", tojson(profileObj));
assert(profileObj.hasOwnProperty("responseLength"), tojson(profileObj));

assert(profileObj.hasOwnProperty("numYield"), tojson(profileObj));
assert(profileObj.hasOwnProperty("locks"), tojson(profileObj));
assert(profileObj.hasOwnProperty("millis"), tojson(profileObj));
assert(profileObj.hasOwnProperty("ts"), tojson(profileObj));
assert(profileObj.hasOwnProperty("client"), tojson(profileObj));
assert.eq(profileObj.appName, "MongoDB Shell", tojson(profileObj));

//
// Test multi-insert.
//
coll.drop();

var docArray = [{_id: 1}, {_id: 2}];
var bulk = coll.initializeUnorderedBulkOp();
bulk.insert(docArray[0]);
bulk.insert(docArray[1]);
assert.commandWorked(bulk.execute());

profileObj = getLatestProfilerEntry(testDB);

expectedKeysInserted = collectionIsClustered ? 0 : 2;

assert.eq(profileObj.ninserted, 2, tojson(profileObj));
assert.eq(profileObj.keysInserted, expectedKeysInserted, tojson(profileObj));
assert.eq(profileObj.appName, "MongoDB Shell", tojson(profileObj));

//
// Test insert options.
//
coll.drop();
doc = {
    _id: 1
};
var wtimeout = 60000;
assert.commandWorked(coll.insert(doc, {writeConcern: {w: 1, wtimeout: wtimeout}, ordered: false}));

profileObj = getLatestProfilerEntry(testDB);

assert.eq(profileObj.command.ordered, false, tojson(profileObj));
assert.eq(profileObj.command.writeConcern.w, 1, tojson(profileObj));
assert.eq(profileObj.command.writeConcern.wtimeout, wtimeout, tojson(profileObj));
assert.eq(profileObj.appName, "MongoDB Shell", tojson(profileObj));
})();