<?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; introspection</title>
	<atom:link href="http://www.christianschenk.org/blog/tag/introspection/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, 29 Aug 2010 09:08:16 +0000</lastBuildDate>
	
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Performance tests for introspection of JavaBeans</title>
		<link>http://www.christianschenk.org/blog/performance-tests-for-introspection-of-javabeans/</link>
		<comments>http://www.christianschenk.org/blog/performance-tests-for-introspection-of-javabeans/#comments</comments>
		<pubDate>Wed, 27 Jun 2007 09:36:37 +0000</pubDate>
		<dc:creator>Christian Schenk</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[beans]]></category>
		<category><![CDATA[comparison]]></category>
		<category><![CDATA[introspection]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[reflection]]></category>
		<category><![CDATA[test]]></category>

		<guid isPermaLink="false">http://www.christianschenk.org/blog/performance-tests-for-introspection-of-javabeans/</guid>
		<description><![CDATA[Tests the performance of the Java Introspector class]]></description>
			<content:encoded><![CDATA[<p>Java has the <code><a title="Introspector (Java 2 Platform SE 5.0)" href="http://java.sun.com/j2se/1.5.0/docs/api/java/beans/Introspector.html">Introspector</a></code> class that either uses the <a title="The Reflection API" href="http://java.sun.com/docs/books/tutorial/reflect/">Reflection API</a> or explicit information about a bean stored in a <code><a title="BeanInfo (Java 2 Platform SE 5.0)" href="http://java.sun.com/j2se/1.5.0/docs/api/java/beans/BeanInfo.html">BeanInfo</a></code> object. This class provides a standard way to access the properties of <a title="JavaBeans Documentation" href="http://java.sun.com/products/javabeans/docs/">JavaBeans</a> and comes in handy if you&#8217;d like to copy properties from one bean to another in a generic way.</p>
<p><span id="more-29"></span></p>
<p>Suppose you&#8217;ve built an application that uses standard JavaBeans to represent business objects of your problem domain. Then you decided to expose your application as a web service that returns XML: you created a <a title="W3C - XML Schema" href="http://www.w3.org/XML/Schema">XML Schema</a> for <a title="Java Architecture for XML Binding" href="http://java.sun.com/developer/technicalArticles/WebServices/jaxb/">JAXB</a> and generated a second model with it. Now all you would have to do is to fill the generated model and use JAXB to read/write it to your web service.</p>
<p>Since you&#8217;re a <a title="How To Become A Hacker - Boredom and drudgery are evil" href="http://www.catb.org/~esr/faqs/hacker-howto.html#believe3">hacker</a> and avoid repetitive work like the plague you write a tool that introspects your original beans and populates their properties to the generated JAXB beans and vice versa. This way you don&#8217;t have to write lots and lots of code that looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// for marshalling</span>
jaxbBean.<span style="color: #006633;">setFoo</span><span style="color: #009900;">&#40;</span>bean.<span style="color: #006633;">getFoo</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: #666666; font-style: italic;">// for unmarshalling</span>
bean.<span style="color: #006633;">setFoo</span><span style="color: #009900;">&#40;</span>jaxbBean.<span style="color: #006633;">getFoo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>You might ask why you don&#8217;t use the generated model in the first place and get rid of the <em>old</em> one. First of all this would be less fun and as a serious answer you probably don&#8217;t want to couple your application too tight to JAXB, because you might want to switch to another Java-to-XML binding framework (e.g. <a title="The Castor Project" href="http://www.castor.org/">Castor</a>) in the future.</p>
<h2>BeanIntrospector</h2>
<p>I wrote a small class (88 <a title="Source lines of code" href="http://en.wikipedia.org/wiki/Lines_of_code">LOC</a>) that&#8217;ll copy properties from one bean to another; it uses the aforementioned <code><a title="Introspector (Java 2 Platform SE 5.0)" href="http://java.sun.com/j2se/1.5.0/docs/api/java/beans/Introspector.html">Introspector</a></code> class. Although this is a pretty straight forward thing it introduces a major performance penalty.</p>
<p>The final class is now a product of several development cycles:</p>
<ol>
<li>my first version was a brute force attempt with some for loops</li>
<li>the second version stored the methods of the JavaBeans in a lookup table; so it could look up the methods faster</li>
<li>in addition to the second version, the third version tries to make no duplicate calls to e.g. <code>getClass()</code> &#8211; these things are stored in variables now.</li>
</ol>
<p>You can download the Eclipse project as <a href="http://data.christianschenk.org/performance-tests-for-introspection-of-javabeans/IntrospectionPerformanceTest-1.0.tar.gz">tar.gz</a> or <a href="http://data.christianschenk.org/performance-tests-for-introspection-of-javabeans/IntrospectionPerformanceTest-1.0.zip">zip</a> file or browse the code <a title="IntrospectionPerformanceTest - Code Reference" href="http://data.christianschenk.org/performance-tests-for-introspection-of-javabeans/xref/">here</a>; make sure that you have the <a title=" Maven Integration for Eclipse" href="http://m2eclipse.codehaus.org/">Maven2</a> plugin for Eclipse installed.</p>
<h2>Tests</h2>
<p>To evaluate the different versions, I coded a simple performance test that compares the performance of my BeanIntrospector implementation to code that does the same thing by hand, i.e. calling every getter/setter of two beans one after another.</p>
<p>The first chart shows a comparison of the different implementations.</p>
<p><img src="http://data.christianschenk.org/performance-tests-for-introspection-of-javabeans/introspection.png" alt="Performance comparison for introspection of JavaBeans"/></p>
<p>This leaves no doubt that the <em>boring</em> solution is by far the fastest. Another thing you can notice is that tuning the code can help a lot: while the first implementation takes more than 100ms to fill 1000 JavaBeans, the tuned version takes about 40ms to do the same thing. In addition to that the final version is more readable than the brute force variant.</p>
<p>The second chart shows a comparison between the final implementation and doing the same thing by hand. Note that I set the y-axis to logarithmic scale.</p>
<p><img src="http://data.christianschenk.org/performance-tests-for-introspection-of-javabeans/introspection-more-runs.png" alt="Performance comparison for introspection of JavaBeans with more runs"/></p>
<h2>Conclusion</h2>
<p>If performance is important for your application you should explicitly copy the properties from one bean to another. If you&#8217;ve got beans with a lot of properties, write yourself a small tool that&#8217;ll generate the code for you. On the other hand if you need a fast hack you might want to use the <code>BeanIntrospector</code> class I wrote or write yourself a similar solution.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.christianschenk.org/blog/performance-tests-for-introspection-of-javabeans/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
