<?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>Christian Schenk&#187; profiling</title>
	<atom:link href="http://www.christianschenk.org/blog/tag/profiling/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.christianschenk.org</link>
	<description>Writing about my experiences with technology and all different kinds of projects and experiments</description>
	<lastBuildDate>Sun, 04 Dec 2011 23:43:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Profiling with CPU-Sampler</title>
		<link>http://www.christianschenk.org/blog/profiling-cpu-sampler/</link>
		<comments>http://www.christianschenk.org/blog/profiling-cpu-sampler/#comments</comments>
		<pubDate>Wed, 13 Jan 2010 10:12:40 +0000</pubDate>
		<dc:creator>Christian Schenk</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[apple]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[cpu sampler]]></category>
		<category><![CDATA[opengl]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[profiling]]></category>
		<category><![CDATA[xcode]]></category>

		<guid isPermaLink="false">http://www.christianschenk.org/?p=734</guid>
		<description><![CDATA[Apple&#8217;s Xcode comes with a neat profiling tool called CPU Sampler. It helps you identifying time consuming code blocks in your software and is really handy if you need to optimize for optimal performance. Just recently I tried rewriting OpenGL&#8217;s routines that handle matrix calculations as part of a lecture at the university. Pretty soon [...]]]></description>
			<content:encoded><![CDATA[<p>Apple&#8217;s Xcode comes with a neat profiling tool called <em>CPU Sampler</em>. It helps you identifying time consuming code blocks in your software and is really handy if you need to optimize for optimal performance.</p>
<p>Just recently I tried rewriting OpenGL&#8217;s routines that handle matrix calculations as part of a lecture at the university. Pretty soon I came to the conclusion, that I had to optimize my code if I wanted to compete with the implementation provided by OpenGL.</p>
<p>This post presents at quick look at <em>CPU Sampler</em> which helped me making well-founded decisions resulting in a faster implementation.</p>
<p><span id="more-734"></span></p>
<h2>Howto</h2>
<p>Once you&#8217;ve developed your software with Xcode and want to profile it using <em>CPU Sampler</em>, all you have to do is to click on Run &#8211; Run with Performance Tool &#8211; CPU Sampler. This will start the tool and run your application; you can start and stop the recording as you see fit.</p>
<p>As soon as you end your application you can have a look at the samples collected during the last run. This is the interesting part because you can review how long a certain function was running and the percentage tells you how long this took compared to the overall run.</p>
<p>Try playing around with the various options. In most situations you can stick with the defaults but there&#8217;re more interesting things to look at if you want to. For example try changing the <em>Active thread</em> from <em>All threads</em> to something else and the view will show the corresponding samples only.</p>
<h2>Example</h2>
<p>As I said, I tried rewriting OpenGL&#8217;s routines that handle matrix calculations. This was meant as an exercise to understand the operations needed that changed the matrices. Of course, the implementation was way slower than OpenGL and I should try speeding it up.</p>
<p>Using <em>CPU Sampler</em> I immediately identified a bottleneck and a major difference between my implementation and the one that comes with OpenGL. The slow part of my code could be found in repeated calls to <code>cos</code> and <code>sin</code>. Since OpenGL seems to work with the libraries of my graphics card directly I guess it tries to do certain computations there.</p>
<p>What I now did was substituting the calls to <code>cos</code> and <code>sin</code> with code that used a lookup table to retrieve precalculated values. This resulted in a major performance boost &#8211; still, OpenGL&#8217;s code is twice as fast as my one.</p>
<p>Have a look at the following screenshots. They show a run using OpenGL&#8217;s implementation of matrix manipulations, my own and an optimized version of my own implementation. Comparing the naive and optimized version of my code clearly shows that the optimized variant doesn&#8217;t spent much time calculating <code>cos</code> and <code>sin</code>.</p>
<div style="white-space:nowrap">
<a href="/wp-content/uploads/opengl.png" title="OpenGL" class="thickbox" rel="gallery-opengl"><img src="/wp-content/uploads/opengl-150x150.png" alt="OpenGL" /></a><a href="/wp-content/uploads/our_impl.png" title="Own implementation" class="thickbox" rel="gallery-opengl"><img src="/wp-content/uploads/our_impl-150x150.png" alt="Own implementation" /></a><a href="/wp-content/uploads/our_impl_opt.png" title="Own implementation optimized" class="thickbox" rel="gallery-opengl"><img src="/wp-content/uploads/our_impl_opt-150x150.png" alt="Own implementation optimized" /></a>
</div>
<h2>Conclusion</h2>
<p>I wanted to show that using a profiling tool like <em>CPU Sampler</em> that comes with Apple&#8217;s Xcode may make your life much easier if you&#8217;re trying to optimize your application. This helps you to make wise decisions when it comes to optimizing code so you don&#8217;t have to obfuscate your software with hard to read code blocks that have a very little effect regarding performance.</p>
<p>It&#8217;s nice to see that Apple ships tools like this with its IDE. Have a look at the other performance tools and you&#8217;ll be amazed how many cools things Xcode is capable of &#8211; things you wouldn&#8217;t even dream about in other IDE&#8217;s.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.christianschenk.org/blog/profiling-cpu-sampler/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Profiling with AspectJ</title>
		<link>http://www.christianschenk.org/blog/profiling-with-aspectj/</link>
		<comments>http://www.christianschenk.org/blog/profiling-with-aspectj/#comments</comments>
		<pubDate>Thu, 09 Aug 2007 10:22:23 +0000</pubDate>
		<dc:creator>Christian Schenk</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[aop]]></category>
		<category><![CDATA[aspectj]]></category>
		<category><![CDATA[HowTo]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[profiling]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.christianschenk.org/blog/profiling-with-aspectj/</guid>
		<description><![CDATA[This post shows how to profile applications easily with AspectJ]]></description>
			<content:encoded><![CDATA[<p>Lately, I read a <a href="http://www.bibsonomy.org/bibtex/24354d3b3d13708590824241967d18cea/cschenk">paper</a> about profiling with <a title="AspectJ Project" href="http://www.eclipse.org/aspectj/">AspectJ</a>: it investigates how to profile heap usage, object lifetime, wasted time and time spent. I was wondering how difficult it would be to write my own simple profiler; and as we&#8217;ll see it&#8217;s easy and fun.</p>
<p>If you&#8217;d like to check out the sample code that accompanies this post, you can download the Eclipse project as <a href="http://data.christianschenk.org/profiling-with-aspectj/ProfilingWithAspectJ-1.0.tar.gz">tar.gz</a> or <a href="http://data.christianschenk.org/profiling-with-aspectj/ProfilingWithAspectJ-1.0.zip">zip</a>; make sure to install the <a title="Eclipse AspectJ Development Tools" href="http://www.eclipse.org/ajdt/">AspectJ Development Tools</a>. You can browse the code online <a title="Profiling with AspectJ - Code Reference" href="http://data.christianschenk.org/profiling-with-aspectj/xref/index.html">here</a>.</p>
<p><span id="more-36"></span></p>
<h2>How to profile methods</h2>
<p>Writing an aspect that adds advice to the methods you&#8217;d like to profile is as easy as this. You define a pointcut that matches the methods you&#8217;d like to monitor and define an <code>around</code> advice that calculates the time it took the method to execute. Have a look at the following piece of code:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Around<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;profile()&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> aroundInterestingMethods<span style="color: #009900;">&#40;</span>ProceedingJoinPoint thisJoinPoint<span style="color: #009900;">&#41;</span>
            <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Throwable</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">long</span> start, end<span style="color: #339933;">;</span>
  start <span style="color: #339933;">=</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">nanoTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  thisJoinPoint.<span style="color: #006633;">proceed</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  end <span style="color: #339933;">=</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">nanoTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">// calculate time: (end - start) / 1000) / 1000;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>All that&#8217;s left to do is to define a pointcut named <code>profile</code>, that matches the methods you&#8217;d like to monitor. Since the pointcut will be variable, I wrote an <a title="Abstract profiling aspect" href="http://data.christianschenk.org/profiling-with-aspectj/xref/index.htmlorg/christianschenk/aspectj/profiling/AbstractProfilingAspect.html">abstract aspect</a> that can be subclassed for different monitoring needs.</p>
<p>In a <a title="Sample code for an implementation" href="http://data.christianschenk.org/profiling-with-aspectj/xref/index.htmlorg/christianschenk/aspectj/profiling/RunnerProfilingAspect.html">subclass</a> you would implement the <code>profile</code> method like so, if you&#8217;d like to monitor all methods named <code>run</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Pointcut<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;execution(* *.*.run())&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> profile<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></pre></div></div>

<h2>What about the results?</h2>
<p>If you would run the <a title="Profiling with AspectJ - Code Reference" href="http://data.christianschenk.org/profiling-with-aspectj/xref/index.html">sample code</a>, you could see this:</p>
<pre class="code">
    Results for Runner.run (1002ms)
    Method: TaskB.run() took 600ms (60.0%)
    Method: TaskA.run() took 400ms (40.0%)
</pre>
<p>Once the <code>run</code> method of the <a title="Monitored class from the sample code" href="http://data.christianschenk.org/profiling-with-aspectj/xref/index.htmlorg/christianschenk/aspectj/profiling/example/Runner.html">Runner</a> class comes to an end, we can see these statistics, produced by the <code>afterTopLevelMethod</code> method from the profiling <a title="Abstract profiling aspect" href="http://data.christianschenk.org/profiling-with-aspectj/xref/index.htmlorg/christianschenk/aspectj/profiling/AbstractProfilingAspect.html">aspect</a>.</p>
<p>Based on this textual results it would be pretty easy to produce a graphical representation with <a title="gnuplot homepage" href="http://www.gnuplot.info/">gnuplot</a> or <a title="JFreeChart - Java chart library" href="http://www.jfree.org/jfreechart/">JFreeChart</a>. I could imagine a picture like this:</p>
<p><img src="http://data.christianschenk.org/profiling-with-aspectj/profiling.png" alt="Graphical representation of the profiling results"/></p>
<p>Although this looks very simple, a picture might be easier to absorb if there would be more methods.</p>
<h2>Conclusion</h2>
<p>As we&#8217;ve seen, profiling with AspectJ can be implemented straight forward. My simple example showed how to monitor certain methods and calculate the their execution time. The authors of this <a href="http://www.bibsonomy.org/bibtex/24354d3b3d13708590824241967d18cea/cschenk">paper</a> point us to some more things we might want to profile, e.g. heap space. Although I haven&#8217;t tried it yet, it should be possible to implement this too.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.christianschenk.org/blog/profiling-with-aspectj/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

