HydrationPerformanceTest.php 14.6 KB
Newer Older
1 2 3 4 5 6
<?php

namespace Doctrine\Tests\ORM\Performance;

require_once __DIR__ . '/../../TestInit.php';

7 8 9
use Doctrine\Tests\Mocks\HydratorMockStatement,
    Doctrine\ORM\Query\ResultSetMapping,
    Doctrine\ORM\Query;
10 11 12 13 14 15 16 17 18 19 20

/**
 * Tests to prevent serious performance regressions.
 *
 * IMPORTANT: Be sure to run these tests withoug xdebug or similar tools that
 * seriously degrade performance.
 *
 * @author robo
 */
class HydrationPerformanceTest extends \Doctrine\Tests\OrmPerformanceTestCase
{
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
    /**
     * Times for comparison:
     *
     * [romanb: 10000 rows => 0.7 seconds]
     *
     * MAXIMUM TIME: 1 second
     */
    public function testSimpleQueryScalarHydrationPerformance10000Rows()
    {
        $rsm = new ResultSetMapping;
        $rsm->addEntityResult('Doctrine\Tests\Models\CMS\CmsUser', 'u');
        $rsm->addFieldResult('u', 'u__id', 'id');
        $rsm->addFieldResult('u', 'u__status', 'status');
        $rsm->addFieldResult('u', 'u__username', 'username');
        $rsm->addFieldResult('u', 'u__name', 'name');

        // Faked result set
        $resultSet = array(
            //row1
            array(
                'u__id' => '1',
                'u__status' => 'developer',
                'u__username' => 'romanb',
                'u__name' => 'Roman',
            ),
            array(
                'u__id' => '1',
                'u__status' => 'developer',
                'u__username' => 'romanb',
                'u__name' => 'Roman',
            ),
            array(
                'u__id' => '2',
                'u__status' => 'developer',
                'u__username' => 'romanb',
                'u__name' => 'Roman',
            )
        );

        for ($i = 4; $i < 10000; ++$i) {
            $resultSet[] = array(
                'u__id' => $i,
                'u__status' => 'developer',
                'u__username' => 'jwage',
                'u__name' => 'Jonathan',
            );
        }

        $stmt = new HydratorMockStatement($resultSet);
        $hydrator = new \Doctrine\ORM\Internal\Hydration\ScalarHydrator($this->_em);

        $this->setMaxRunningTime(1);
        $s = microtime(true);
        $result = $hydrator->hydrateAll($stmt, $rsm);
        $e = microtime(true);
        echo __FUNCTION__ . " - " . ($e - $s) . " seconds" . PHP_EOL;
    }
    
79 80 81
    /**
     * Times for comparison:
     *
romanb's avatar
romanb committed
82
     * [romanb: 10000 rows => 1 second]
83
     *
romanb's avatar
romanb committed
84
     * MAXIMUM TIME: 2 seconds
85
     */
86
    public function testSimpleQueryArrayHydrationPerformance10000Rows()
87 88
    {
        $rsm = new ResultSetMapping;
89
        $rsm->addEntityResult('Doctrine\Tests\Models\CMS\CmsUser', 'u');
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
        $rsm->addFieldResult('u', 'u__id', 'id');
        $rsm->addFieldResult('u', 'u__status', 'status');
        $rsm->addFieldResult('u', 'u__username', 'username');
        $rsm->addFieldResult('u', 'u__name', 'name');

        // Faked result set
        $resultSet = array(
            //row1
            array(
                'u__id' => '1',
                'u__status' => 'developer',
                'u__username' => 'romanb',
                'u__name' => 'Roman',
            ),
            array(
                'u__id' => '1',
                'u__status' => 'developer',
                'u__username' => 'romanb',
                'u__name' => 'Roman',
            ),
            array(
                'u__id' => '2',
                'u__status' => 'developer',
                'u__username' => 'romanb',
                'u__name' => 'Roman',
            )
        );

        for ($i = 4; $i < 10000; ++$i) {
            $resultSet[] = array(
                'u__id' => $i,
                'u__status' => 'developer',
                'u__username' => 'jwage',
                'u__name' => 'Jonathan',
            );
        }

        $stmt = new HydratorMockStatement($resultSet);
        $hydrator = new \Doctrine\ORM\Internal\Hydration\ArrayHydrator($this->_em);

romanb's avatar
romanb committed
130
        $this->setMaxRunningTime(2);
131
        $s = microtime(true);
132
        $result = $hydrator->hydrateAll($stmt, $rsm);
133 134
        $e = microtime(true);
        echo __FUNCTION__ . " - " . ($e - $s) . " seconds" . PHP_EOL;
135 136 137 138 139
    }

    /**
     * Times for comparison:
     *
romanb's avatar
romanb committed
140
     * [romanb: 10000 rows => 1.4 seconds]
141
     *
romanb's avatar
romanb committed
142
     * MAXIMUM TIME: 3 seconds
143
     */
144
    public function testMixedQueryFetchJoinArrayHydrationPerformance10000Rows()
145 146
    {
        $rsm = new ResultSetMapping;
147
        $rsm->addEntityResult('Doctrine\Tests\Models\CMS\CmsUser', 'u');
148
        $rsm->addJoinedEntityResult(
149
                'Doctrine\Tests\Models\CMS\CmsPhonenumber',
150 151
                'p',
                'u',
152
                'phonenumbers'
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
        );
        $rsm->addFieldResult('u', 'u__id', 'id');
        $rsm->addFieldResult('u', 'u__status', 'status');
        $rsm->addFieldResult('u', 'u__username', 'username');
        $rsm->addFieldResult('u', 'u__name', 'name');
        $rsm->addScalarResult('sclr0', 'nameUpper');
        $rsm->addFieldResult('p', 'p__phonenumber', 'phonenumber');

        // Faked result set
        $resultSet = array(
            //row1
            array(
                'u__id' => '1',
                'u__status' => 'developer',
                'u__username' => 'romanb',
                'u__name' => 'Roman',
                'sclr0' => 'ROMANB',
                'p__phonenumber' => '42',
            ),
            array(
                'u__id' => '1',
                'u__status' => 'developer',
                'u__username' => 'romanb',
                'u__name' => 'Roman',
                'sclr0' => 'ROMANB',
                'p__phonenumber' => '43',
            ),
            array(
                'u__id' => '2',
                'u__status' => 'developer',
                'u__username' => 'romanb',
                'u__name' => 'Roman',
                'sclr0' => 'JWAGE',
                'p__phonenumber' => '91'
            )
        );

        for ($i = 4; $i < 10000; ++$i) {
            $resultSet[] = array(
                'u__id' => $i,
                'u__status' => 'developer',
                'u__username' => 'jwage',
                'u__name' => 'Jonathan',
                'sclr0' => 'JWAGE' . $i,
                'p__phonenumber' => '91'
            );
        }

        $stmt = new HydratorMockStatement($resultSet);
        $hydrator = new \Doctrine\ORM\Internal\Hydration\ArrayHydrator($this->_em);

romanb's avatar
romanb committed
204
        $this->setMaxRunningTime(3);
205
        $s = microtime(true);
206
        $result = $hydrator->hydrateAll($stmt, $rsm);
207 208
        $e = microtime(true);
        echo __FUNCTION__ . " - " . ($e - $s) . " seconds" . PHP_EOL;
209 210 211
    }

    /**
romanb's avatar
romanb committed
212
     * [romanb: 10000 rows => 1.5 seconds]
213
     *
romanb's avatar
romanb committed
214
     * MAXIMUM TIME: 3 seconds
215
     */
216
    public function testSimpleQueryPartialObjectHydrationPerformance10000Rows()
217 218
    {
        $rsm = new ResultSetMapping;
219
        $rsm->addEntityResult('Doctrine\Tests\Models\CMS\CmsUser', 'u');
220 221 222 223 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
        $rsm->addFieldResult('u', 'u__id', 'id');
        $rsm->addFieldResult('u', 'u__status', 'status');
        $rsm->addFieldResult('u', 'u__username', 'username');
        $rsm->addFieldResult('u', 'u__name', 'name');

        // Faked result set
        $resultSet = array(
            //row1
            array(
                'u__id' => '1',
                'u__status' => 'developer',
                'u__username' => 'romanb',
                'u__name' => 'Roman',
            ),
            array(
                'u__id' => '1',
                'u__status' => 'developer',
                'u__username' => 'romanb',
                'u__name' => 'Roman',
            ),
            array(
                'u__id' => '2',
                'u__status' => 'developer',
                'u__username' => 'romanb',
                'u__name' => 'Roman',
            )
        );

        for ($i = 4; $i < 10000; ++$i) {
            $resultSet[] = array(
                'u__id' => $i,
                'u__status' => 'developer',
                'u__username' => 'jwage',
                'u__name' => 'Jonathan',
            );
        }

        $stmt = new HydratorMockStatement($resultSet);
        $hydrator = new \Doctrine\ORM\Internal\Hydration\ObjectHydrator($this->_em);

romanb's avatar
romanb committed
260
        $this->setMaxRunningTime(3);
261
        $s = microtime(true);
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
        $result = $hydrator->hydrateAll($stmt, $rsm, array(Query::HINT_FORCE_PARTIAL_LOAD => true));
        $e = microtime(true);
        echo __FUNCTION__ . " - " . ($e - $s) . " seconds" . PHP_EOL;
    }
    
    /**
     * [romanb: 10000 rows => 3 seconds]
     *
     * MAXIMUM TIME: 4.5 seconds
     */
    public function testSimpleQueryFullObjectHydrationPerformance10000Rows()
    {
        $rsm = new ResultSetMapping;
        $rsm->addEntityResult('Doctrine\Tests\Models\CMS\CmsUser', 'u');
        $rsm->addFieldResult('u', 'u__id', 'id');
        $rsm->addFieldResult('u', 'u__status', 'status');
        $rsm->addFieldResult('u', 'u__username', 'username');
        $rsm->addFieldResult('u', 'u__name', 'name');
280 281 282 283 284 285 286 287 288 289
        $rsm->addJoinedEntityResult(
                'Doctrine\Tests\Models\CMS\CmsAddress',
                'a',
                'u',
                'address'
        );
        $rsm->addFieldResult('a', 'a__id', 'id');
        //$rsm->addFieldResult('a', 'a__country', 'country');
        //$rsm->addFieldResult('a', 'a__zip', 'zip');
        //$rsm->addFieldResult('a', 'a__city', 'city');
290 291 292 293 294 295 296 297 298

        // Faked result set
        $resultSet = array(
            //row1
            array(
                'u__id' => '1',
                'u__status' => 'developer',
                'u__username' => 'romanb',
                'u__name' => 'Roman',
299
                'a__id' => '1'
300 301 302
            )
        );

303
        for ($i = 2; $i < 10000; ++$i) {
304 305 306 307 308
            $resultSet[] = array(
                'u__id' => $i,
                'u__status' => 'developer',
                'u__username' => 'jwage',
                'u__name' => 'Jonathan',
309
                'a__id' => $i
310 311 312 313 314 315
            );
        }

        $stmt = new HydratorMockStatement($resultSet);
        $hydrator = new \Doctrine\ORM\Internal\Hydration\ObjectHydrator($this->_em);

316
        $this->setMaxRunningTime(5);
317
        $s = microtime(true);
318
        $result = $hydrator->hydrateAll($stmt, $rsm);
319 320
        $e = microtime(true);
        echo __FUNCTION__ . " - " . ($e - $s) . " seconds" . PHP_EOL;
321 322 323
    }

    /**
324
     * [romanb: 2000 rows => 0.4 seconds]
325
     *
326
     * MAXIMUM TIME: 1 second
327
     */
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
    public function testMixedQueryFetchJoinPartialObjectHydrationPerformance2000Rows()
    {
        $rsm = new ResultSetMapping;
        $rsm->addEntityResult('Doctrine\Tests\Models\CMS\CmsUser', 'u');
        $rsm->addJoinedEntityResult(
                'Doctrine\Tests\Models\CMS\CmsPhonenumber',
                'p',
                'u',
                'phonenumbers'
        );
        $rsm->addFieldResult('u', 'u__id', 'id');
        $rsm->addFieldResult('u', 'u__status', 'status');
        $rsm->addFieldResult('u', 'u__username', 'username');
        $rsm->addFieldResult('u', 'u__name', 'name');
        $rsm->addScalarResult('sclr0', 'nameUpper');
        $rsm->addFieldResult('p', 'p__phonenumber', 'phonenumber');

        // Faked result set
        $resultSet = array(
            //row1
            array(
                'u__id' => '1',
                'u__status' => 'developer',
                'u__username' => 'romanb',
                'u__name' => 'Roman',
                'sclr0' => 'ROMANB',
                'p__phonenumber' => '42',
            ),
            array(
                'u__id' => '1',
                'u__status' => 'developer',
                'u__username' => 'romanb',
                'u__name' => 'Roman',
                'sclr0' => 'ROMANB',
                'p__phonenumber' => '43',
            ),
            array(
                'u__id' => '2',
                'u__status' => 'developer',
                'u__username' => 'romanb',
                'u__name' => 'Roman',
                'sclr0' => 'JWAGE',
                'p__phonenumber' => '91'
            )
        );

        for ($i = 4; $i < 2000; ++$i) {
            $resultSet[] = array(
                'u__id' => $i,
                'u__status' => 'developer',
                'u__username' => 'jwage',
                'u__name' => 'Jonathan',
                'sclr0' => 'JWAGE' . $i,
                'p__phonenumber' => '91'
            );
        }

        $stmt = new HydratorMockStatement($resultSet);
        $hydrator = new \Doctrine\ORM\Internal\Hydration\ObjectHydrator($this->_em);

        $this->setMaxRunningTime(1);
        $s = microtime(true);
        $result = $hydrator->hydrateAll($stmt, $rsm, array(Query::HINT_FORCE_PARTIAL_LOAD => true));
        $e = microtime(true);
        echo __FUNCTION__ . " - " . ($e - $s) . " seconds" . PHP_EOL;
    }
    
    /**
     * [romanb: 2000 rows => 0.6 seconds]
     *
     * MAXIMUM TIME: 1 second
     */
400
    public function testMixedQueryFetchJoinFullObjectHydrationPerformance2000Rows()
401 402
    {
        $rsm = new ResultSetMapping;
403
        $rsm->addEntityResult('Doctrine\Tests\Models\CMS\CmsUser', 'u');
404
        $rsm->addJoinedEntityResult(
405
                'Doctrine\Tests\Models\CMS\CmsPhonenumber',
406 407
                'p',
                'u',
408
                'phonenumbers'
409 410 411 412 413 414 415
        );
        $rsm->addFieldResult('u', 'u__id', 'id');
        $rsm->addFieldResult('u', 'u__status', 'status');
        $rsm->addFieldResult('u', 'u__username', 'username');
        $rsm->addFieldResult('u', 'u__name', 'name');
        $rsm->addScalarResult('sclr0', 'nameUpper');
        $rsm->addFieldResult('p', 'p__phonenumber', 'phonenumber');
416 417 418 419 420 421 422
        $rsm->addJoinedEntityResult(
                'Doctrine\Tests\Models\CMS\CmsAddress',
                'a',
                'u',
                'address'
        );
        $rsm->addFieldResult('a', 'a__id', 'id');
423 424 425 426 427 428 429 430 431 432 433

        // Faked result set
        $resultSet = array(
            //row1
            array(
                'u__id' => '1',
                'u__status' => 'developer',
                'u__username' => 'romanb',
                'u__name' => 'Roman',
                'sclr0' => 'ROMANB',
                'p__phonenumber' => '42',
434
                'a__id' => '1'
435 436 437
            )
        );

438
        for ($i = 2; $i < 2000; ++$i) {
439 440 441 442 443 444
            $resultSet[] = array(
                'u__id' => $i,
                'u__status' => 'developer',
                'u__username' => 'jwage',
                'u__name' => 'Jonathan',
                'sclr0' => 'JWAGE' . $i,
445 446
                'p__phonenumber' => '91',
                'a__id' => $i
447 448 449 450 451 452
            );
        }

        $stmt = new HydratorMockStatement($resultSet);
        $hydrator = new \Doctrine\ORM\Internal\Hydration\ObjectHydrator($this->_em);

453
        $this->setMaxRunningTime(1);
454
        $s = microtime(true);
455
        $result = $hydrator->hydrateAll($stmt, $rsm);
456 457
        $e = microtime(true);
        echo __FUNCTION__ . " - " . ($e - $s) . " seconds" . PHP_EOL;
458 459 460
    }
}