summaryrefslogtreecommitdiff
path: root/jstests/replsets/apply_batches_totalMillis.js
blob: b8b2ee74f7c31b687500c4961dcc6244b789d88d (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
/**
 * serverStatus.metrics.repl.apply.batches.totalMillis is a cumulative measure of how much time a
 * node spends applying batches. This test checks that it includes the time spent waiting for
 * batches to finish, by comparing the time recorded after replicating a small and a large load.
 */

(function() {
    "use strict";

    // Gets the value of metrics.repl.apply.batches.totalMillis.
    function getTotalMillis(node) {
        return assert.commandWorked(node.adminCommand({serverStatus: 1}))
            .metrics.repl.apply.batches.totalMillis;
    }

    // Do a bulk insert of documents as: {{key: 0}, {key: 1}, {key: 2}, ... , {key: num-1}}
    function performBulkInsert(coll, key, num) {
        let bulk = coll.initializeUnorderedBulkOp();
        for (let i = 0; i < num; i++) {
            let doc = {};
            doc[key] = i;
            bulk.insert(doc);
        }
        assert.writeOK(bulk.execute());
        rst.awaitReplication();
    }

    let name = "apply_batches_totalMillis";
    let rst = new ReplSetTest({name: name, nodes: 2});
    rst.startSet();
    rst.initiate();

    let primary = rst.getPrimary();
    let secondary = rst.getSecondary();
    let coll = primary.getDB(name)["foo"];

    // Perform an initial write on the system and ensure steady state.
    assert.writeOK(coll.insert({init: 0}));
    rst.awaitReplication();
    let baseTime = getTotalMillis(secondary);

    // Introduce a small load and wait for it to be replicated.
    performBulkInsert(coll, "small", 1000);

    // Record the time spent applying the small load.
    let timeAfterSmall = getTotalMillis(secondary);
    let deltaSmall = timeAfterSmall - baseTime;

    // Insert a significantly larger load.
    performBulkInsert(coll, "large", 20000);

    // Record the time spent applying the large load.
    let timeAfterLarge = getTotalMillis(secondary);
    let deltaLarge = timeAfterLarge - timeAfterSmall;

    jsTestLog(`Recorded deltas: {small: ${deltaSmall}ms, large: ${deltaLarge}ms}.`);

    // We should have recorded at least as much time on the second load as we did on the first.
    // This is a crude comparison that is only taken to check that the timer is used correctly.
    assert(deltaLarge >= deltaSmall, "Expected a higher net totalMillis for the larger load.");
    rst.stopSet();

})();