Joomla on steroids: A Hack to render in 40ms
- Category: Joomla!
- Published: Thursday, 26 March 2015 09:28
- Written by Administrator
- Hits: 420
Ever thought you could get your Joomla site to load faster and tried stuff....well, today Im going to show you a hack to render your Joomla site in less than 40 milli seconds. Cache #22 render is a Joomla measurement that is displayed when you set Debug on in Joomla's Global Configuartion. Technically, the site first spews out some code which contains links to images and scripts and load time is the rendering of all such, on the users screen. The Joomla debug render measurement is the first spew on the server perse. If you're looking at load times...watch out for my new Joomla package multicache that guarantees to load in under 3 seconds.Well that's a 40$ money back guarantee.
So delving straight into the few tweaks that could enable rendering in 40 milli seconds, open up the first file called index.php in your Joomla root folder and define a new Joomla event, for the sake of simplicity lets call it on after load like this onAfterLoad. We will be adding two lines of code to the index.php just before the $app->execute() , like fig 1.
What this does is, force Joomla to search for an onAfterLoad event before it does anything else.......and hey...Joomla does a lot of stuff in between like initiating sessions and language constructs among other things. What our hack intends to do is check to see whether Joomla has already constructed this page in the past and render else go back and construct it. Like I said this is a naive hack if you have a more sophisticated application where sessions and language are crucial ...watch out for multicache. After you insert these two lines of code your index page should look something like fig 2. .
Now for the second part, there is no Joomla event called onAfterLoad. Further there is no method that runs out of onAfterLoad. So we need to define it. A simple copy paste should do the trick. Open up your plugins folder yoursite/plugins/system/cache/cache.php. Specifically the cache.php. You will see a function, described as onAfterInitialise. copy every thing from the protected function onAfterInitialise() upto the closing bracket and paste it just above the method. Now rename the pasted onAfterInitialise to onAfterLoad. Your hacks should look something like this.
And that's that. Now test a page on your site and see how it loads. Measuring the render time is not going to be as simple as Joomla's global debug though as Joomla's cache is off when you set it to debug. The hack that we described cuts out all the frills and with it the measurement methods. One way to get around this is to define a global constant with the microtime(true) right at the start of the index.php and an measure the time taken right at the end of the plugin.
The index.php will have this piece right in the beginning
define('SOMETIMEVARFORMEASUREMENT' , microtime(true));
In your plugin onAfterLoad methodlook for the piece that says $app->close();
and define this just before it
$t2 = microtime(true);
$t1 = defined(SOMETIMEVARFORMEASUREMENT)? SOMETIMEVARFORMEASUREMENT:null;
$totaltime = $t2 - $t1;
As mentioned earlier this should not be used for professional sites as it cuts out a lot of middle stuff. But hey, if you have a site which showcases stuff wherein users do not normally login, try it out ...it should be fun.