PHP Performance Crash Course, Part 1: The Basics
Join the DZone community and get the full member experience.Join For Free
we all know performance is important, but performance tuning is too often an afterthought. as a result, taking on a performance tuning project for a slow application can be pretty intimidating – where do you even begin? in this series i’ll tell you about the strategies and technologies that (in my experience) have been the most successful in improving php performance. to start off, however, we’ll talk about some of the easy wins in php performance tuning. these are the things you can do that’ll get you the most performance bang for your buck, and you should be sure you’ve checked off all of them before you take on any of the more complex stuff.
why does performance matter?
the simple truth is that application performance has a direct impact on your bottom line:
follow these simple best practices to start improving php performance:
one of the easiest improvements you can make to improve performance and stability is to upgrade your version of php. php 5.3.x was released in 2009. if you haven’t migrated to php 5.4, now is the time! not only do you benefit from bug fixes and new features, but you will also see faster response times immediately. see php.net to get started.
- installing the latest php on linux
- installing the latest php on osx
- installing the latest php on windows
once you’ve finished upgrading php, be sure to disable any unused extensions in production such as xdebug or xhprof.
use an opcode cache
php is an interpreted language, which means that every time a php page is requested, the server will interpet the php file and compile it into something the machine can understand (opcode). opcode caches preserve this generated code in a cache so that it will only need to be interpreted on the first request. if you aren’t using an opcode cache you’re missing out on a very easy performance gain. pick your flavor: apc , zend optimizer , xcache , or eaccellerator . i highly recommend apc , written by the creator of php, rasmus lerdorf .
many developers writing object-oriented applications create one php source file per class definition. one of the biggest annoyances in writing php is having to write a long list of needed includes at the beginning of each script (one for each class). php re-evaluates these require/include expressions over and over during the evaluation period each time a file containing one or more of these expressions is loaded into the runtime. using an autoloader will enable you to remove all of your require/include statements and benefit from a performance improvement. you can even cache the class map of your autoloader in apc for a small performance improvement.
optimize your sessions
while http is stateless, most real life web applications require a way to manage user data. in php, application state is managed via sessions . the default configuration for php is to persist session data to disk. this is extremely slow and not scalable beyond a single server. a better solution is to store your session data in a database and front with an lru (least recently used) cache with memcached or redis . if you are super smart you will realize you should limit your session data size ( 4096 bytes ) and store all session data in a signed or encrypted cookie.
- encrypting php sessions with suhosin
- storing sessions in memcached
- using a database to persist sessions
use a distributed data cache
applications usually require data. data is usually structured and organized in a database. depending on the data set and how it is accessed it can be expensive to query. an easy solution is to cache the result of the first query in a data cache like memcached or redis . if the data changes, you invalidate the cache and make another sql query to get the updated result set from the database.
there are many use cases for a distributed data cache from caching web service responses and app configurations to entire rendered pages.
do blocking work in the background
often times web applications have to run tasks that can take a while to complete. in most cases there is no good reason to force the end-user to have to wait for the job to finish. the solution is to queue blocking work to run in background jobs. background jobs are jobs that are executed outside the main flow of your program, and usually handled by a queue or message system. there are a lot of great solutions that can help solve running backgrounds jobs. the benefits come in terms of both end-user experience and scaling by writing and processing long running jobs from a queue. i am a big fan of resque for php that is a simple toolkit for running tasks from queues. there are a variety of tools that provide queuing or messaging systems that work well with php:
i highly recommend wan qi chen’s excellent blog post series about getting started with background jobs and resque for php .
leverage http caching
http caching is one of the most misunderstood technologies on the internet. go read the http caching specification . don’t worry, i’ll wait. seriously, go do it! they solved all of these caching design problems a few decades ago. it boils down to expiration or invalidation and when used properly can save your app servers a lot of load. please read the excellent http caching guide from mark nottingam . i highly recommend using varnish as a reverse proxy cache to alleviate load on your app servers.
optimize your favorite framework
deep diving into the specifics of optimizing each framework is outside of the scope of this post, but these principles apply to every framework:
- stay up-to-date with the latest stable version of your favorite framework
- disable features you are not using (i18n, security, etc)
- enable caching features for view and result set caching
learn to how to profile code for php performance
xdebug is a php extension for powerful debugging. it supports stack and function traces, profiling information and memory allocation and script execution analysis. it allows developers to easily profile php code.
webgrind is an xdebug profiling web frontend in php5. it implements a subset of the features of kcachegrind and installs in seconds and works on all platforms. for quick-and-dirty optimizations it does the job. here’s a screenshot showing the output from profiling:
xhprof is a function-level hierarchical profiler for php with a reporting and ui layer. xhprof is capable of reporting function-level inclusive and exclusive wall times, memory usage, cpu times and number of calls for each function. additionally, it supports the ability to compare two runs (hierarchical diff reports) or aggregate results from multiple runs.
appdynamics is application performance management software designed to help dev and ops troubleshoot problems in complex production apps.
get started with appdynamics today and get in-depth analysis of your applications performance.
php application performance is only part of the battle
now that you have optimized the server-side, you can spend time improving the client side! in modern web applications most of the end-user experience time is spent waiting on the client side to render. google has dedicated many resources to helping developers improve client side performance.
see us live!
Published at DZone with permission of Dustin Whittle, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.