Uvd

Profiling Doctrine 2 with Zend Framework

We have been working on a few projects using Doctrine 2 and Zend Framework (ZF). We are using the Bisna Package (some additional documentation available here) as the glue to get everything working together nicely.

When working with any application it is always useful to have some kind of reporting mechanism for execution time, number of queries, memory use etc and we have used the following to help us profile our applications.

Doctrine 2 provides a simple interface for logging, namely
\Doctrine\DBAL\Logging\SQLLogger()
and the following resources implement it.

Using ZFDebug and the Doctrine 2 plugin

Download and unpack the ZFDebug package from https://github.com/MontmereLimited/ZFDebug

Drop the ZFDebug folder found in the ‘library’ directory of the unzipped package next to your Zend folder in the library directory of your application.

The ZFDebug plugin includes the following code for bootstrapping the ZFDebug bar with Doctrine 2

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
<?php
// ../application/Bootstrap.php
 
protected function _initZFDebug()
{
if(APPLICATION_ENV == 'development')
{
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace('ZFDebug');
$em = $this->bootstrap('doctrine')->getResource('doctrine')->getEntityManager();
$em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\DebugStack());
 
$options = array(
'plugins' => array(
'Variables',
'ZFDebug_Controller_Plugin_Debug_Plugin_Doctrine2' => array(
'entityManagers' => array($em),
),
'File' => array('basePath' => APPLICATION_PATH . '/application'),
'Exception',
'Html',
'Memory',
'Time',
)
);
 
$debug = new ZFDebug_Controller_Plugin_Debug($options);
$this->bootstrap('frontController');
$frontController = $this->getResource('frontController');
$frontController->registerPlugin($debug);
}
}

As our Doctrine 2 configuration is set in the application.ini we are using the following code

1 2 3
# ../application/configs/application.ini
resources.doctrine.dbal.connections.default.sqlLoggerClass = "Doctrine\DBAL\Logging\DebugStack"
ZFDebug = 1
view raw application.ini hosted with ❤ by GitHub

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
// ../application/Bootstrap.php
 
if($this->getOption('ZFDebug') == '1')
{
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace('ZFDebug');
$em = $this->bootstrap('doctrine')->getResource('doctrine')->getEntityManager();
 
$options = array(
'plugins' => array(
'Variables',
'ZFDebug_Controller_Plugin_Debug_Plugin_Doctrine2' => array(
'entityManagers' => array($em),
),
'File' => array('basePath' => APPLICATION_PATH . '/application'),
'Exception',
'Html',
'Memory',
'Time',
)
);
 
$debug = new ZFDebug_Controller_Plugin_Debug($options);
 
$this->bootstrap('frontController');
 
$frontController = $this->getResource('frontController');
$frontController->registerPlugin($debug);
}

You can also install and enable the python-sqlparse on your development environment to prettify the SQL output, however this will increase your script execution time for each application, I noticed an increase of ~1 second locally.

Using FirePHP to profile Doctrine 2

Micheal Ridgway’s package is also very easy to get up and running. First make sure you have Firefox, Firebug and FirePHP installed.

Then download https://github.com/mridgway/ZendX_Doctrine2/ and extract the above package from github.

Grab the ZendX directory found inside lib in the package, and drop it into your application’s library directory (next to your Zend folder as with ZFDebug).

You can then load the plugin with the following code

1 2
# ../application/configs/application.ini
resources.doctrine.dbal.connections.default.sqlLoggerClass = "ZendX\Doctrine2\FirebugProfiler"

Then load up your app in firefox and check out the console in Firebug for query logging goodness.

N.B. There appears to be a FirePHP extension for Chrome, but I couldn’t get it working, so I would recommend using ZFDebug if Chrome is your browser of choice.

There is also a plugin for Doctrine 1 and ZFDebug

 

  • Webdevilopers

    Is there a working example using ODM and the documentManager for MongoDB?

  • http://twitter.com/Peterjmit Peter Mitchell

    I haven’t had any experience with the ODM (yet) sorry!