Dirt cheap way to make magento faster

  • Sumo

Dirt cheap way to make magento faster

Performance is key. Speed is everything when it comes down to websites, especially in eCommerce.
The code we are about to see it is a dirt cheap way to make magento faster. It is not performance optimization just a good way to improve Magento’s processing time.

If you don’t want to use better implementations or can’t use something like nginx or you simply can use vala then this article may help as it will provide you with some guidance on how to make magento faster and improve its speed without having to spend too much time or change its architecture.

What is all this about?

While there are several ways to accomplish the same task and some are more elegant than others.
This one speeds up any Magento community edition and it is really easy and quick (read no Magento compliant in my book – because it modifies the templates). It is very easy because you have all the components already installed and don’t need to download any other extensions but I can’t stress enough this is a very dirty and cheap way to make magento load faster and it is far from perfect.

In other words while this trick can help you speed up your Magento store I am not too proud of it. Why is that a big deal? I am not a big fan of modifying templates. Even when they are technically “allowed” to be modified I’d much rather use the layout xml and the event driven architecture already in place. Wait, don’t we have extensions like lightspeed and nitrogento which make Magento fly already, why do we need another one?

This one allows me to get the similar results with a very slight advantage: Magento events still get dispatched and I am not sacrificing speeds or the processing time too much and this is not really an extension even when I will show it that way.

How can we get this baby rolling?

Every single required component is already in your system. Because Magento is already using some sort of caching, hopefully something much better than just using the default file based caching option.

I recommend that you use memcache or apc with a web server like Nginx to get better results but that is totally up to you. As you will see, using Apache and the file based caching I still got really good results.

Let’s get down to business

Your actual cache helper class

 class Performance_CacheHelper_Helper_Data extends Mage_Core_Helper_Data{
    /**
     * cache key
     * @var string|mixed
     */
    protected $key;
    /**
     *
     * @var Mage_Core_Model_Cache
     */
    protected $_cache;
    /**
     * data container
     * @var string|mixed
     */
    protected $data;
    protected $tags = array(__CLASS__);
    public function canUseCache()
    {
        //verify if the cache is enabled
        return Mage::app()->useCache('block_html');
    }
    //**
    //* Gets the cache object
    //* @return Mage_Core_Model_Cache
    //*/
    protected function _getCacheObject()
    {
        if (!$this->_cache) {
            $this->_cache = Mage::app()->getCache();
        }
        return $this->_cache;
    }
    public function getData()
    {
        return $this->data;
    }
    public function setData($data)
    {
        $this->data = $data;
        return $this;
    }
    public function getKey()
    {
        return $this->key;
    }
    public function setKey($key)
    {
        $this->key = $key;
        return $this;
    }
    //**
    //* saves data in a serialize format to cache under the name of this class if the cache can be used
    //* @return Performance_CacheHelper_Helper_Data
    //*/
    public function saveDataInCache(){
        if ($this->canUseCache()) {
            $cookie = Mage::getModel('core/cookie');
            $this->_getCacheObject()->save(serialize($this->data), $this->key, $this->tags, $cookie->getLifetime());
        }
        return $this;
    }
    /**
     * @param string $key
     * @return mixed
     */
    public function getDataFromCache($key)
    {
        $this->key = $key;
        return $this->_getCachedData();
    }
    /**
     * gets the data saved in cache, if it finds it then it unserializes itF
     * @param string|mixed $key
     * @return bool | string
     */
    protected function _getCachedData($key = null)
    {
        if ($key !== null) {
            $this->key = $key;
        }
        /**
         * clear the data variable
         */
        $this->data = false;
        //ensure cache can be used
        if ($data = $this->_getCacheObject()->load($this->key)) {
            $this->data = unserialize($data);
        }
        return $this->data;
    }
}

In your current template:

//this code has been modified so the highligthing works right
$helper = Mage::helper('cachehelper');
$cachingEnabled = array('catalog');
$key = md5(Mage::helper('core/url')->getCurrentUrl());
$currentModule = Mage::app()->getRequest()->getModuleName();
if (in_array($currentModule, $cachingEnabled)){
 if ($content = $helper->getDataFromCache($key)){
//nothing do $content now has the data
 }else{
     $content = $this->getChildHtml('content');
     $helper->setKey($key)->setData($content)->saveDataInCache();
  }
}
else{
 $content = $this->getChildHtml('content');
}

Now inside the div with class col-main replace $this->getChildHtml(‘content’) with:

echo $content

Now let’s test this and see how it works:

siege -t1m -c5 -b -d -i http://mage.dev/apparel

Examples with File Based cache

Without the “dirt cheap trick”

Transactions:324 hits
Availability:100.00 %
Elapsed time:59.94 secs
Data transferred:2.13 MB
Response time:0.92 secs
Transaction rate:5.41 trans/sec
Throughput:0.04 MB/sec
Concurrency:4.95
Successful transactions:324
Failed transactions:0
Longest transaction:2.32
Shortest transaction:0.63

With the “dirt cheap trick”

Transactions:475 hits
Availability:100.00 %
Elapsed time:59.56 secs
Data transferred:3.14 MB
Response time:0.62 secs
Transaction rate:7.98 trans/sec
Throughput:0.05 MB/sec
Concurrency:4.97
Successful transactions:475
Failed transactions:0
Longest transaction:1.45
Shortest transaction:0.37

Examples with memcache

Without the “dirt cheap trick”

Transactions:329 hits
Availability:100.00 %
Elapsed time:59.65 secs
Data transferred:2.17 MB
Response time:0.90 secs
Transaction rate:5.52 trans/sec
Throughput:0.04 MB/sec
Concurrency:4.95
Successful transactions:329
Failed transactions:0
Longest transaction:1.60
Shortest transaction:0.68

With the “dirt cheap trick”

Transactions:507 hits
Availability:100.00 %
Elapsed time:59.47 secs
Data transferred:3.35 MB
Response time:0.58 secs
Transaction rate:8.53 trans/sec
Throughput:0.06 MB/sec
Concurrency:4.97
Successful transactions:507
Failed transactions:0
Longest transaction:1.91
Shortest transaction:0.40

Lots of numbers, what do they mean?

It simply means that this trick works… more than just good enough which is pretty awesome right? What is that I hear? You don’t think siege is a good testing tool and this is not a good indication? What about jmeter? I believe you cannot disagree jmeter will benchmark and do some very intelligent testing.

Here is the test plan I used: http://goo.gl/jp83n

Here are the results, sorry about the formatting:

Without the “dirt cheap trick”

sampler_labelaggregate_report_countaverageaggregate_report_minaggregate_report_maxaggregate_report_sthdevaggregate_report_error%aggregate_report_rateaggregate_report_bandwidthaverage_bytes
/apparel10171012742180290.242312559704.5724737083199.835855338444753
/furniture/living-room108966181272254.866415990807.5757575758227.117365056830699
/furniture/bedroom10747565953137.607.3637702504204.862389543428488
TOTAL3011175652180484.880513345507.2904009721246.668058930734646.6666666667

With the “dirt cheap trick”

sampler_labelaggregate_report_countaverageaggregate_report_minaggregate_report_maxaggregate_report_sthdevaggregate_report_error%aggregate_report_rateaggregate_report_bandwidthaverage_bytes
/apparel10675434904147.373708645706.1199510404268.315626912544895
/furniture/living-room10741581950107.750684452605.4436581383164.067816072430862.6
/furniture/bedroom10631389827157.522188913207.3691967576206.138886330128644.4
TOTAL30682389950146.3524057427010.8577633008369.001368530634800.6666666667

You got my attention… talk to me

If you made this far well it means you probably want to know more about this. But as you could see this is a trick that helps a lot but it doesn’t compete with Magento’s FPC or Varnish and it still needs lots of polishing.

The good thing is that is doesn’t require holepunching or lots of Magento knowledge to get rolling. You don’t need to spend 40hrs worth of work and you get immediate results. So this could be used as a temporary bandaid.

Now a few pointers… this is not the full implementation, as this code doesn’t work well with load balancers and there is not a way to clear the cache either.

But implementing both is trivial because you could use the server signature as part of the caching key and the same for clearing cache.

The most important use I can see for something like this is for category or product pages with remarketing scripts that will have different values depending on the category.

VN:F [1.9.22_1171]
Rating: 10.0/10 (7 votes cast)
VN:F [1.9.22_1171]
Rating: +4 (from 4 votes)
Dirt cheap way to make magento faster, 10.0 out of 10 based on 7 ratings

Author: Luis Tineo

Husband, Father, performance improvement junkie, biker and video gamer, Linux user and in my day job I'm a Software Engineer at BuyerQuest.

Share This Post On
  • It’s very simple actually, The Mage.php gives you the power to implement your own caching system much like the way Full page caching works. If you don’t want to go that route, you could for example cache in the process response event and read the cache in the predispatch event

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  • Hi Luis, How do the “data transferred” figures come into play here? Noticed they are much higher with your hack.

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
    • Luis Tineo

      Hey Mark,
      Good to see you around – the data transferred metric is very important and the higher the better. Because siege aggregates the header and body of each successful request the more successful requests you get the better.

      Because Magento spends less time assembly the content the web server can ‘serve’ more people quicker.

      does it make sense?

      VA:F [1.9.22_1171]
      Rating: 0.0/5 (0 votes cast)
      VA:F [1.9.22_1171]
      Rating: 0 (from 0 votes)
  • Babu

    I am new to magento and trying tricks to make my website faster. Didnt quite understand what has to be done here. Is there a step by step guide which i can follow to have this done.

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  • alex

    can you contact me

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  • David Murray

    Hi Luis. Would you be able to make a youtube video on the installation?

    Would really appreciate it!

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)