<?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>Web Mozarts &#187; Tips &amp; Tricks</title>
	<atom:link href="http://webmozarts.com/category/dev-tools/feed/" rel="self" type="application/rss+xml" />
	<link>http://webmozarts.com</link>
	<description>On The Art Of Web Development</description>
	<lastBuildDate>Tue, 07 Sep 2010 08:02:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Speedup: Profile your symfony app using Xdebug</title>
		<link>http://webmozarts.com/2009/05/01/speedup-performance-profiling-for-your-symfony-app/</link>
		<comments>http://webmozarts.com/2009/05/01/speedup-performance-profiling-for-your-symfony-app/#comments</comments>
		<pubDate>Fri, 01 May 2009 10:17:09 +0000</pubDate>
		<dc:creator>Klemens</dc:creator>
				<category><![CDATA[Tips & Tricks]]></category>

		<guid isPermaLink="false">http://webmozarts.com/?p=276</guid>
		<description><![CDATA[Hi! A warm welcome to Web Mozarts also from my side. It took some time to write my first post, but here it is! Your app is slow? If the symfony web debug toolbar doesn&#8217;t give you the details you want, you can take a real deep look into your application using the profiling options [...]
No related posts.]]></description>
			<content:encoded><![CDATA[<p>Hi! A warm welcome to Web Mozarts also from my side. It took some time to write my first post, but here it is!</p>
<p>Your app is slow? If the symfony <a href="http://www.symfony-project.org/book/1_2/16-Application-Management-Tools#chapter_16_sub_web_debug_toolbar" title="symfony web debug toolbar" target="_blank" class="liexternal">web debug</a> toolbar doesn&#8217;t give you the details you want, you can take a real deep look into your application using the profiling options of <a href="http://www.xdebug.org/" title="Xdebug homepage" target="_blank" class="liexternal">Xdebug</a>.</p>
<p>In this tutorial we will set up Xdebug to profile your application and then we&#8217;ll analyse the output with <a href="http://kcachegrind.sourceforge.net/html/Home.html" target="_blank" class="liexternal">KCachegrind</a>.</p>
<p class="note">By the way: Xdebug has a lot of other useful features. One that comes automatically is the nicely formated php debugging output in case of errors including the full call stack. KCachegrind on the other hand has very interesting graphical output features like the call graph. In the case of symfony the call graph can be like an interesting expedition into the functionality of the framework.</p>
<p><span id="more-276"></span></p>
<h3>Installation</h3>
<p>We&#8217;ll do the installation using Ubuntu Linux, but it works similar for other unixes or even Windows with a WAMP stack. Ah yes &#8211; we assume you have the correct environment for symfony installed (Apache, PHP5, &#8230;).</p>
<p class="note">KCachegrind is not available for Windows. You can use <a href="http://sourceforge.net/projects/wincachegrind/" target="_blank" class="liexternal">WinCacheGrind</a> instead although it doesn&#8217;t provide the graphical goodies.</p>
<p>Download and install:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">sudo aptitude install php5<span style="color: #339933;">-</span>xdebug kcachegrind</pre></div></div>

<p>Ubuntu automatically inserts the Xdebug module in the php ini files:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># /etc/php5/conf.d/xdebug.ini
</span>zend_extension<span style="color: #339933;">=/</span>usr<span style="color: #339933;">/</span>lib<span style="color: #339933;">/</span>php5<span style="color: #339933;">/</span><span style="color: #cc66cc;">20060613</span><span style="color: #339933;">+</span>lfs<span style="color: #339933;">/</span>xdebug<span style="color: #339933;">.</span>so</pre></div></div>

<p>Now let&#8217;s restart apache to load the new configuration:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">sudo apache2ctl restart</pre></div></div>

<p>Let&#8217;s provoke an error and let us see if Xdebug works. I put an invalid function call into a php script:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// apps/frontend/modules/myModule/actions.class.php</span>
<span style="color: #000000; font-weight: bold;">class</span> myModuleActions <span style="color: #000000; font-weight: bold;">extends</span> sfActions
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> executeIndex<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    invalid_function<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now we get the typical Xdebug output including the call stack in the browser, so we know it works:<br />
<a href="http://webmozarts.com/wp-content/uploads/2009/04/xdebug_trace.png" class="liimagelink"><img class="alignnone size-medium wp-image-296" src="http://webmozarts.com/wp-content/uploads/2009/04/xdebug_trace-300x215.png" alt="xdebug_trace" width="300" height="215" /></a></p>
<h3>Enable profiling</h3>
<p>The profiling capability of Xdebug has to be explictly enabled because it produces A LOT of output and slows down the application serverely.</p>
<p>If you use .htaccess for your symfony project this is the easiest way to enable the profiling:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># web/.htaccess
</span>
<span style="color: #666666; font-style: italic;"># append at the end of the file:
</span>php_value xdebug<span style="color: #339933;">.</span>profiler_enable <span style="color: #cc66cc;">1</span>
php_value xdebug<span style="color: #339933;">.</span>profiler_output_dir <span style="color: #339933;">/</span>tmp</pre></div></div>

<p>If you don&#8217;t use .htaccess, or if you want to profile a cli php script, you have to enable the profiling in your php.ini (cli stands for command line interface). Example for the cli:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># /etc/php5/cli/conf.d/xdebug.ini
</span>
<span style="color: #666666; font-style: italic;"># append at the end of the file:
</span>xdebug<span style="color: #339933;">.</span>profiler_output_dir <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;/tmp&quot;</span>
xdebug<span style="color: #339933;">.</span>profiler_enable <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span></pre></div></div>

<p>Don&#8217;t forget to restart apache if you changed the php.ini config!</p>
<h3>Data galore&#8230;</h3>
<p>Ok, let&#8217;s profile something! Just fire up your webbrowser and load any page of your local symfony project.<br />
Xdebug now creates a &#8220;cachegrind&#8221; file like &#8220;cachegrind.out.22076 in your /tmp/ directory.</p>
<p>Here&#8217;s how a cachgrind file looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">version<span style="color: #339933;">:</span> 0<span style="color: #339933;">.</span>9<span style="color: #339933;">.</span>6
cmd<span style="color: #339933;">:</span> <span style="color: #339933;">/</span><span style="color: #000000; font-weight: bold;">var</span><span style="color: #339933;">/</span>www<span style="color: #339933;">/</span>ullright<span style="color: #339933;">/</span>web<span style="color: #339933;">/</span>index<span style="color: #339933;">.</span>php
part<span style="color: #339933;">:</span> <span style="color: #cc66cc;">1</span>
&nbsp;
events<span style="color: #339933;">:</span> <span style="color: #990000;">Time</span>
&nbsp;
fl<span style="color: #339933;">=</span>php<span style="color: #339933;">:</span>internal
fn<span style="color: #339933;">=</span>php<span style="color: #339933;">::</span><span style="color: #990000;">realpath</span>
<span style="color: #cc66cc;">38</span> <span style="color: #cc66cc;">98</span>
&nbsp;
fl<span style="color: #339933;">=/</span><span style="color: #000000; font-weight: bold;">var</span><span style="color: #339933;">/</span>www<span style="color: #339933;">/</span>ullright<span style="color: #339933;">/</span>plugins<span style="color: #339933;">/</span>ullCorePlugin<span style="color: #339933;">/</span>lib<span style="color: #339933;">/</span>vendor<span style="color: #339933;">/</span>symfony<span style="color: #339933;">/</span>lib<span style="color: #339933;">/</span>autoload<span style="color: #339933;">/</span>sfCoreAutoload<span style="color: #339933;">.</span><span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">.</span>php
fn<span style="color: #339933;">=</span>sfCoreAutoload<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>__construct
<span style="color: #cc66cc;">50</span> <span style="color: #cc66cc;">93</span>
cfn<span style="color: #339933;">=</span>php<span style="color: #339933;">::</span><span style="color: #990000;">dirname</span>
calls<span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">0</span>
<span style="color: #cc66cc;">38</span> <span style="color: #cc66cc;">2</span>
cfn<span style="color: #339933;">=</span>php<span style="color: #339933;">::</span><span style="color: #990000;">realpath</span>
calls<span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">0</span>
<span style="color: #cc66cc;">38</span> <span style="color: #cc66cc;">98</span></pre></div></div>

<p>It basically records which function was called from which script file, how many times it was called and how long the execution took.</p>
<p>Now that isn&#8217;t really human readable, is it?</p>
<p>So let&#8217;s open the cachegrind file with KCachegrind. It looks something like this:</p>
<p><a href="http://webmozarts.com/wp-content/uploads/2009/05/kcachegrind-overview.png" class="liimagelink"><img class="alignnone size-medium wp-image-309" src="http://webmozarts.com/wp-content/uploads/2009/05/kcachegrind-overview-300x187.png" alt="kcachegrind-overview" width="300" height="187" /></a></p>
<p>So what have we got here? On the left side you see the &#8220;Flat Profile&#8221;. A list of all function and method calls. From left to right:</p>
<ul>
<li>Incl. &#8211; The cost of the function including all child functions</li>
<li>Self &#8211; The cost of the function itself</li>
<li>Called &#8211; How many times a function was called</li>
<li>Function &#8211; Which function</li>
<li>Location &#8211; Script file</li>
</ul>
<p>A good starting point is to click on &#8220;Called&#8221; to see which functions are called the most often. It&#8217;s also interesting to order by &#8220;Self&#8221; to see which method itself took the most time.</p>
<p>On the right side I selected the tab &#8220;Call Graph&#8221; which shows the sequence of the function calls starting with &#8220;{main}&#8221;. For symfony it&#8217;s usually &#8220;web/index.php&#8221;.</p>
<p>The number of nodes in the call graph is limited by the relative percentage of the execution time. Per default only functions that take more than 5% of the execution time are displayed. Get a more detailed graph by right clicking into the call graph and selecting &#8220;Graph -&gt; Min. node cost -&gt; 1%&#8221;. You can also doube click on any node to see a more detailed child-graph.</p>
<p><a href="http://webmozarts.com/wp-content/uploads/2009/05/kcachegrind-doctrine.png" class="liimagelink"><img class="alignnone size-medium wp-image-314" src="http://webmozarts.com/wp-content/uploads/2009/05/kcachegrind-doctrine-300x187.png" alt="kcachegrind-doctrine" width="300" height="187" /></a></p>
<p>Here is a nice detailed graph showing the internals of a doctrine query.  I&#8217;d like to invite you to explore your symfony app using the call graph &#8211; it surely is an interesting journey and you can learn a lot about the architecture of symfony.</p>
<p>Coming to an end I&#8217;d like to hear about your experiences and findings about performance profiling:</p>
<ul>
<li>What tools do you use?</li>
<li>What are your best practices?</li>
</ul>
<p>Looking forward to interesting discussions &#8211; have a nice day!</p>
<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://webmozarts.com/2009/05/01/speedup-performance-profiling-for-your-symfony-app/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

