InsertPerformanceTest.php 1.33 KB
Newer Older
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
<?php

namespace Doctrine\Tests\ORM\Performance;

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

use Doctrine\Tests\Models\CMS\CmsUser;

/**
 * Description of InsertPerformanceTest
 *
 * @author robo
 */
class InsertPerformanceTest extends \Doctrine\Tests\OrmPerformanceTestCase
{
    protected function setUp() {
        $this->useModelSet('cms');
        parent::setUp();
    }

    /**
     * [romanb: 10000 objects in ~8 seconds]
     */
    public function testInsertPerformance()
    {
        $s = microtime(true);

        $conn = $this->_em->getConnection();

        $this->setMaxRunningTime(10);

32 33
        //echo "Memory usage before: " . (memory_get_usage() / 1024) . " KB" . PHP_EOL;
        
romanb's avatar
romanb committed
34
        $batchSize = 20;
35
        for ($i=1; $i<=10000; ++$i) {
36 37 38 39
            $user = new CmsUser;
            $user->status = 'user';
            $user->username = 'user' . $i;
            $user->name = 'Mr.Smith-' . $i;
romanb's avatar
romanb committed
40
            $this->_em->persist($user);
romanb's avatar
romanb committed
41
            if (($i % $batchSize) == 0) {
42 43 44 45
                $this->_em->flush();
                $this->_em->clear();
            }
        }
46 47 48
        
        //gc_collect_cycles();
        //echo "Memory usage after: " . (memory_get_usage() / 1024) . " KB" . PHP_EOL;
49 50 51

        $e = microtime(true);

52
        echo ' Inserted 10000 objects in ' . ($e - $s) . ' seconds' . PHP_EOL;        
53 54 55
    }
}