Wednesday, October 17, 2007

PHP Performance Improvements

Summary:
My Zend framework page generation time went from 1.2 sec to 0.4 sec using a number of techniques. I compare my performance with Rob Allen's tutorial code (which has minimal excess code -- being a tutorial). Allen's code also runs at 0.4 sec (http://www.nabble.com/Re%3A-ZF-performance-advice-p13185506s16154.html), so my ZF infrastructure has negligible overhead now.

It did not start out that way. It started out slow at 1.2 sec.

I looked at techniques from http://ilia.ws/files/frankfurt_perf.pdf and some from http://talks.php.net/show/ezkey06

My setup: my laptop is the web server: 1.2 gb ram, 1.6 ghz single core, 7200 rpm disk, Windows XP, php 5.2.3, Apache 2.2.4, postgres 8.2, Zend Framework 1.0.2 and I used a different PC as my browser client.

I'm testing the performance of a web page that just has a few words on it. It also does a Smarty include of a header page with a few words. The PHP script does connect to my db.

It's hard to track down the time culprut. Time floats from one section to another when I try to narrow down slow sections. Yet the overall time is somewhat consistent. I switched to mysql db for timing purposes because the times were more consistent and the connection time was smaller so other performance times would not be masked by the db connect.

What code changes did I implement?
  • combine classes into 1 file so fewer files are require / include
  • use full path or at least a relative path for require / include for files.
  • Prune include path to be only essential directories (eg: top level directory)
  • Less less impact but i implemented: Avoid instantiating a feature rich class. Eg: zend_log: put a wrapper so that I only instantiates it if I use it – was a bit tricky since there are log filters too

i also got a significant speed improvement by moving my code under the apache root (rather than being on a different partition on my hard drive).

this got me down to about .6 - .7 sec.

now the "interesting" part: if i restart apache and run my page first, it runs fast (.4 sec). In fact, the Allen tutorial runs at .6 sec if it's loaded after my page. restart apache and run them in the different order and the speeds will be swapped... Something funky is going on (cache size?).

well, the code is fast enough to start development on top of it.

Future coding for performance:

  • Only use modules/building blocks that I really need. Fancy features will cost me time. eg: zend_config is powerful but: file read and parse were taking time, but even accessing the config parameters were taking significant time according to cachegrind.

looking at cachegrind via wincachegrind, i see that file loading is still taking a approx 1/3 of the total time!! this is even with opcode caching. Most of this is ZF file loading! i'll have to look at the best way to load files when i get to my production platform. This does suggest a possible performance improvement: have a "packed version" of the zend framework that combines classes into fewer files -- this would improve performance on every ZF script execution and be of no detriment!

3/22/08 update: see important performance notes about the Windows OS file system in my later post at http://beach-blogger.blogspot.com/2007/11/zend-framework-and-htmlquickformcontrol.html

About Me

I'm a web dude and this is my geek blog. There should be lots of content ... if I'm working hard ...