ExplainFunctionalTest.php 14.8 KB
Newer Older
1 2 3 4
<?php

namespace MongoDB\Tests\Operation;

5
use MongoDB\Driver\BulkWrite;
6
use MongoDB\Collection;
7
use MongoDB\Operation\Count;
8
use MongoDB\Operation\CreateCollection;
9
use MongoDB\Operation\Distinct;
10
use MongoDB\Operation\Delete;
11 12
use MongoDB\Operation\DeleteMany;
use MongoDB\Operation\DeleteOne;
13
use MongoDB\Operation\Explain;
14
use MongoDB\Operation\Find;
15
use MongoDB\Operation\FindAndModify;
16
use MongoDB\Operation\FindOne;
17 18 19
use MongoDB\Operation\FindOneAndDelete;
use MongoDB\Operation\FindOneAndReplace;
use MongoDB\Operation\FindOneAndUpdate;
20
use MongoDB\Operation\Update;
21 22
use MongoDB\Operation\UpdateMany;
use MongoDB\Operation\UpdateOne;
23 24
use MongoDB\Tests\CommandObserver;
use stdClass;
25 26 27

class ExplainFunctionalTest extends FunctionalTestCase
{
28 29 30 31
    /**
     * @dataProvider provideVerbosityInformation
     */
    public function testCount($verbosity, $executionStatsExpected, $allPlansExecutionExpected)
32
    {
33
        $this->createFixtures(3);
34 35

        $operation = new Count($this->getDatabaseName(), $this->getCollectionName(), ['x' => ['$gte' => 1]], []);
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

        $explainOperation = new Explain($this->getDatabaseName(), $operation, ['verbosity' => $verbosity, 'typeMap' => ['root' => 'array', 'document' => 'array']]);
        $result = $explainOperation->execute($this->getPrimaryServer());

        $this->assertExplainResult($result, $executionStatsExpected, $allPlansExecutionExpected);
    }

    /**
     * @dataProvider provideVerbosityInformation
     */
    public function testDelete($verbosity, $executionStatsExpected, $allPlansExecutionExpected)
    {
        $this->createFixtures(3);

        $filter = ['_id' => 1];

        $operation = new Delete($this->getDatabaseName(), $this->getCollectionName(), $filter, 1);

54
        $explainOperation = new Explain($this->getDatabaseName(), $operation, ['verbosity' => $verbosity, 'typeMap' => ['root' => 'array', 'document' => 'array']]);
55 56
        $result = $explainOperation->execute($this->getPrimaryServer());

57
        $this->assertExplainResult($result, $executionStatsExpected, $allPlansExecutionExpected);
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
    /**
     * @dataProvider provideVerbosityInformation
     */
    public function testDeleteMany($verbosity, $executionStatsExpected, $allPlansExecutionExpected)
    {
        $this->createFixtures(3);

        $filter = ['_id' => ['$gt' => 1]];

        $operation = new DeleteMany($this->getDatabaseName(), $this->getCollectionName(), $filter);

        $explainOperation = new Explain($this->getDatabaseName(), $operation, ['verbosity' => $verbosity, 'typeMap' => ['root' => 'array', 'document' => 'array']]);
        $result = $explainOperation->execute($this->getPrimaryServer());

        $this->assertExplainResult($result, $executionStatsExpected, $allPlansExecutionExpected);
    }

    /**
     * @dataProvider provideVerbosityInformation
     */
    public function testDeleteOne($verbosity, $executionStatsExpected, $allPlansExecutionExpected)
    {
        $this->createFixtures(3);

        $filter = ['_id' => 1];

        $operation = new DeleteOne($this->getDatabaseName(), $this->getCollectionName(), $filter);

        $explainOperation = new Explain($this->getDatabaseName(), $operation, ['verbosity' => $verbosity, 'typeMap' => ['root' => 'array', 'document' => 'array']]);
        $result = $explainOperation->execute($this->getPrimaryServer());

        $this->assertExplainResult($result, $executionStatsExpected, $allPlansExecutionExpected);
    }

94 95 96 97
    /**
     * @dataProvider provideVerbosityInformation
     */
    public function testDistinct($verbosity, $executionStatsExpected, $allPlansExecutionExpected)
98 99
    {
        if (version_compare($this->getServerVersion(), '3.2.0', '<')) {
100
            $this->markTestSkipped('Explaining distinct command requires server version >= 3.2');
101 102 103 104
        }

        $operation = new Distinct($this->getDatabaseName(), $this->getCollectionName(), 'x', []);

105
        $explainOperation = new Explain($this->getDatabaseName(), $operation, ['verbosity' => $verbosity, 'typeMap' => ['root' => 'array', 'document' => 'array']]);
106 107
        $result = $explainOperation->execute($this->getPrimaryServer());

108
        $this->assertExplainResult($result, $executionStatsExpected, $allPlansExecutionExpected);
109 110
    }

111 112 113 114
    /**
     * @dataProvider provideVerbosityInformation
     */
    public function testFindAndModify($verbosity, $executionStatsExpected, $allPlansExecutionExpected)
115 116
    {
        if (version_compare($this->getServerVersion(), '3.2.0', '<')) {
117
            $this->markTestSkipped('Explaining findAndModify command requires server version >= 3.2');
118 119 120 121
        }

        $operation = new FindAndModify($this->getDatabaseName(), $this->getCollectionName(), ['remove' => true]);

122
        $explainOperation = new Explain($this->getDatabaseName(), $operation, ['verbosity' => $verbosity, 'typeMap' => ['root' => 'array', 'document' => 'array']]);
123 124
        $result = $explainOperation->execute($this->getPrimaryServer());

125
        $this->assertExplainResult($result, $executionStatsExpected, $allPlansExecutionExpected);
126
    }
127

128 129 130 131
    /**
     * @dataProvider provideVerbosityInformation
     */
    public function testFind($verbosity, $executionStatsExpected, $allPlansExecutionExpected)
132 133 134
    {
        $this->createFixtures(3);

135
        $operation = new Find($this->getDatabaseName(), $this->getCollectionName(), []);
136

137
        $explainOperation = new Explain($this->getDatabaseName(), $operation, ['verbosity' => $verbosity, 'typeMap' => ['root' => 'array', 'document' => 'array']]);
138 139
        $result = $explainOperation->execute($this->getPrimaryServer());

140
        $this->assertExplainResult($result, $executionStatsExpected, $allPlansExecutionExpected);
141 142
    }

143
    public function testFindMaxAwait()
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
    {
        if (version_compare($this->getServerVersion(), '3.2.0', '<')) {
            $this->markTestSkipped('maxAwaitTimeMS option is not supported');
        }

        $maxAwaitTimeMS = 100;

        /* Calculate an approximate pivot to use for time assertions. We will
         * assert that the duration of blocking responses is greater than this
         * value, and vice versa. */
        $pivot = ($maxAwaitTimeMS * 0.001) * 0.9;

        // Create a capped collection.
        $databaseName = $this->getDatabaseName();
        $cappedCollectionName = $this->getCollectionName();
        $cappedCollectionOptions = [
            'capped' => true,
            'max' => 100,
            'size' => 1048576,
        ];

        $operation = new CreateCollection($databaseName, $cappedCollectionName, $cappedCollectionOptions);
        $operation->execute($this->getPrimaryServer());

168
        $this->createFixtures(2);
169 170 171

        $operation = new Find($databaseName, $cappedCollectionName, [], ['cursorType' => Find::TAILABLE_AWAIT, 'maxAwaitTimeMS' => $maxAwaitTimeMS]);

172 173 174 175 176
        (new CommandObserver)->observe(
            function() use ($operation) {
                $explainOperation = new Explain($this->getDatabaseName(), $operation, ['typeMap' => ['root' => 'array', 'document' => 'array']]);
                $explainOperation->execute($this->getPrimaryServer());
            },
177 178
            function(array $event) {
                $command = $event['started']->getCommand();
179 180 181 182 183 184
                $this->assertObjectNotHasAttribute('maxAwaitTimeMS', $command->explain);
                $this->assertObjectHasAttribute('tailable', $command->explain);
                $this->assertObjectHasAttribute('awaitData', $command->explain);
            }
        );
    }
185

186 187 188 189 190 191 192 193
    public function testFindModifiers()
    {
        $this->createFixtures(3);

        $operation = new Find(
            $this->getDatabaseName(),
            $this->getCollectionName(),
            [],
194
            ['modifiers' => ['$orderby' => ['_id' => 1]]]
195 196 197 198 199 200 201
        );

        (new CommandObserver)->observe(
            function() use ($operation) {
                $explainOperation = new Explain($this->getDatabaseName(), $operation, ['typeMap' => ['root' => 'array', 'document' => 'array']]);
                $explainOperation->execute($this->getPrimaryServer());
            },
202 203
            function(array $event) {
                $command = $event['started']->getCommand();
204
                $this->assertObjectHasAttribute('sort', $command->explain);
205 206 207
                $this->assertObjectNotHasAttribute('modifiers', $command->explain);
            }
        );
208 209
    }

210

211 212 213 214
    /**
     * @dataProvider provideVerbosityInformation
     */
    public function testFindOne($verbosity, $executionStatsExpected, $allPlansExecutionExpected)
215 216 217 218 219
    {
        $this->createFixtures(1);

        $operation = new FindOne($this->getDatabaseName(), $this->getCollectionName(), []);

220
        $explainOperation = new Explain($this->getDatabaseName(), $operation, ['verbosity' => $verbosity, 'typeMap' => ['root' => 'array', 'document' => 'array']]);
221 222
        $result = $explainOperation->execute($this->getPrimaryServer());

223
        $this->assertExplainResult($result, $executionStatsExpected, $allPlansExecutionExpected);
224 225
    }

226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
    /**
     * @dataProvider provideVerbosityInformation
     */
    public function testFindOneAndDelete($verbosity, $executionStatsExpected, $allPlansExecutionExpected)
    {
        if (version_compare($this->getServerVersion(), '3.2.0', '<')) {
            $this->markTestSkipped('Explaining findOneAndDelete command requires server version >= 3.2');
        }

        $operation = new FindOneAndDelete($this->getDatabaseName(), $this->getCollectionName(), []);

        $explainOperation = new Explain($this->getDatabaseName(), $operation, ['verbosity' => $verbosity, 'typeMap' => ['root' => 'array', 'document' => 'array']]);
        $result = $explainOperation->execute($this->getPrimaryServer());

        $this->assertExplainResult($result, $executionStatsExpected, $allPlansExecutionExpected);
    }

    /**
     * @dataProvider provideVerbosityInformation
     */
    public function testFindOneAndReplace($verbosity, $executionStatsExpected, $allPlansExecutionExpected)
    {
        if (version_compare($this->getServerVersion(), '3.2.0', '<')) {
            $this->markTestSkipped('Explaining findOneAndReplace command requires server version >= 3.2');
        }

        $operation = new FindOneAndReplace($this->getDatabaseName(), $this->getCollectionName(), ['x' => 1.1], ['x' => 5]);

        $explainOperation = new Explain($this->getDatabaseName(), $operation, ['verbosity' => $verbosity, 'typeMap' => ['root' => 'array', 'document' => 'array']]);
        $result = $explainOperation->execute($this->getPrimaryServer());

        $this->assertExplainResult($result, $executionStatsExpected, $allPlansExecutionExpected);
    }

    /**
     * @dataProvider provideVerbosityInformation
     */
    public function testFindOneAndUpdate($verbosity, $executionStatsExpected, $allPlansExecutionExpected)
    {
        if (version_compare($this->getServerVersion(), '3.2.0', '<')) {
            $this->markTestSkipped('Explaining findOneAndUpdate command requires server version >= 3.2');
        }

        $operation = new FindOneAndUpdate($this->getDatabaseName(), $this->getCollectionName(), [], ['$rename' => ['x' => 'y']]);

        $explainOperation = new Explain($this->getDatabaseName(), $operation, ['verbosity' => $verbosity, 'typeMap' => ['root' => 'array', 'document' => 'array']]);
        $result = $explainOperation->execute($this->getPrimaryServer());

        $this->assertExplainResult($result, $executionStatsExpected, $allPlansExecutionExpected);
    }

277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
    /**
     * @dataProvider provideVerbosityInformation
     */
    public function testUpdate($verbosity, $executionStatsExpected, $allPlansExecutionExpected)
    {
        $this->createFixtures(3);

        $filter = ['_id' => ['$gt' => 1]];
        $update = ['$inc' => ['x' => 1]];

        $operation = new Update($this->getDatabaseName(), $this->getCollectionName(), $filter, $update);

        $explainOperation = new Explain($this->getDatabaseName(), $operation, ['verbosity' => $verbosity, 'typeMap' => ['root' => 'array', 'document' => 'array']]);
        $result = $explainOperation->execute($this->getPrimaryServer());

        $this->assertExplainResult($result, $executionStatsExpected, $allPlansExecutionExpected);
    }

295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
    /**
     * @dataProvider provideVerbosityInformation
     */
    public function testUpdateMany($verbosity, $executionStatsExpected, $allPlansExecutionExpected)
    {
        $this->createFixtures(3);

        $filter = ['_id' => ['$gt' => 1]];
        $update = ['$inc' => ['x' => 1]];

        $operation = new UpdateMany($this->getDatabaseName(), $this->getCollectionName(), $filter, $update);

        $explainOperation = new Explain($this->getDatabaseName(), $operation, ['verbosity' => $verbosity, 'typeMap' => ['root' => 'array', 'document' => 'array']]);
        $result = $explainOperation->execute($this->getPrimaryServer());

        $this->assertExplainResult($result, $executionStatsExpected, $allPlansExecutionExpected);
    }

    /**
     * @dataProvider provideVerbosityInformation
     */
    public function testUpdateOne($verbosity, $executionStatsExpected, $allPlansExecutionExpected)
    {
        $this->createFixtures(3);

        $filter = ['_id' => ['$lte' => 1]];
        $update = ['$inc' => ['x' => 1]];

        $operation = new UpdateOne($this->getDatabaseName(), $this->getCollectionName(), $filter, $update);

        $explainOperation = new Explain($this->getDatabaseName(), $operation, ['verbosity' => $verbosity, 'typeMap' => ['root' => 'array', 'document' => 'array']]);
        $result = $explainOperation->execute($this->getPrimaryServer());

        $this->assertExplainResult($result, $executionStatsExpected, $allPlansExecutionExpected);
    }

331
    public function provideVerbosityInformation()
332
    {
333 334 335
        return [
            [Explain::VERBOSITY_ALL_PLANS, true, true],
            [Explain::VERBOSITY_EXEC_STATS, true, false],
336
            [Explain::VERBOSITY_QUERY, false, false],
337
        ];
338 339
    }

340
    private function assertExplainResult($result, $executionStatsExpected, $allPlansExecutionExpected)
341
    {
342 343 344 345 346 347 348 349 350 351 352
        $this->assertArrayHasKey('queryPlanner', $result);
        if ($executionStatsExpected) {
            $this->assertArrayHasKey('executionStats', $result);
            if ($allPlansExecutionExpected) {
                $this->assertArrayHasKey('allPlansExecution', $result['executionStats']);
            } else {
                $this->assertArrayNotHasKey('allPlansExecution', $result['executionStats']);
            }
        } else {
            $this->assertArrayNotHasKey('executionStats', $result);
        }
353 354 355 356 357 358 359 360 361 362 363 364 365 366
    }

    /**
     * Create data fixtures.
     *
     * @param integer $n
     */
    private function createFixtures($n)
    {
        $bulkWrite = new BulkWrite(['ordered' => true]);

        for ($i = 1; $i <= $n; $i++) {
            $bulkWrite->insert([
                '_id' => $i,
367
                'x' => (integer) ($i . $i),
368 369 370 371 372 373 374
            ]);
        }

        $result = $this->manager->executeBulkWrite($this->getNamespace(), $bulkWrite);

        $this->assertEquals($n, $result->getInsertedCount());
    }
375
}