Doctrine2 デバッグモードとEntityManager::clear()について

Doctrine2のパフォーマンスとメモリ使用量について、「デバッグモード」と「EntityManager::clear()」を検証します。

結論

以下の条件を満たす場合、パフォーマンスとメモリ使用量に影響が出るようです。

  • env が prod 以外で app/console の「--no-debug」オプションを付けない場合
  • 大量データを処理する際に EntityManager::clear() を呼んでいない場合

検証コード

class xxxxCommand extends ContainerAwareCommand
{
    protected function configure()
    {
        $this->setName('sample:doctrine-batch');
        $this->addOption('no-clear', null, InputOption::VALUE_NONE);
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $batchSize = 500;
        $em = $this->getContainer()->get('doctrine')->getManager();

        $stopwatch = new Stopwatch();
        $stopwatch->start('doctrine');

        for ($i = 1; $i <= 10000; ++$i) {

            $entity = new Entity();
            $entity->setXxxx(...);
            ...
            $em->persist($entity);

            if (($i % $batchSize) == 0) {
                $event = $stopwatch->lap('doctrine');
                $em->flush();
                if (!$input->getOption('no-clear')) {
                    $em->clear();    // ★ココ
                }
                printf("%8d: %8.3f sec %8.2f MB\n", $i, $event->getDuration() / 1000,
                    floatval($event->getMemory() / 1024 / 1024));
            }
        }

        $stopwatch->stop('doctrine');
        $em->flush();
        $em->clear();
        $em->close();
    }
}

no-debugモード + EntityManager::clear() あり

$ php app/console sample:doctrine-batch --no-debug
     500:    0.204 sec    13.50 MB
    1000:    0.964 sec    14.25 MB
    1500:    1.663 sec    14.50 MB
    2000:    2.421 sec    14.50 MB
    2500:    3.117 sec    14.50 MB
    3000:    3.832 sec    14.50 MB
    3500:    4.690 sec    14.50 MB
    4000:    5.431 sec    14.50 MB
    4500:    6.159 sec    14.50 MB
    5000:    6.861 sec    14.50 MB
    5500:    7.593 sec    14.50 MB
    6000:    8.326 sec    14.50 MB
    6500:    9.037 sec    14.50 MB
    7000:    9.735 sec    14.50 MB
    7500:   10.473 sec    14.50 MB
    8000:   11.215 sec    14.50 MB
    8500:   11.917 sec    14.50 MB
    9000:   12.646 sec    14.50 MB
    9500:   13.382 sec    14.50 MB
   10000:   14.097 sec    14.50 MB

no-debugモード + EntityManager::clear() なし

$ php app/console sample:doctrine-batch --no-debug --no-clear
     500:    0.203 sec    13.50 MB
    1000:    0.959 sec    14.50 MB
    1500:    1.743 sec    15.00 MB
    2000:    2.639 sec    15.75 MB
    2500:    3.568 sec    16.00 MB
    3000:    4.545 sec    16.50 MB
    3500:    5.609 sec    17.00 MB
    4000:    6.720 sec    18.00 MB
    4500:    7.930 sec    18.50 MB
    5000:    9.166 sec    19.00 MB
    5500:   10.487 sec    19.50 MB
    6000:   11.896 sec    20.00 MB
    6500:   13.339 sec    20.50 MB
    7000:   14.887 sec    21.00 MB
    7500:   16.515 sec    21.50 MB
    8000:   18.182 sec    22.25 MB
    8500:   19.894 sec    22.75 MB
    9000:   21.713 sec    23.25 MB
    9500:   23.608 sec    23.75 MB
   10000:   25.574 sec    24.25 MB

debugモード + EntityManager::clear() あり

$ php app/console sample:doctrine-batch
     500:    0.209 sec    13.50 MB
    1000:    2.120 sec    15.75 MB
    1500:    4.154 sec    17.00 MB
    2000:    6.052 sec    18.00 MB
    2500:    7.926 sec    19.50 MB
    3000:    9.830 sec    20.75 MB
    3500:   11.806 sec    21.75 MB
    4000:   13.900 sec    23.00 MB
    4500:   15.802 sec    24.25 MB
    5000:   17.684 sec    25.50 MB
    5500:   19.617 sec    26.50 MB
    6000:   21.513 sec    27.75 MB
    6500:   23.447 sec    28.75 MB
    7000:   25.680 sec    29.75 MB
    7500:   27.579 sec    31.00 MB
    8000:   29.386 sec    32.00 MB
    8500:   31.270 sec    33.75 MB
    9000:   33.100 sec    35.00 MB
    9500:   35.112 sec    36.00 MB
   10000:   36.941 sec    37.00 MB

env=prod + EntityManager::clear() あり

$ php app/console sample:doctrine-batch --env=prod
     500:    0.209 sec    12.50 MB
    1000:    0.971 sec    13.50 MB
    1500:    1.698 sec    13.75 MB
    2000:    2.408 sec    13.75 MB
    2500:    3.099 sec    13.75 MB
    3000:    3.790 sec    13.75 MB
    3500:    4.514 sec    13.75 MB
    4000:    5.194 sec    13.75 MB
    4500:    5.945 sec    13.75 MB
    5000:    6.652 sec    13.75 MB
    5500:    7.522 sec    13.75 MB
    6000:    8.219 sec    13.75 MB
    6500:    8.927 sec    13.75 MB
    7000:    9.639 sec    13.75 MB
    7500:   10.350 sec    13.75 MB
    8000:   11.055 sec    13.75 MB
    8500:   11.769 sec    13.75 MB
    9000:   12.456 sec    13.75 MB
    9500:   13.142 sec    13.75 MB
   10000:   13.834 sec    13.75 MB

env=prod + EntityManager::clear() なし

$ php app/console sample:doctrine-batch --env=prod --no-clear
     500:    0.199 sec    12.50 MB
    1000:    0.965 sec    13.75 MB
    1500:    1.747 sec    14.25 MB
    2000:    2.606 sec    14.75 MB
    2500:    3.521 sec    15.25 MB
    3000:    4.513 sec    15.75 MB
    3500:    5.560 sec    16.25 MB
    4000:    6.670 sec    17.25 MB
    4500:    7.822 sec    17.50 MB
    5000:    9.068 sec    18.00 MB
    5500:   10.375 sec    18.50 MB
    6000:   11.733 sec    19.00 MB
    6500:   13.159 sec    19.50 MB
    7000:   14.662 sec    20.00 MB
    7500:   16.323 sec    20.50 MB
    8000:   17.975 sec    21.25 MB
    8500:   19.685 sec    21.75 MB
    9000:   21.455 sec    22.50 MB
    9500:   23.266 sec    23.00 MB
   10000:   25.168 sec    23.25 MB