Magento websites might be slow. In many cases the poor performance comes from poor coding. In this article I will show you how to speed up magento by reducing time to first byte (TTFB).
Did you know TTFB affects SEO?
First we need to know what magento is doing behind the scenes. For that reason we will use the built-in magento tool - Magento Profiler. It will show us the list of internal magento calls along with the corresponding run times.
You can as well place profiler handles in your own code to measure its performance and identify bottlenecks.
How to Enable Magento Profiler
In magento backend go to System > Configuration > Advanced > Developer > Debug > Profiler set to Yes.
In index.php file uncomment the following line:
Make sure you flush magento cache for magento profiler out to be displayed.
How to Use Profiler in your code
In order to use profiler add Varien_Profiler::start('any_name_here') and Varien_Profiler::stop('any_name_here') to the code block that you want to profile:
Varien_Profiler::start('any_name_here'); ....magento code block that is too slow....; Varien_Profiler::end('any_name_here'); ...
Analyze Profiler Output
- Code Profiler - the handle that is used in Varien_Profiler::start and Varien_Profiler::stop calls
- Time - time in seconds that takes to run code between Varien_Profiler::start and Varien_Profiler::stop calls with the handle specified in Code Profiler column
- Cnt - number of times the specified code block was executed during page load
- Emalloc - memory allocated to PHP process during code block execution by emalloc system call
- RealMem - real memory allocated to PHP process during code block execution
How to reduce TTFB
Disable unnecessary modules
Go to your website homepage and analyze profiler output at the bottom. First look for OBSERVER keywords. Those are the observers that most probably are executed on every page request. Some of them might be disabled - for example if you dont use magento reviews or magento sales rules functionality the corresponding Mage_Review and Mage_SalesRule modules might be disabled.
An approximate list of default magento extensions you might not need:
Also you might have the custom extension installed in the past that you dont use anymore but it still has frontend observer that is executed on every page request slowing your website down.
Optimize code in theme .phtml files
Php code in theme files could be optimized as well. I will give you an example:
<?php $_collection = $this->getLoadedProductCollection(); ?> <?php foreach($_collection as $_product):?> <?php $_product = Mage::getModel('catalog/product')->load($_product->getId()) ?> <?php echo $_product->getSku() ?> <?php endforeach; ?>
You don't need to load catalog/product model in foreach loop as $_product variable is already catalog/product model. If collection is big enough you do many many unneeded model loads.
That is just one example of how unoptimized magento php code might be. To reduce time to first byte and speed up magento website you might want to go over every .phtml file and audit the code there. Pay attention to the loop structures.
Upgrade magento to the latest version
Magento team always work on making magento the better e-commerce framework. If you are running old magento version try to perform magento upgrade. Newest releases consists of many core files rewrites that optimize the way magento handles internal logic.
Reducing time to first byte might be tedious and complex but the result would sure cheer you up. Your website pages would load faster even uncached. Cache != performance so when optimizing magento you should first optimize TTFB and not setup complex cache system that would most probably bring a lot of maintenance headache.
If you find this post interesting do not hesitate to sign up for our newsletter and join the 692 people who receive Magento news, tips and tricks regularly.