How to enable “Magento Profiler” and use it to identify Magento Performance bottlenecks

Magneto comes with a very useful debugging tool for developers called “Profiler”. It can be used to identify issues in your code making the website slow. The Magento “Profiler” reports the time taken by block of code under test to execute, the number of times the block of code executed and the memory used by it while rendering a Magento web page.

By default Magento Profiler is disabled, let see how to enable it.

How to enable “Profiler” in Magento?

To enable “Profiler” in Magento, you have to go through 2 parts:

Part A

1) Go to Admin >> System >> Configuration.

2) Under the “Advanced” tab, click the “Developer” link to open the developer section.

3) Now expand the “Debug” block and select “Yes” in the Profiler dropdown.

4) If you are debugging in a live environment then its better to restrict the profiler output display to the developers only so that customers browsing your website do not see any differences on the frontend. To do so expand the “Developer Client Restrictions” block and add the developer IPs comma separated in the text box provided.

5) Save the configuration.

Magento Profiler

Part B

1) Open “index.php” file located at the {{MAGENTO_ROOT}} folder in your favorite editor.

2) Find the line of code “#Varien_Profiler::enable();”.

3) Uncomment this line as below and save the file.


require_once $mageFilename;

Varien_Profiler::enable();

if (isset($_SERVER['MAGE_IS_DEVELOPER_MODE'])) {
Mage::setIsDeveloperMode(true);
}

Thats all, profiler has been enabled.

Now reload the Magento page and scroll down to the bottom of the page, you will see a link “[Profiler]”. Click on it, it will expand to table that shows statistics about times, number of execution and memory used for each watched fragment of the code.

Magento Profiler

The result of the profiler are hard to interpret, it does not reflect the true structure/hierarchy of the code. To provide a nice colourful and user friendly interface to the Magento profiler, Fabrizio has built a Magento module that extends the default profiler and makes it a much more useful tool.

Click here to download the extension from the git hub.

If you look at function _toHtml() in file \app\code\core\Mage\Core\Block\Profiler.php, you will notice that Magento does profiling for “main connection to database and queries” also, but why did it not show in profiler output? Guessed right it needs to be enabled separately.

As seen in function getSqlProfiler() in file /lib/Varien/Profiler.php, the profiler displays the “Number of queries executed and the time taken for it”, “Average query length”, “Queries per second”, “Longest query length”, and “Longest query”.

How to enable SQL profiling?

1) To enable sql profiling, you need to enable “Profiler” from admin as described in Part A above.

2) Open the local.xml config file located in /app/etc/ folder and add the node <profiler>true</profiler> into global/resources/default_setup/connection/


<resources>
<db>
<table_prefix><![CDATA[]]></table_prefix>
</db>
<default_setup>
<connection>
<host><![CDATA[localhost]]></host>
<username><![CDATA[root]]></username>
<password><![CDATA[]]></password>
<dbname><![CDATA[magento_1_8_1_0]]></dbname>
<initStatements><![CDATA[SET NAMES utf8]]></initStatements>
<model><![CDATA[mysql4]]></model>
<type><![CDATA[pdo_mysql]]></type>
<pdoType><![CDATA[]]></pdoType>
<active>1</active>
<profiler>true</profiler>
</connection>
</default_setup>
</resources>

3) Save the file and clear the cache.

Now if you refresh the page, you will see the SQL profiling output below the code profiling results.

Magento SQL Profiling

How to add profiling to custom module code?

To add profiling to custom module code add:


Varien_Profiler::start('LabelForCodeFragmentToProfile');
///... here some code we like to get statistics for
Varien_Profiler::stop('LabelForCodeFragmentToProfile');

If cumulative time of execution for this code is more than .0001 second it will be shown in profiler table with cumulative time, number of executions and cumulative memory consumption.

Lets have a look at Mage::run() function, to see an example of using profiler in code:


try {
Varien_Profiler::start('app');

Varien_Profiler::start('app::init');
self::app($code, $type, $options);
Varien_Profiler::stop('app::init');

Varien_Profiler::start('app::dispatch');
self::app()->getFrontController()->dispatch();
Varien_Profiler::stop('app::dispatch');

Varien_Profiler::stop('app');
}
catch (Mage_Core_Model_Store_Exception $e) {
$baseUrl = rtrim(dirname($_SERVER['SCRIPT_NAME']), '/');
header('Location: ' . $baseUrl.'/404/');
die();
}

If you need to enable profiling just for your custom code and not for entire Magento code then you do not need to uncomment the line “Varien_Profiler::enable();” in index.php file located at {{Magento_Root}} folder; enable and add profiling in your custom code as shown below:


//First, enable the profiler
Varien_Profiler::enable();

//Second, start the profiler with a unique label

Varien_Profiler::start('LabelForCodeFragmentToProfile');

///... here some code we like to get statistics for

//next, stop your profiler with the same label
Varien_Profiler::stop('LabelForCodeFragmentToProfile');

//Last, disable your profiler
Varien_Profiler::disable();

Leave a Comment

Back to top