summaryrefslogtreecommitdiff
path: root/jstests/core/top.js
blob: 7fa0dbac39cf698aedba2dfacb22866cab5bba33 (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
/**
 * 1. check top numbers are correct
 */

var name = "toptest";

var testDB = db.getSiblingDB(name);
var testColl = testDB[name + "coll"];

//  Ensure an empty collection exists for first top command
testColl.drop();
testColl.insert({x:0});
testColl.remove({x:0});

// get top statistics for the test collection
function getTop() {
    return testDB.adminCommand("top").totals[testColl.getFullName()];
}

//  This variable is used to get differential output
var lastTop = getTop();

//  return the number of operations since the last call to diffTop for the specified key
function diffTop(key) {
    var thisTop = getTop();
    difference = { time  : thisTop[key].time - lastTop[key].time,
                   count : thisTop[key].count - lastTop[key].count }
    lastTop[key] = thisTop[key];

    assert.gte(difference.count, 0, "non-decreasing count");
    assert.gte(difference.time, 0, "non-decreasing time");

    //  Time should advance iff operations were performed
    assert.eq(difference.count != 0, difference.time > 0,"non-zero time iff non-zero count");
    return difference;
}

var numRecords = 100;

// check stats for specified key are as expected
var checked = { }
function checkStats(key, expected) {
    checked[key]++
    var actual = diffTop(key).count;
    assert.eq(actual, expected, "top reports wrong count for " + key);
}

//  Insert
for(i = 0; i < numRecords; i++) {
    testColl.insert({_id:i});
}
checkStats("insert", numRecords);
checkStats("writeLock", numRecords);

// Update
for(i = 0; i < numRecords; i++) {
    testColl.update({_id:i},{x:i});
}
checkStats("update", numRecords);

// Queries
var query = { }
for(i = 0; i < numRecords; i++) {
    query[i] = testColl.find({x : {$gte:i}}).batchSize(2);
    assert.eq(query[i].next()._id, i);
}
checkStats("queries" ,numRecords);

// Getmore
for(i = 0; i < numRecords / 2; i++) {
    assert.eq(query[i].next()._id, i + 1);
    assert.eq(query[i].next()._id, i + 2);
    assert.eq(query[i].next()._id, i + 3);
    assert.eq(query[i].next()._id, i + 4);
}
checkStats("getmore", numRecords);

// Remove
for(i = 0; i < numRecords; i++) {
    testColl.remove({_id : 1});
}
checkStats("remove", numRecords);

// Upsert, note that these are counted as updates, not inserts
for(i = 0; i < numRecords; i++) {
    testColl.update({_id:i},{x:i},{upsert:1});
}
checkStats("update", numRecords);


// Commands
diffTop("commands"); // ignore any commands before this
for(i = 0; i < numRecords; i++) {
    assert.eq(testDB.runCommand({count:"toptestcoll"}).n, numRecords);
}
checkStats("commands", numRecords);

for(key in lastTop) {
   if (!(key in checked)) {
      printjson({key:key, stats:diffTop(key)});
   }
}