summaryrefslogtreecommitdiff
path: root/jstests/noPassthroughWithMongod/bench_test_crud_commands.js
blob: e6db1e1bcb9ef8a2c736ed2e5311295a75365e0f (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
// Tests the "writeCmd" and "readCmd" options to benchRun().
(function() {
"use strict";

var coll = db.bench_test_crud_commands;
coll.drop();
assert.commandWorked(coll.getDB().createCollection(coll.getName()));

function makeDocument(docSize) {
    var doc = {"fieldName": ""};
    var longString = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    while (Object.bsonsize(doc) < docSize) {
        if (Object.bsonsize(doc) < docSize - longString.length) {
            doc.fieldName += longString;
        } else {
            doc.fieldName += "x";
        }
    }
    return doc;
}

function executeBenchRun(benchOps) {
    var benchArgs = {ops: benchOps, parallel: 2, seconds: 5, host: db.getMongo().host};
    if (jsTest.options().auth) {
        benchArgs['db'] = 'admin';
        benchArgs['username'] = jsTest.options().authUser;
        benchArgs['password'] = jsTest.options().authPassword;
    }
    return benchRun(benchArgs);
}

function testInsert(docs, writeCmd, wc) {
    coll.drop();

    var res = executeBenchRun(
        [{ns: coll.getFullName(), op: "insert", doc: docs, writeCmd: writeCmd, writeConcern: wc}]);

    assert.gt(coll.count(), 0);
    assert.eq(coll.findOne({}, {_id: 0}), docs[0]);
}

function testFind(readCmd) {
    coll.drop();
    for (var i = 0; i < 100; i++) {
        assert.writeOK(coll.insert({}));
    }

    var res = executeBenchRun([
        {ns: coll.getFullName(), op: "find", query: {}, batchSize: NumberInt(10), readCmd: readCmd}
    ]);
    assert.gt(res.query, 0, tojson(res));
}

function testFindOne(readCmd) {
    coll.drop();
    for (var i = 0; i < 100; i++) {
        assert.writeOK(coll.insert({}));
    }

    var res =
        executeBenchRun([{ns: coll.getFullName(), op: "findOne", query: {}, readCmd: readCmd}]);
    assert.gt(res.findOne, 0, tojson(res));
}

function testWriteConcern(writeCmd) {
    var bigDoc = makeDocument(260 * 1024);
    var docs = [];
    for (var i = 0; i < 100; i++) {
        docs.push({x: 1});
    }

    testInsert([bigDoc], writeCmd, {});
    testInsert(docs, writeCmd, {});
    testInsert(docs, writeCmd, {"w": "majority"});
    testInsert(docs, writeCmd, {"w": 1, "j": false});

    var storageEnginesWithoutJournaling = new Set(["ephemeralForTest", "inMemory"]);
    var runningWithoutJournaling = TestData.noJournal ||
        storageEnginesWithoutJournaling.has(db.serverStatus().storageEngine.name);
    if (!runningWithoutJournaling) {
        // Only test journaled writes if the server actually supports them.
        testInsert(docs, writeCmd, {"j": true});
    }
}

testWriteConcern(false);
testWriteConcern(true);

testFind(false);
testFind(true);
testFindOne(false);
testFindOne(true);
})();