summaryrefslogtreecommitdiff
path: root/jstests/concurrency/fsm_workloads/explain_count.js
blob: f33673eba97011d1e44c594934f19454e66ebf2f (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
'use strict';

/**
 * explain_count.js
 *
 * Runs explain() and count() on a collection.
 */
load('jstests/concurrency/fsm_libs/extend_workload.js');           // for extendWorkload
load('jstests/concurrency/fsm_workloads/explain.js');              // for $config
load('jstests/concurrency/fsm_workload_helpers/server_types.js');  // for isMongos
load('jstests/libs/analyze_plan.js');                              // for planHasStage

var $config = extendWorkload($config, function($config, $super) {

    function assertNCounted(num, obj, db) {
        var stage = obj.executionStats.executionStages;
        // get sharded stage(s) if counting on mongos
        if (isMongos(db)) {
            stage = stage.shards[0].executionStages;
        }
        assertWhenOwnColl.eq(num, stage.nCounted);
    }

    $config.states = Object.extend({
        explainBasicCount: function explainBasicCount(db, collName) {
            var res = db[collName].explain().count();
            assertAlways.commandWorked(res);
            assertAlways(planHasStage(db, res.queryPlanner.winningPlan, 'COUNT'));
        },
        explainCountHint: function explainCountHint(db, collName) {
            assertWhenOwnColl(function() {
                var res = db[collName].explain().find({i: this.nInserted / 2}).hint({i: 1}).count();
                assertWhenOwnColl.commandWorked(res);
                assertWhenOwnColl(planHasStage(db, res.queryPlanner.winningPlan, 'COUNT'));
                assertWhenOwnColl(planHasStage(db, res.queryPlanner.winningPlan, 'COUNT_SCAN'));
            });
        },
        explainCountNoSkipLimit: function explainCountNoSkipLimit(db, collName) {
            var res = db[collName]
                          .explain('executionStats')
                          .find({i: this.nInserted})
                          .skip(1)
                          .count(false);
            assertAlways.commandWorked(res);
            assertNCounted(1, res, db);
        },
        explainCountSkipLimit: function explainCountSkipLimit(db, collName) {
            var res = db[collName]
                          .explain('executionStats')
                          .find({i: this.nInserted})
                          .skip(1)
                          .count(true);
            assertAlways.commandWorked(res);
            assertNCounted(0, res, db);
        }
    },
                                   $super.states);

    $config.transitions = Object.extend(
        {explain: $config.data.assignEqualProbsToTransitions($config.states)}, $super.transitions);

    return $config;
});