<?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>Sonatype Blog &#187; performance</title>
	<atom:link href="http://www.sonatype.com/people/tag/performance/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sonatype.com/people</link>
	<description>Sonatype is transforming software development with tools, information and services that enable organizations to build better software, faster, using open-source components.</description>
	<lastBuildDate>Wed, 23 May 2012 14:24:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Profiling Maven Tests</title>
		<link>http://www.sonatype.com/people/2009/09/profiling-maven-tests/</link>
		<comments>http://www.sonatype.com/people/2009/09/profiling-maven-tests/#comments</comments>
		<pubDate>Mon, 07 Sep 2009 12:02:30 +0000</pubDate>
		<dc:creator>Marvin Froeder</dc:creator>
				<category><![CDATA[How-To]]></category>
		<category><![CDATA[Maven]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[quality]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://www.sonatype.com/people/?p=2763</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.sonatype.com/people/2009/09/profiling-maven-tests/' addthis:title='Profiling Maven Tests '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_button_google_plusone" g:plusone:size="medium"></a><a class="addthis_counter addthis_pill_style"></a></div>A few days ago we changed nexus integration tests to use an embedded version of nexus instead of using a forked VM. Before every test Nexus is started and then stopped again after the test completes. This is done once for each test class (Junit: @BeforeClass=start nexus, @AfterClass=stop nexus). During the first run we noticed [...]]]></description>
			<content:encoded><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.sonatype.com/people/2009/09/profiling-maven-tests/' addthis:title='Profiling Maven Tests '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_button_google_plusone" g:plusone:size="medium"></a><a class="addthis_counter addthis_pill_style"></a></div><p>A few days ago we changed nexus integration tests to use an embedded version of nexus instead of using a forked VM.  Before every test Nexus is started and then stopped again after the test completes.  This is done once for each test class (Junit: @BeforeClass=start nexus, @AfterClass=stop nexus).</p>

<p>During the first run we noticed some problems.Each time Nexus started, VM would use an additional 45Mb of memory.  Soon we realized that our embedder was not working right.  Stopping Nexus was not removing it from memory.</p>

<p>Setting all fields to null after stopping helped somewhat.  But now instead of 45Mb of leak we were getting 40Mb, which was still too much when we had more then 200 tests to run. It was time to profile our tests.</p>

<p>At Sonatype we use <a href="http://www.yourkit.com/">yourkit</a> for profiling, so I will demonstrate how to use yourkit to profile tests executed by maven. You should be able to use the same principle with any other profile tool.
<span id="more-2763"></span>
1 &#8211; First, <a href="http://www.yourkit.com/download/index.jsp">download, get a license</a> and install yourkit.
2 &#8211; Create a new profile to hold yourkit-related configuration:</p>


<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;profiles<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
...
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;profile<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #808080; font-style: italic;">&lt;!-- http://www.yourkit.com/docs/80/help/agent.jsp --&gt;</span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>yourkit-profile<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;properties<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;yourkit.home<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>C:\Arquivos de programas\YourKit Java Profiler 8.0.13<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/yourkit.home<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/properties<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/profile<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/profiles<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>


<p>I put the yourkit.home in pom.xml, but you can have it in settings.xml as well.</p>

<p>3 &#8211; Add yourkit agent to surefire inside the profile:</p>


<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">...
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;profile<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
...
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;build<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugins<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.apache.maven.plugins<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>maven-surefire-plugin<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
              <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;argLine<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>-agentpath:&quot;${yourkit.home}\bin\win32\yjpagent.dll&quot;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/argLine<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plugins<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/build<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/profile<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>


<p>There are other ways to do this you can see <a href="http://www.yourkit.com/docs/80/help/agent.jsp">here</a>.</p>

<p>4 &#8211; Launch yourkit UI.</p>

<div id="attachment_2765" class="wp-caption alignnone" style="width: 160px"><img class="size-thumbnail wp-image-2765" title="ui" src="http://www.sonatype.com/people/wp-content/uploads/2009/08/ui-150x150.png" alt="Yourkit UI" width="150" height="150" /><p class="wp-caption-text">Yourkit UI</p></div>

<p>5 &#8211; Launch maven using yourkit profile:
$ mvn clean test -P yourkit-profile -Dyourkit.home={your yourkit installation home}</p>

<p>As soon as yourkit UI detects your application, it will begin collecting profiling data.</p>

<div id="attachment_2766" class="wp-caption alignnone" style="width: 160px"><img class="size-thumbnail wp-image-2766" title="loaded-ui" src="http://www.sonatype.com/people/wp-content/uploads/2009/08/loaded-ui-150x150.png" alt="Test running" width="150" height="150" /><p class="wp-caption-text">Test running</p></div>

<h5>Optional, using the API</h5>

<p>Regular profiling is not very useful to find memory leaks on unit tests.  A much more efficient approach is to take memory snapshots programmatically.  To do so, we need <a href="http://www.yourkit.com/docs/80/help/api.jsp">yourkit API</a>.</p>

<p>On the same profile, add this dependency:</p>


<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;profile<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
...
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependencies<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.yourkit<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>yjp<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>8.0.13<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>system<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;systemPath<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${yourkit.home}\lib\yjp.jar<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/systemPath<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependencies<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
...
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/profile<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>


<p>Note that you should not move yjp.jar to your local maven repository.</p>

<p>Now we can create a <em>com.yourkit.api.Controller</em> instance and take memory snapshos when we need to.</p>

<p>To avoid compilation problems during regular builds (when you are not profiling) I suggest doing that using reflection, like this:</p>


<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    <span style="color: #666666; font-style: italic;">// profiling with yourkit, activate using -P youtkit-profile</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">Object</span> profiler<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> startProfiler<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">Class</span> controllerClazz<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">try</span>
        <span style="color: #009900;">&#123;</span>
            controllerClazz <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">Class</span>.<span style="color: #006633;">forName</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;com.yourkit.api.Controller&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span> <span style="color: #003399;">Exception</span> e <span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            log.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;Profiler not present&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">try</span>
        <span style="color: #009900;">&#123;</span>
            profiler <span style="color: #339933;">=</span> controllerClazz.<span style="color: #006633;">newInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            controllerClazz.<span style="color: #006633;">getMethod</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;captureMemorySnapshot&quot;</span> <span style="color: #009900;">&#41;</span>.<span style="color: #006633;">invoke</span><span style="color: #009900;">&#40;</span> profiler <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span> <span style="color: #003399;">Exception</span> e <span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            fail<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;Profiler was active, but failed due: &quot;</span> <span style="color: #339933;">+</span> e.<span style="color: #006633;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> takeSnapshot<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> profiler <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">try</span>
            <span style="color: #009900;">&#123;</span>
                profiler.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getMethod</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;forceGC&quot;</span> <span style="color: #009900;">&#41;</span>.<span style="color: #006633;">invoke</span><span style="color: #009900;">&#40;</span> profiler <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                profiler.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getMethod</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;captureMemorySnapshot&quot;</span> <span style="color: #009900;">&#41;</span>.<span style="color: #006633;">invoke</span><span style="color: #009900;">&#40;</span> profiler <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span> <span style="color: #003399;">Exception</span> e <span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#123;</span>
                fail<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;Profiler was active, but failed due: &quot;</span> <span style="color: #339933;">+</span> e.<span style="color: #006633;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>


<p>That way you won&#8217;t need to change the code to run in non-profiling mode.</p>

<p>You can see the full example in place at <a href="https://svn.sonatype.org/nexus/trunk/nexus/nexus-test-harness/nexus-test-harness-launcher">nexus-test-harness</a>.</p>

<p>If you are still reading and are curious what was causing the memory leak on Nexus tests, we discovered that jetty creates Shutdown Hook that can hold a reference to all servers (depends on how you configure it).  That shutdown hook stops jetty instances when the VM is going down.  So it was just a matter of removing my jetty instances from it when I manually stopped Nexus.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sonatype.com/people/2009/09/profiling-maven-tests/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

