PHP Performance Tuning
Sign in

PHP Performance Tuning

Software engg
See interview of Rajan  Singh

PHP Performance Tuning

There are many methods available to help you performance tune PHP.Some require little to no effort, while others take more time inanalysis for potential opportunities.

Opcode Caching

The number one item to start tuning is to ensure that you are usingan opcode cache. Opcode caching will cache and optimize PHPintermediate code, of which will give you a very large performance gainin comparison to straight PHP.

Your options here include APC, XCache, EAccelerator and Zend Platform. I recommend APC and XCache, you decide.

APC File Priming

If you are utilizing APC a great way during a release is to primethe file which will store the file in the bytecode cache bypassing allof the filters. This is done by running each file through apc_compile_file.Now you can create your cache quickly and effectively. Here is anexample PHP file to run this (you have to utilize apc.enable_cli=1 ifyou want to make this run from the CLI). The script below doesn’tcontain much checking on your directory so you may want to add that inif you’d like.

 


 

Includes

The number of files you include can certainly limit yourperformance. While this is minimal, when you start to include a largenumber of files this starts to gain. When you are running a page,remember, that every time your page runs it has to include those files.If you are including a large number of files that you typically utilizein a bootstrap, merge those files at deploy time to create a singleinclude file that is used through your bootstrap. Please note, that Iam not stating to do this in development as that could lead to amaintenance nightmare but creating a release process where you would dothis before deployment.

Avoid Loops when Possible

Loops can be expensive especially the more operations that you runin that loop. For instance take the following scenario where you needto retrieve a set of integer values from the browser that correspondwith a particular record in that database and then output the row.

Poor Performance Looping Example

 

 
High Performance Looping Example

 

 

The examples above simulate a common mistake you see often in code.The first example shows us executing the same query per every ID wherein the second we only use one query. This cuts down considerably in thetime it takes. To generate the below I passed 15 id’s to hit a singletable that had only 40 records (6 id’s did not actually exist).

Number of IDS: 15
The Bad Way: 0.0044221878051758 seconds
The Good Way: 0.0011670589447021 seconds

Add on quite a bit more id values and a few joins in there and you have yourself a bottleneck building up.

memcache

memcache is a distributed memory object caching system to which we have a memcache extensionin PHP. You can utilize memcache to store certain aspects of your datathat is utilized often. Typically, you will store database resultswithin memcache for quick access to ever changing content. If you arethinking about the query caching that RDBMS especially with MySQL 4.xread the section on memcaches front page.

Database Connections

Many applications connect to the database starting in the main file.You should not be doing this as the initial connection to the databasestill takes time. While it may be minor if you have quite a bit ofcontent that you do not need to connect to the database you are savingyour server precious resources and your database from havingconnections that do nothing. One way to implement a change like this isto use or create an abstraction layer modifying it to only connect onthe first command sent to the database.

RDBMS Performance Tuning

I am only going to say a few pointers here as each RDBMSimplementation varies but there is general information that you shouldtend to follow.

Ensure Queries are using Indexes

This is the number one reason for poor performance on a databasebesides terrible data models is that of poor indexes. You should behitting your indexes with just about every query. Check your explainplan, ensure that it is using the proper indexes and if they are notadjust it to ensure the speed is going to be adequate.

Further, ensure that you have enough memory to handle large indexes and more than just one at a time.

Use Less Joins

The more joins you have to make the harder you are making thedatabase work. This slows the query down from being extremely fast toslowly taking on a load. You shouldn’t be joining 10 tables together ona page that is utilized a mass amount of the time.

Query Caching

Make sure to make usage of the query caching on your database thiswill increase performance for your queries that are run more often.

Archive, Archive, Archive

Archiving old data is a huge thing to make use of. Especially intables that are ever growing and are getting into the millions ofrecords. Archiving data that is used less often will speed up yourquery time since it will reduce the amount that is stored in theindexes as well as limiting the number of rows you are potentiallyscanning through.

Many larger companies when archiving old data will produce aggregatestatistics on this archived data if it is going to be of use for thecustomer, client, etc.

HTTP Performance Tuning

As with RDBMS there are multiple web servers so here I will simplygive some brief tips considering a few different areas. If you arelooking for specific tuning such as apache there are articles scatteredthroughout the web and information on the Apache website to give you more details.

Content Compression

Utilizing GZIP to send your files to the browser you can effectivelyslim the file sizes down cutting down on your bandwidth, the amount oftime the server needs to be connected with the user and increase theuser experience by faster loading times.

Combining Files

To cut down on the amount of requests your web server needs tohandle you can effectively combine most used CSS files and JavaScriptfiles. Here you take all of the CSS files you utilize combine them andstrip out the whitespace. This will save the user time when downloadingthe CSS file as well as saving you bandwidth. Do the same with yourJavaScript files thus cutting down wasted server resources, bandwidth,etc.

CSS Sprites

CSS Sprites is the process of taking an image that you would havespliced or a set of icons and creating a single image to then use CSSto apply showing the correct image. Take a look at the CSS Sprites article on A List Apart.

Utilize Not Modified Headers

If your content has not changed, you should be sending not modifiedheaders and/or last modified date headers. You want the browser tocache information that isn’t changing so that they do not waste yourresources. A browser will only look at the headers if the user has acache of the content and it has not been modified saving you bandwidth,server resources and the user time.

Limit Modules

The more modules you utilize the more memory and resources your webserver will take up. If you do not use a module, disable it or removeit. Secondly a technique to perform better is to have 2 web servers,one for your dynamic content and one for the static content so thatthere is no overhead of loading PHP with the web server for eachinstance.

exit(0);

Now as I stated this was a very simplistic overview of some performance tuning options and does not take it to the nthdegree of what you can do as there is always more performance tuningthat can be done. Further there are many tools to benchmark the impactthat each of these are making on your site but that is out of the scopeof this post.

prevnew
start_blog_img