Benchmarking My CakePHP Asset Packer Helper
R. Rajesh Jeba Anbiah left a comment concerned about the performance hit when using the CakePHP Asset Packer Helper.
The major reason for choosing the Asset Helper is to speed up the site. But, the hit on the helper call is heavy–at least for me. [snip] ...for the real benefit, one seems need to manually minify/pack.
I decided to put this to the test. I grabbed a fresh download of the latest CakePHP 1.2-beta and made a basic TestsController with one action with a view. It didn't use a model. I took the default layout and moved it to my /app/views/layouts. In the layout I added 2 Stylesheets and 2 JavaScripts. In the view I included one of each using the $scripts_for_layout method. I used ab, with the parameters "-c 10 -t 60", to do the testing. Since this was all on my local machine network speed isn't really a factor. Essentially I would be getting the times to render the page.
The first test I ran was without the Asset Packer Helper - what you would get with the base CakePHP install.
Here's the baseline results:
Complete requests: 622
Requests per second: 10.36 [#/sec] (mean)
The hope is that including the helper won't hurt these numbers too much. The speed increase gained by using the helper is seen in the download of merged js and css files and since ab is only testing the html file (not the images, css, jss linked within), the benefit of the helper won't be evident in this test.
First I included the helper and commented out the filecaching. This is essentially what happens when the helper is being used for the first time on a particular group of css/js files. Either because one of the files has changed or because the cache hasn't been created yet. Consider this to be the worst case scenario and probably would only really happen a tiny percentage of the time, like right after you do an update. I expected the numbers to take a hit, but not to be this bad:
Complete requests: 32
Requests per second: 0.52 [#/sec] (mean)
Did I emphasize that this scenario doesn't happen very often? I could try to improve this, but I'm not sure it's worth it. This doesn't happen too ofter, plus most of the time is probably in the css and js minifying/packing, which are vendor classes.
Let's take a look at a more common use case. The cached files already exist, so the helper checks to see if there are any changes, finds none and doesn't have to do any extra processing.
Complete requests: 583
Requests per second: 9.69 [#/sec] (mean)
Much better. Still a hit from the baseline numbers, but not too bad.
Here's one more test. In the AssetHelper class there is a var $checkTS. Setting this to false will disable the functionality described above, where the helper will see if any of the original css/js have changed and the cached version needs to be rebuilt. You can safely set this to false if you clean out your packed directories with new builds.
Complete requests: 602
Requests per second: 10.03 [#/sec] (mean)
That's an improvement over the default settings for the helper and are pretty close to the baseline results. The difference is 3.15700 milliseconds/request or .003157 in seconds.
I may look into trying to get that last scenario closer to the baseline, but the difference isn't so significant that I would really worry about it.
So what does the extra .003157 seconds of processing time get you? This isn't a perfect test, but I ran Yslow on the baseline setup and again with the helper. The baseline scored a D(61). With the helper that jumped to a B(81).

7 Comments
I would assume that in reality your requests/sec would probably be lower when you have models, components, include files etc packed into your controllers/views :)
The cost of using a framework!
-Mandy
Yes, I wouldn't pay too much attention to the actual numbers since this was done on my laptop running XAMPP. I was looking more for the relative change.
Besides in a real world scenario I would probably be using caching (which I profiled here) and none of this would matter.
But, I think, you need to use xdebug or APD to profile your code.
I'm not sure how much there would be to gain by profiling this. Sure there are probably tweaks that could be done to make it faster, but the performance is about what I would expect. I'm more concerned with Rajesh's statement that the helper call is heavy, which I believe is an issue with his particular instance and not a problem with the helper as a whole.
How do you get background images in the CSS to appear when using the packer?
What's probably happening is that your packed css files are being placed in /css/packed, however the images referenced in the css are using paths relative to /css. The only way to fix this at the moment is to change the image paths in your css to be absolute. Instead of img/whatever.jpg make them /img/whatever.jpg
I realize this isn't a great solution and isn't practical for all setups. I'll probably add something in the future to better accommodate relative urls.
-Matt