<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Joseph Keeler &#187; PHP Performance</title>
	<atom:link href="http://josephkeeler.com/category/php-performance/feed/" rel="self" type="application/rss+xml" />
	<link>http://josephkeeler.com</link>
	<description>PHP/LAMP Development and Software Process Improvement</description>
	<lastBuildDate>Thu, 05 Nov 2009 15:11:04 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>PHP Performance &#8211; Optimize, Eliminate or Scale</title>
		<link>http://josephkeeler.com/2009/05/php-performance-optimize-eliminate-or-scale/</link>
		<comments>http://josephkeeler.com/2009/05/php-performance-optimize-eliminate-or-scale/#comments</comments>
		<pubDate>Thu, 14 May 2009 22:07:07 +0000</pubDate>
		<dc:creator>Joseph</dc:creator>
				<category><![CDATA[PHP Performance]]></category>

		<guid isPermaLink="false">http://josephkeeler.com/?p=59</guid>
		<description><![CDATA[Get several more uses from your toothpaste by placing the tube in boiling water for several minutes to loosen up the toothpaste inside, then pulling the tube down across the edge of the bathroom counter to scrape all of the toothpaste from the bottom of the tube to the top.
Once this technique no longer yields more toothpaste from the tube, use [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p>Get several more uses from your toothpaste by placing the tube in boiling water for several minutes to loosen up the toothpaste inside, then pulling the tube down across the edge of the bathroom counter to scrape all of the toothpaste from the bottom of the tube to the top.</p>
<p>Once this technique no longer yields more toothpaste from the tube, use a pair of scissors to cut the tube open about an inch or two from the top. Open the top part and use your toothbrush to scrape the toothpaste out of the inside of the tube.</p></blockquote>
<p>-Associated Content</p>
<p>When it comes to improving performance in a PHP system, there are a few different approaches you can take.  First, find out where your system performance can best be improved &#8211; see <a href="http://josephkeeler.com/2009/05/php-optimization-where-to-start/" target="_self">PHP Optimization &#8211; Where to Start</a>.  Once you know where your biggest bottlenecks are, you can try to optimize through them, but at some point, you&#8217;ll reach the limits of what optimization can do for you &#8211;  run out of toothpaste, so to speak.  There are two widely used performance techniques beyond optimization &#8211; eliminate it, or scale it out.</p>
<p><span id="more-59"></span></p>
<p> </p>
<h2>Optimize</h2>
<p>Generally speaking, optimizing is getting the system to do the same thing, only faster. Optimization techniques include things like tuning the server configuration for optimal performance, refactoring the code to get better performance from PHP, Opcode caching or adding indexes to the database tables for faster reads. Optimizing is usually one of the first tools you&#8217;ll be pulling out of your bag to deal with performance issues.  But there are limits as to how far optimization will take you.</p>
<p>With optimization, there is the law of diminishing returns.  Your first pass at optimization may yield a 600%-800% performance improvement.  Maybe your next one will give you a 200% improvement.  The next one may still give you a 100% improvement.  With each pass, finding ways to get those big performance gains will become increasingly difficult.  At some point, you&#8217;ll be struggling to get a 5% performance boost.  Or maybe you&#8217;ll find a way to increase performance, but it will also mean the code will become unmaintainable (Even if your one supergenius coder on the team can work with it, if it&#8217;s so confusing no other programmer can, it&#8217;s unmaintainable.  More on that later).</p>
<p>At some point, it&#8217;s not going to be worth trying to continue down this route as your performance needs increase.  I don&#8217;t boil my toothpaste and split it open to get every last drop, I go buy a new tube of toothpaste.  Similarly, at some point, even if I know that there are still probably some performance improvements somewhere still be in the system, I&#8217;ll start looking for something other than optimization to improve system performance.  This is where elimination and scaling come into play.</p>
<p> </p>
<h2>Eliminate</h2>
<p>Elimination is getting rid of your bottlenecks by finding a way to not have to execute them.  There are several different ways to do this.  Caching and using background process are commonly used ones.  Caching is probably the most common elimination technique &#8211; if there is data that does not need to be updated with every run, saving and reusing the processed information will save you from having to process it each time.  </p>
<p>Another common elimination technique is to take the heavy lifting off of the PHP loader and put it into separate running background process.  If there is a &#8220;heavy&#8221; process that doesn&#8217;t need to be real time, just close to real time, maybe it would make sense to have a cron job do the processing of this information.  The web user would then just look at this pre-processed information instead of processing every time.  This works two ways too &#8211; If a user uploads data, instead of waiting for it to be processed in real-time, maybe you could just hold it in a queue and tell them that it will be processed later.</p>
<p> </p>
<h2>Scale</h2>
<p>Even with using optimization and elimination, or if you need to deal with real-time processes where elimination isn&#8217;t an option, you&#8217;ll reach some limits and need to be able to scale your PHP system to get better performance.  There&#8217;s &#8220;vertical scaling,&#8221; which means doing something like buying a 48-core server with a terrabyte of RAM; kind of an expensive route.  And then there&#8217;s horizontal scaling, which is setting up your system so that it can be split across multiple pieces of hardware to distribute the load.  This same technique can also be used to give you highly available redundant systems.</p>
<p>Designing a system to horizontally scale typically requires the most work and has the most complexity of any of these three routes. And for many PHP systems, it&#8217;s a necessary route to take somewhere in the system.  Deciding what needs to be able to horizontally scale is extremely situationally dependent.  And with the added complexity to the development and the extra development time and costs that comes with this, designing to scale in situations that don&#8217;t need it is a very poor use of resources.</p>
<p> </p>
<h2>Summing it up</h2>
<p>When it comes to improving PHP performance, there are many approaches that you can take, with no blanket solutions that will solve your problems in every situation.  With each option, there are pros and cons to consider that will be impacted by many factors, anything from the deployment to the makeup of team that&#8217;s working on it.  Having a big bag of tools to approach performance issues is necessary, especially for those times you can&#8217;t rely on being able to boil out a little more toothpaste.</p>
]]></content:encoded>
			<wfw:commentRss>http://josephkeeler.com/2009/05/php-performance-optimize-eliminate-or-scale/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP Optimization &#8211; Where to start</title>
		<link>http://josephkeeler.com/2009/05/php-optimization-where-to-start/</link>
		<comments>http://josephkeeler.com/2009/05/php-optimization-where-to-start/#comments</comments>
		<pubDate>Tue, 12 May 2009 01:19:37 +0000</pubDate>
		<dc:creator>Joseph</dc:creator>
				<category><![CDATA[PHP Performance]]></category>

		<guid isPermaLink="false">http://josephkeeler.com/?p=20</guid>
		<description><![CDATA[“Our web app is slow,” complained the boss.
“Well, I hear that using commas instead of periods with echo() makes PHP faster,” replied the code monkey.
“Well, let’s try optimizing that,” said the boss.
$100k later.
“Well, it’s still slow after that change.  I guess we’ve reached the limits of PHP.  Time to switch to Java.”
If you [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p>“Our web app is slow,” complained the boss.<br />
“Well, I hear that using commas instead of periods with echo() makes PHP faster,” replied the code monkey.<br />
“Well, let’s try optimizing that,” said the boss.</p>
<p>$100k later.<br />
“Well, it’s still slow after that change.  I guess we’ve reached the limits of PHP.  Time to switch to Java.”</p></blockquote>
<p>If you run a website that becomes successful, at some point, you’re going to run into performance issues.  Heck, you may only need a single visitor with some scripts to bring it to a crawl.  There are a few different approaches often used for optimizing PHP, but really only one right one.  First there’s the old school way, dump in a bunch of var_dumps of script execution time and hope to find a slow spot.  Not very efficient.</p>
<p>Then there’s what I like to call the cluster-bomb optimizing approach.  These are those lists of 40 things to do to optimize PHP and the like.  I call this the cluster-bomb approach because you’re just throwing a bunch of tiny optimizations all over the place hoping that you hit one that’s causing your performance issues.  A lot of these recommendations will leave you with a trashed collection of unmaintainable code.  A cluster-f*** of code so to speak.</p>
<p>Very rarely will the majority of these recommendations actually make an impact to a real-world performance issue.  I know this because I regularly fix real-world performance issues, and I rarely follow most of the optimization “tips” in those lists.</p>
<p>And then there’s the profiling method.  This I think is the only right way to begin any sort of optimization process, in PHP or any other language.  For the uninitiated, a profiler is a process that will sit with your script handler and generate a detailed log of exactly what happened during a script run, how long a subprocess took, etc.  You can then take this log and get a visual representation of your scripts performance.  From there, it’s really easy to see where the performance bottlenecks are in the system, and give you a good idea of where the best effort could be put in optimization.</p>
<p><span id="more-20"></span></p>
<p>For PHP optimization and profiling, the tools that I like to use are <a href="http://www.xdebug.org/" target="_blank">Xdebug</a> and <a href="http://kcachegrind.sourceforge.net/html/Home.html" target="_blank">KCacheGrind</a>.  Xdebug is the profiler, and KCacheGrind is the graphical reader of the profiler.  First, install Xdebug on your PHP server.  You’ll need to be able to generate a profiler log.  You can do this by either enabling it in your core php.ini file, activating it from within your script, or, my preference, turning on the xdebug.profiler_enable_trigger setting in your php.ini so you just have to add XDEBUG_PROFILE=1 as a parameter in your URL or POST parameters, and you can generate the profiler files easily on demand.</p>
<p>Once you’ve generated the profiler log, open it up in KCacheGrind.  This is a very powerful tool that I won’t be covering very in depth, but it does make finding problem spots in the code very simple.  Using the Callee Map tab on the right side of the screen, you will see a visual representation of how long the calls in your script were for each run.  The larger the box, the more time relative to the other process was spent in that process.  By clicking on a box, you can see a stack trace of where that specific box is.</p>
<p>The visual profile of a complex OO script may look something like this</p>
<div id="attachment_21" class="wp-caption alignnone" style="width: 510px"><img class="size-full wp-image-21" title="KCacheGrind Dump" src="http://www.josephkeeler.com/wp-content/uploads/2009/05/better_profile.jpg" alt="KCacheGrind Dump" width="500" height="334" /><p class="wp-caption-text">KCacheGrind Dump</p></div>
<p>The largest block there represents 17% of the run time of that script.  If I wanted to improve performance of this script, I may want to look there.  Or maybe this tells me that I need to optimize system performance by looking at something other than script internals. Or maybe I need to look at ways to reduce the number of calls in the script run.  Each situation has its own unique requirements, and there is often more than one good approach to solving the problem.</p>
<p>If you have a slow performing script with a bottleneck, you may see something more like this.</p>
<div id="attachment_22" class="wp-caption alignnone" style="width: 510px"><img class="size-full wp-image-22" title="KCachegrind dump of bottlenecked PHP script" src="http://www.josephkeeler.com/wp-content/uploads/2009/05/bottleneck_profile.jpg" alt="KCachegrind dump of bottlenecked PHP script" width="500" height="309" /><p class="wp-caption-text">KCachegrind dump of bottlenecked PHP script</p></div>
<p>The two largest boxes there represent 80% of the total script run time.  This is definitely where the script bottleneck is, and performance efforts would be best directed. Directing any effort anywhere else wouldn&#8217;t have any significant impact as it represents &lt;20% of the script run.  In this particular case, the two boxes were a couple of sloppy SQL queries (something, btw, which isn’t even on the 40 ways to optimize PHP list).  But by cleaning those up, I was able to easily do a 70% performance improvement to the script run time.</p>
<p>Now, a code bottleneck is certainly not going to be the cause of every problem, but it will be found with the majority of performance issue you’ll come across.  And each bottleneck needs to be treated differently, more to come on that later. But by using the right tools and taking the few minutes to analyze your script, you&#8217;ll quickly gain excellent insight into how to best direct your optimization efforts.</p>
<p>Using these tools to find where to optimize, I was able to reduce script run time by 70% in about 20 minutes.  Let’s see you do that by swapping out your double quotes for single quotes (#28).</p>
]]></content:encoded>
			<wfw:commentRss>http://josephkeeler.com/2009/05/php-optimization-where-to-start/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
