Optimising & streamlining CakePHP installations can be pretty tricky, because it’s such a broad system.
I built a CakePHP based application for my workplace that involves tracking jobs. We do a lot of jobs at Celf Creative, so naturally, the database is quite bulky.
I was getting a lot of ‘complaints’ from my colleagues that CakePHP was simply too slow and took too much time to do anything on the site. I told them to just get on with it, but predictably, use of the service that I so lovingly constructed started to drop.
So I found some time to spend optimising the CakePHP site so that it can be used in a more efficient manner.
On my day of testing, our internet connection was particularly slow, which in-theory, allowed me to easily see any finite adjustments to the load-time, on a broader scale.
My pathway to optimising CakePHP
I worked with a number of different resources to get the most efficient optimisation to my CakePHP installation. My first step was to read the current state of the website.
A page refresh took 16.08 seconds.
With the help of the chainfire development blog’s post on CakePHP performance, I took to adding a helper to manage URLs. From what I could gather, this Helper that the poster had written caches the ‘unseen’ part of the URL.
When $this->Html->link is used, the CakePHP backend creates (at run-time) a link to the specified path. The domain of the request will rarely change. If this part of the URL is cached, then there’s less processing to determine it.
A page refresh took 13.36 seconds.
So this saved me around 3 seconds, but the time is still unacceptable.
Chainfire also mention that setting debug to 0 helps in a production environment. In core.php, there is a debug variable that processes and outputs all the SQL commands for us to analyse. This is intended to work within development environments to help iron out code kinks. Setting it to 2 outputs full SQL processes, 1 outputs just errors, and 0 outputs nothing and indeed processes no output in the backend.
A page refresh took 10.20 seconds.
Miles Johnson has written an assets compression script. This script can be used to minify and compress all css and js resources. I had a lot of resources, so this helped quite a bit.
A page refresh took 5.48 seconds.
Pseudocoder has also published an article on CakePHP optimisation. The 8 Ways to Speed Up the Performance of CakePHP Apps was a great help. Option 6 allowed me to cache my models so they’re not rebuilt every time.
A page refresh took 3.49 seconds.
There was also a number of other things I did to improve my views.
- Converted many of my icons to ‘sprites’ i.e. a single file for all icons and then control which is displayed with background-position and absolute image positioning
- Removed some unnecessary processing
- I had stupidly, included some conditions in my view, I added this to the controller instead
A page refresh took 1.57 seconds.
There is every possibility that my internet connection improved whilst doing these tests, which may skew the results. That being said, the difference is still 14 full seconds of processing time removed.
Conclusion
When I originally built the CakePHP site (well over a year ago), I was fairly unfamiliar with MVC principles and even with CakePHP itself. In hindsight, I can see that nothing will improve the speed of the application like good coding practices and familiarity with the system.
A lot of the techniques I discussed above, should have been implemented in the first instance, instead of frantically trying to speed the site up at the end.
I hope somebody else finds this useful.
Comments
Powered by Facebook Comments