summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/wt_operation_stats.js
blob: 073bed8619854ea17e6dff1f3804fa6096dc1894 (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
// This test inserts multiple records into a collection creating a btree spanning multiple pages,
// restarts the server and scans the collection. This would trigger application thread to read from
// the disk as the startup would only partially load the collection data into the cache. In doing so
// check that the WiredTiger storage statistics are present in the slowop log message and in the
// system.profile collection for the profiled read operation.
//
// @tags: [requires_profiling]

(function() {
'use strict';

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

const readStatRegx = /"storage":{"data":{"bytesRead":([0-9]+)/;

let checkLogStats = function() {
    // Check if the log output contains the expected statistics.
    let mongodLogs = rawMongoProgramOutput();
    let lines = mongodLogs.split('\n');
    let match;
    let logLineCount = 0;
    for (let line of lines) {
        if ((match = readStatRegx.exec(line)) !== null) {
            jsTestLog(line);
            logLineCount++;
        }
    }
    assert.gte(logLineCount, 1);
};

let checkSystemProfileStats = function(profileObj, statName) {
    // Check that the profiled operation contains the expected statistics.
    assert(profileObj.hasOwnProperty("storage"), tojson(profileObj));
    assert(profileObj.storage.hasOwnProperty("data"), tojson(profileObj));
    assert(profileObj.storage.data.hasOwnProperty(statName), tojson(profileObj));
};

// This test can only be run if the storageEngine is wiredTiger
if (jsTest.options().storageEngine && (jsTest.options().storageEngine !== "wiredTiger")) {
    jsTestLog("Skipping test because storageEngine is not wiredTiger");
} else {
    let name = "wt_op_stat";

    jsTestLog("run mongod");
    let conn = MongoRunner.runMongod();
    assert.neq(null, conn, "mongod was unable to start up");
    let testDB = conn.getDB(name);

    // Insert 200 documents of size 1K each, spanning multiple pages in the btree.
    let value = 'a'.repeat(1024);

    jsTestLog("insert data");
    for (let i = 0; i < 200; i++) {
        assert.commandWorked(testDB.foo.insert({x: value}));
    }

    let connport = conn.port;
    MongoRunner.stopMongod(conn);

    // Restart the server
    conn = MongoRunner.runMongod({
        restart: true,
        port: connport,
        slowms: "0",
    });

    clearRawMongoProgramOutput();

    // Scan the collection and check the bytes read statistic in the slowop log and
    // system.profile.
    testDB = conn.getDB(name);
    testDB.setProfilingLevel(2);
    jsTestLog("read data");
    let cur = testDB.foo.find();
    while (cur.hasNext()) {
        cur.next();
    }

    // Look for the storage statistics in the profiled output of the find command.
    let profileObj = getLatestProfilerEntry(testDB, {op: "query", ns: "wt_op_stat.foo"});
    checkSystemProfileStats(profileObj, "bytesRead");

    // Stopping the mongod waits until all of its logs have been read by the mongo shell.
    MongoRunner.stopMongod(conn);
    checkLogStats();

    jsTestLog("Success!");
}
})();