<?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>erik dörnenburg &#187; Visualisation</title>
	<atom:link href="http://erik.doernenburg.com/topics/softviz/feed/" rel="self" type="application/rss+xml" />
	<link>http://erik.doernenburg.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Tue, 01 Nov 2011 08:36:48 +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>Metrics Tree Maps</title>
		<link>http://erik.doernenburg.com/2010/05/metrics-tree-maps/</link>
		<comments>http://erik.doernenburg.com/2010/05/metrics-tree-maps/#comments</comments>
		<pubDate>Mon, 03 May 2010 10:36:45 +0000</pubDate>
		<dc:creator>Erik Doernenburg</dc:creator>
				<category><![CDATA[Visualisation]]></category>

		<guid isPermaLink="false">http://erik.doernenburg.com/?p=365</guid>
		<description><![CDATA[As a consultant I often find myself in a position where I have to get to know a large existing code base quickly; I need to understand how the code is structured, how well it is written, whether there are any major issues, and if so, whether they are localised or whether they are spread [...]]]></description>
			<content:encoded><![CDATA[<p>As a consultant I often find myself in a position where I have to get to know a large existing  code base quickly; I need to understand how the code is structured, how well it is written, whether there are any major issues, and if so, whether they are localised or whether they are spread throughout the code base. To get a feeling for the general quality of the code I have found <a href="http://erik.doernenburg.com/2008/11/how-toxic-is-your-code/">Toxicity charts</a> useful. To understand the structure, <a href="http://erik.doernenburg.com/2010/04/dependency-structure-matrix/">Dependency Structure Matrices</a> come in handy. Conceptually somewhere between those two lie metrics tree maps, which I want to write about today.</p>
<p>A metrics tree map visualises the structure of the code by rendering the hierarchical package (namespace) structure as nested rectangles, with parent packages encompassing child packages. The actual display is taken up by the leaves in this structure, the classes. Have a look at the following tree map which shows the JRuby code base, without worrying too much about the &#8220;metrics&#8221; part yet.</p>
<p><a href="http://erik.doernenburg.com/wp-content/uploads/2010/05/JRuby-sizecomplexity.png"><img src="http://erik.doernenburg.com/wp-content/uploads/2010/05/JRuby-sizecomplexity-600x415.png" alt="" title="JRuby (Size/Complexity)" width="600" height="415" class="aligncenter size-large wp-image-405" /></a></p>
<p>At the top right I have highlighted the <em>org.jruby.compiler</em> package. The tree map shows that this package contains a few classes, such as ASTCompiler and ASTInspector, as well as three subpackages, namely <em>impl</em>, <em>ir</em>, and <em>util</em>, with <em>util</em> for example containing a class called HandleFactory, visible on the far right. (Visible in the full-size version.) In the following I explain how the tree maps visualise metrics, and I will explain how to create such maps from Java source code. As usual, adapting this other programming languages is relatively easy.</p>
<p><span id="more-365"></span></p>
<h4>Showing metrics in the tree map</h4>
<p>From the JRuby example above it is clear that tree maps can visualise two metrics by using the size and the colour of the rectangles. In the example the size of the rectangle represents the number of lines in the corresponding file (the actual length of the file, not the sum of the lines of code in all the methods) while the colour shows the sum of the cyclomatic complexity of all the methods in the class; the redder the class, the higher the complexity.</p>
<p>This map allows easy identification of large and complex classes, and usually file size and  complexity are correlated, meaning that larger rectangles appear in a darker shade of red. The left third of the JRuby tree map follows this pattern almost perfectly. However, the tree map also highlights an exception to this pattern: the IANA class in the <em>org.jruby.ext.socket</em> package. This class is large but has a very low complexity for a class if its size, something that is worth an investigation.</p>
<p>It should be obvious that other size metrics, such as method count or length, could be mapped to the rectangle size, while metrics such as class fan-out or data abstraction coupling could be mapped to the colour. Going further, while it is intuitive to show a size-based metric as the size, it can be worthwhile to map other metrics to the size. For example, a tree map showing complexity via the size and fan-out as colour makes it obvious which complex classes have high fan-out and which ones have not. This can be useful to identify which (overly) complex classes can be refactored more easily. In a class that has lower fan-out the complexity is more &#8220;self-contained&#8221;, likely allowing for one or more easy Extract Class refactorings.</p>
<p>Going beyond these examples, it is also possible to map non-metric information onto the colour, for example the name of the dominant committer of a file. In such a case each committer would be mapped to an individual colour, and rectangles could be shaded grey if no single committer accounted for more than a certain percentage of commits. Such a view would show whether a development team has bought into collective code ownership, or whether developers have carved off individual packages for themselves. More ideas, and other visualisations <a href="http://www.moosetechnology.org/docs/distributionmap">here</a>.</p>
<h4>Using Checkstyle to get metrics</h4>
<p>Checkstyle is meant to report on violations of guidelines, usually if a numeric threshold is exceeded. (I&#8217;ve shown how this data can be visualised in a <a href="http://erik.doernenburg.com/2008/11/how-toxic-is-your-code/">previous article</a>.) If all thresholds are set to zero Checkstyle considers everything a violation and reports all metrics. The following file shows the configuration used for this article.</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;module</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Checker&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;severity&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;warning&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;module</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;TreeWalker&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;module</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;FileContentsHolder&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;module</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;FileLength&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;max&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;0&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/module<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;module</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;MethodLength&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;max&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;0&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/module<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;module</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;ClassDataAbstractionCoupling&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;max&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;0&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/module<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;module</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;ClassFanOutComplexity&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;max&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;0&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/module<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;module</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;CyclomaticComplexity&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;max&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;0&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/module<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/module<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/module<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>With this file saved as &#8220;metrics.xml&#8221; Checkstyle can be invoked as follows to create an XML file containing all metrics we are interested in.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">java <span style="color: #660033;">-jar</span> checkstyle-all-<span style="color: #000000;">4.1</span>.jar <span style="color: #660033;">-c</span> metrics.xml <span style="color: #660033;">-f</span> xml <span style="color: #660033;">-o</span> checkstyle_out.xml <span style="color: #660033;">-r</span> <span style="color: #000000; font-weight: bold;">&lt;</span>source_dir<span style="color: #000000; font-weight: bold;">&gt;</span></pre></div></div>


<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;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;checkstyle</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;4.1&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;file</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;jruby-1.4.1/src/org/jruby/anno/AnnotationBinder.java&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;error</span> <span style="color: #000066;">line</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">severity</span>=<span style="color: #ff0000;">&quot;warning&quot;</span> <span style="color: #000066;">message</span>=<span style="color: #ff0000;">&quot;File length is 487 lines (max allowed is 0).&quot;</span> <span style="color: #000066;">source</span>=<span style="color: #ff0000;">&quot;com.puppycrawl.tools.checkstyle.checks.sizes.FileLengthCheck&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;error</span> <span style="color: #000066;">line</span>=<span style="color: #ff0000;">&quot;31&quot;</span> <span style="color: #000066;">column</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">severity</span>=<span style="color: #ff0000;">&quot;warning&quot;</span> <span style="color: #000066;">message</span>=<span style="color: #ff0000;">&quot;Class Data Abstraction Coupling is 1 (max allowed is 0) classes [AnnotationBindingProcessor].&quot;</span> <span style="color: #000066;">source</span>=<span style="color: #ff0000;">&quot;com.puppycrawl.tools.checkstyle.checks.metrics.ClassDataAbstractionCouplingCheck&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;error</span> <span style="color: #000066;">line</span>=<span style="color: #ff0000;">&quot;31&quot;</span> <span style="color: #000066;">column</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">severity</span>=<span style="color: #ff0000;">&quot;warning&quot;</span> <span style="color: #000066;">message</span>=<span style="color: #ff0000;">&quot;Class Fan-Out Complexity is 5 (max allowed is 0).&quot;</span> <span style="color: #000066;">source</span>=<span style="color: #ff0000;">&quot;com.puppycrawl.tools.checkstyle.checks.metrics.ClassFanOutComplexityCheck&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;error</span> <span style="color: #000066;">line</span>=<span style="color: #ff0000;">&quot;36&quot;</span> <span style="color: #000066;">column</span>=<span style="color: #ff0000;">&quot;5&quot;</span> <span style="color: #000066;">severity</span>=<span style="color: #ff0000;">&quot;warning&quot;</span> <span style="color: #000066;">message</span>=<span style="color: #ff0000;">&quot;Cyclomatic Complexity is 1 (max allowed is 0).&quot;</span> <span style="color: #000066;">source</span>=<span style="color: #ff0000;">&quot;com.puppycrawl.tools.checkstyle.checks.metrics.CyclomaticComplexityCheck&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;error</span> <span style="color: #000066;">line</span>=<span style="color: #ff0000;">&quot;36&quot;</span> <span style="color: #000066;">column</span>=<span style="color: #ff0000;">&quot;5&quot;</span> <span style="color: #000066;">severity</span>=<span style="color: #ff0000;">&quot;warning&quot;</span> <span style="color: #000066;">message</span>=<span style="color: #ff0000;">&quot;Method length is 3 lines (max allowed is 0).&quot;</span> <span style="color: #000066;">source</span>=<span style="color: #ff0000;">&quot;com.puppycrawl.tools.checkstyle.checks.sizes.MethodLengthCheck&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
...</pre></div></div>

<p>For languages other than Java other tools can be used to create a similar output file. The format does not need to be exactly the same (more later).</p>
<h4>Drawing tree maps with InfoVis</h4>
<p><a href="http://erik.doernenburg.com/wp-content/uploads/2010/05/InfoVis-panel.png"><img src="http://erik.doernenburg.com/wp-content/uploads/2010/05/InfoVis-panel-178x300.png" alt="" title="InfoVis" width="178" height="300" class="alignright size-medium wp-image-424" /></a></p>
<p>I&#8217;ve been using the <a href="http://ivtk.sourceforge.net/">InfoVis Toolkit</a> for years and while development on it has stagnated it just works; at least as long as a Java 5 runtime is available, which, unfortunately, is not the case on Mac OS X Snow Leopard. I&#8217;m open to suggestions of better/newer tools&#8230;</p>
<p>InfoVis is an interactive Java application that has a decent user interface to map various fields of the input file onto different aspects of the tree map, size and colour being two obvious ones. At this point it is probably worth mentioning that I generally also &#8220;sort&#8221; the tree map by whatever metric I have mapped to the size. The screenshot on the right shows the settings I used for the JRuby tree map above, and in case you wonder about the abbreviations for the metrics, FLENGTH is obviously file length, and WMCcc stands for Weighted Method Count (cyclomatic complexity). </p>
<h4>Data conversion</h4>
<p>The remaining piece of the puzzle is the transformation of the Checkstyle output into a format that InfoVis can interpret. Luckily, the input file format for InfoVis, named <em>TM3</em>, is very simple. The following excerpt shows data for the first few classes of the JRuby code base.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">FLENGTH	CDAC	CFOC	MCOUNT	WMCloc	WMCcc
INTEGER	INTEGER	INTEGER	INTEGER	INTEGER	INTEGER
487	6	16	15	422	87		org	jruby	anno	AnnotationBinder
21	0	1	0	0	0		org	jruby	anno	Coercion
14	0	0	0	0	0		org	jruby	anno	CoercionType
5	0	0	0	0	0		org	jruby	anno	FrameField
69	6	9	1	54	11		org	jruby	anno	InvokerGenerator
136	1	3	4	93	26		org	jruby	anno	JavaMethodDescriptor
20	0	0	0	0	0		org	jruby	anno	JRubyClass</pre></div></div>

<p>The first line contains the names of the metrics, the second line their data types. Both are tab separated. The remaining lines contain the metrics for each of the classes followed by the tree structure, with all package nodes flattened out and separated by tabs as well. Sometimes non-XML formats are so refreshingly easy to work with.</p>
<p>A small Ruby script, which I have attached further down can be used to convert Checkstyle&#8217;s XML output into the TM3 format, using a command like this:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">ruby checkstyle2tm3.rb checkstyle_out.xml <span style="color: #000000; font-weight: bold;">&lt;</span>source_dir<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">&gt;</span> treemap.tm3</pre></div></div>

<p>Currently, the script has the path separator hard-coded. If you intend to run this script on a Windows machine you have to change the path separator constant in line 6 manually.</p>
<p><strong>This <a href="http://erik.doernenburg.com/wp-content/uploads/2010/05/checkstyle2tm3.rb.zip">Zip archive</a> contains the Ruby script used to convert Checkstyle XML output format to the InfoVis TM3 file format. Remember that you might have to change the path separator.</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://erik.doernenburg.com/2010/05/metrics-tree-maps/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Dependency Structure Matrix</title>
		<link>http://erik.doernenburg.com/2010/04/dependency-structure-matrix/</link>
		<comments>http://erik.doernenburg.com/2010/04/dependency-structure-matrix/#comments</comments>
		<pubDate>Fri, 09 Apr 2010 07:43:27 +0000</pubDate>
		<dc:creator>Erik Doernenburg</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Visualisation]]></category>

		<guid isPermaLink="false">http://erik.doernenburg.com/?p=373</guid>
		<description><![CDATA[This is just a quick post to raise awareness for a technique that has been around for a while. In software architecture a Dependency Structure Matrix (DSM) can be used to understand dependencies between groupings of classes, that is packages in Java and namespaces in C#. There are obviously other uses, and this Wikipedia article [...]]]></description>
			<content:encoded><![CDATA[<p>This is just a quick post to raise awareness for a technique that has been around for a while. In software architecture a Dependency Structure Matrix (DSM) can be used to understand dependencies between groupings of classes, that is packages in Java and namespaces in C#. There are obviously other uses, and this <a href="http://en.wikipedia.org/wiki/Design_Structure_Matrix">Wikipedia article</a> has more background information.</p>
<p>Returning to classes and packages, the following matrix shows a view of some of the core  classes of the Spring framework:</p>
<p><a href="http://erik.doernenburg.com/wp-content/uploads/2010/04/DSM1.png"><img src="http://erik.doernenburg.com/wp-content/uploads/2010/04/DSM1.png" alt="" title="DSM1" width="444" height="444" class="aligncenter size-full wp-image-376" /></a></p>
<p>In this typical package DSM the packages are listed on both axes. If a package has dependencies on another package, the number of dependencies is listed at the intersection. The package that has the dependency is on the top, the package that it depends on is on the left. In the example above the matrix shows that there are seven dependencies from the <em>beans.propertyeditors</em> package to the <em>core.io</em> package.</p>
<p><span id="more-373"></span></p>
<p>The first, and most obvious, issue that can be spotted in a DSM is a cyclical dependency. Most tools (more on those at the end of the post) try to arrange the matrix such that all dependencies are shown in the lower left triangle. A cyclical dependency makes this impossible and one &#8220;half&#8221; of the dependency will be shown in the upper right triangle. </p>
<p>In the following matrix, which shows a package view of the Jetty server, we can see that <em>jetty.webapp</em> has six dependencies on <em>jetty.handler</em> but the latter also has a single dependency on the former.</p>
<p><a href="http://erik.doernenburg.com/wp-content/uploads/2010/04/DSM2.png"><img src="http://erik.doernenburg.com/wp-content/uploads/2010/04/DSM2.png" alt="" title="DSM2" width="344" height="344" class="aligncenter size-full wp-image-377" /></a></p>
<p>Another design concern is that of cohesion and coupling, very well explained <a href="http://www.codeodor.com/index.cfm/2009/6/17/Strive-for-low-coupling-and-high-cohesion-What-does-that-even-mean/2902">here</a>. Applying this to a package level and DSM it is good to see dependency numbers arranged in squares, showing cohesion between related packages, but rows or columns filled with numbers are often a concern. A row filled with numbers signifies a package that many other packages depend on. This can be acceptable, as in the <em>util</em> package in the Spring matrix above, but often it is a sign of a lack of cohesion. Conversely, a column filled with numbers represents a package that depends on many other packages, which is usually a sign of bad coupling and the possible presence of <a href="http://c2.com/cgi/wiki?GodClass">god classes</a> and <a href="http://c2.com/cgi/wiki.htm?FeatureEnvySmell">feature envy</a>. </p>
<p>These patterns become even more apparent when looking at larger systems. The following matrix is a zoomed out version showing most of the Spring framework.</p>
<p><a href="http://erik.doernenburg.com/wp-content/uploads/2010/04/DSM3b.png"><img src="http://erik.doernenburg.com/wp-content/uploads/2010/04/DSM3b.png" alt="" title="DSM3b" width="561" height="561" class="aligncenter size-full wp-image-387" /></a></p>
<p>There is a good cohesion, shown in pink, in many of the subpackages, but there are also some classes that invite a large number of dependencies, shown in blue, as well as a couple of classes that have unfocussed dependencies, shown in green.</p>
<p>At this point I should probably mention that I think that the Spring framework is fairly well designed, and that I&#8217;ve seen quite different matrices in the wild.</p>
<p>I hope I have convinced you that DSMs are worth a look, and luckily there is now widespread support for DSMs, in IDEs, e.g. <a href="http://www.jetbrains.com/idea/features/dependency_analysis.html">IntelliJ</a>, and in tools, e.g. <a href="http://www.headwaysoftware.com/products/structure101/client/dependency-matrix.php">Structure&nbsp;101</a>, <a href="http://www.lattix.com/technology">Lattix</a>, <a href="http://tcdev.free.fr/">.NET Reflector</a>. The screenshots in this post were created with Structure&nbsp;101 for Java.</p>
]]></content:encoded>
			<wfw:commentRss>http://erik.doernenburg.com/2010/04/dependency-structure-matrix/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Making build pain visible</title>
		<link>http://erik.doernenburg.com/2009/11/making-build-pain-visible/</link>
		<comments>http://erik.doernenburg.com/2009/11/making-build-pain-visible/#comments</comments>
		<pubDate>Tue, 03 Nov 2009 02:28:04 +0000</pubDate>
		<dc:creator>Erik Doernenburg</dc:creator>
				<category><![CDATA[Visualisation]]></category>

		<guid isPermaLink="false">http://erik.doernenburg.com/?p=334</guid>
		<description><![CDATA[The practice of continuous integration is gaining widespread adoption and almost every project I was involved in over the past few years used a continuous integration server to maintain an up-to-date view on the status of the build. Developers can look at the status page of the server or use tools such as CCTray and [...]]]></description>
			<content:encoded><![CDATA[<p>The practice of continuous integration is gaining widespread adoption and almost every project I was involved in over the past few years used a continuous integration server to maintain an up-to-date view on the status of the build. Developers can look at the status page of the server or use tools such as CCTray and CCMenu to find out whether a recent check-in has broken the build. Some teams also use build lights, like <a href="http://www.delcomproducts.com/products_USBLMP.asp">these</a> for example, or other information radiators to make the status of the build visible.</p>
<p>The reason why developers need an up-to-date build status is a common, and good, practice: new check-ins are only allowed when the build is known to be good. If it is broken chances are that someone is trying to fix it and dumping a whole new set of changes onto them would undoubtedly make that task harder. Similarly, when the server is building nobody knows for sure whether the build will succeed, and checking in changes would make fixing the build harder, should it fail.</p>
<p>To recap: the build must be good for a developer to be able to check in. On one of our projects this was becoming a rare occurrence, though. In fairness, the build performed fairly comprehensive checks in a complex integration environment, involving an ESB and an SSO solution. The team had already relegated some long-running tests to a different build stage, and they had split the <i>short build</i>, ie. the build that determines whether check ins are allowed, into five parallel builds, bringing build time down from over 45 to under ten minutes. Still, developers often found themselves waiting in a queue, maintained with post-its on a wall, for a chance to check in their changes. Not only that but everybody felt the situation was getting worse, that the build was broken more often. This was obviously a huge waste and I was keen to make it visible to management using a visualisation.</p>
<p><span id="more-334"></span></p>
<h4>Buildlines visualisation</h4>
<p>After some experimentation I decided on a variation of <a href="http://www.edwardtufte.com/bboard/q-and-a-fetch-msg?msg_id=0001OR">spark lines</a> for the build status. Each of the builds gets its own line and the colour shows the status of the build at any given point in time, green for good, red for broken, and blue for building. I blanked out the weekends and stretched the time during the days so that  only the hours between 8am and 7pm are visible. The resulting visualisation looked like this: (Click through for the full-size version.)</p>
<p><a href="http://erik.doernenburg.com/wp-content/uploads/2009/11/buildlines.png"><img src="http://erik.doernenburg.com/wp-content/uploads/2009/11/buildlines-600x65.png" alt="" title="Buildlines" width="600" height="65" class="alignnone size-medium wp-image-336" /></a></p>
<p>The zoomed out version clearly shows that matters were getting worse. The first two weeks in this diagram only show a few broken builds that, with the exception of one episode on build number 4 on Aug 11/12, get fixed relatively quickly. Looking at the last week and a half the picture changes quite dramatically:</p>
<p><a href="http://erik.doernenburg.com/wp-content/uploads/2009/11/buildlines-end.png"><img src="http://erik.doernenburg.com/wp-content/uploads/2009/11/buildlines-end-600x343.png" alt="Buildlines (last week)" title="Buildlines (last week)" width="600" height="343" class="aligncenter size-medium wp-image-345" /></a></p>
<p>On Monday, Sep 7, build 1 is broken for a while, which can happen. Later in the day build 2 breaks and is only fixed early on Tuesday. Again, this alone would not concern me, and while it is generally not such a great idea to leave a broken build behind, sometimes it is important to go home and get some rest.</p>
<p>At this point I should probably clarify that for a developer to be able to check in all the builds in this diagram need to be green. Each test in each build of this stage is significant. Long running, brittle tests, which include end-to-end integration tests with a mainframe system, have been moved to a different build stage and are treated separately.</p>
<p>A real reason for concern is that that around noon on Sep 8, all builds break at the same time. This is unlikely to be the result of a code change, because for a single change to break all builds it would have had to affect at least one test in each of the builds. Possible but not likely. A more reasonable explanation for this failure is a problem with the environment, maybe a database that is not responding or an SSO server that cannot authenticate the test users any longer. Similar problems can be seen Sep 14 during the morning, and Sep 15 in the afternoon. They are not completely new, either, as looking at Aug 28 reveals.</p>
<p>Environment problems like these can be extremely frustrating for a development team because a build that is broken not because of a code change but because of a problem with the  environment leaves the developers in an awkward position. They can either wait until the environment is fixed, but that often relies on a separate team that may have different priorities, or they can continue to check in based on the assumption that the build isn&#8217;t <em>really</em> broken. The latter is, of course, playing with fire as the team now effectively works without continuous integration.</p>
<p>Visualisations such as this one can help management get clarity on environment problems, and hopefully support a case for improving the build environment.</p>
<h4>The buildines script</h4>
<p>Unlike the visualisations I wrote about on this blog so far, this time I needed to write a bit more code as I could not find a tool to draw the spark lines for me. The data acquisition step was easy, though, because the team was using the <a href="http://studios.thoughtworks.com/cruise-release-management">Cruise</a> continuous integration server, which allowed me to get information about past builds through a web-based API. I used the cURL command, which, by the way, is available for Windows, too.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">curl <span style="color: #660033;">-o</span> Shortbuild1.csv <span style="color: #ff0000;">&quot;http://servername:8153/cruise/properties/search?pipelineName=WebFrontEnd-Dev&amp;stageName=Shortbuild&amp;jobName=Shortbuild1&amp;limitCount=1000&quot;</span>
curl <span style="color: #660033;">-o</span> Shortbuild2.csv <span style="color: #ff0000;">&quot;http://servername:8153/cruise/properties/search?pipelineName=WebFrontEnd-Dev&amp;stageName=Shortbuild&amp;jobName=Shortbuild2&amp;limitCount=1000&quot;</span>
...</pre></div></div>

<p>Normally I&#8217;m advocating to split the data processing stage from the actual visualisation but in this case, because I was going to write the visualisation from scratch and the data coming from Cruise was in a pretty good format already, I decided to put everything into a single script. For that I chose Ruby and <a href="http://rmagick.rubyforge.org/">RMagick</a>. The latter can be a pain to set up but there are installers for Windows, on the Mac it can be installed using MacPorts, and presumably the Linux package management systems include it, too.</p>
<p>The script is available from this <a href="http://bitbucket.org/erikdoe/buildlines/">Bitbucket repository</a>. It is relatively straight-forward and I am not going to examine it in great detail in this post. What is noteworthy is that, internally, the script shows a separation of concerns, with one class reading the Cruise files, a second class scaling and drawing the lines and labels, and a main class that holds everything together. If you were to adapt this to a different continuous integration server it should be possible to achieve this by simply writing a different DataFile class.</p>
<p>To run the script simply pass the list of build status files as command line parameters to it:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">ruby buildlines.rb Shortbuild<span style="color: #000000; font-weight: bold;">*</span>.csv</pre></div></div>

<p>The output will be written into a file named <i>buildlines.png</i> in the working directory. If you prefer a different format you can change the extension of the filename in the script, and RMagick, provided it supports the format, will magically write the corresponding format.</p>
<p><b>The visualisation script can be downloaded from <a href="http://bitbucket.org/erikdoe/buildlines/downloads/">Bitbucket</a>.  Simply select one of the zipped versions of the &#8220;tip&#8221; snapshot.</b></p>
]]></content:encoded>
			<wfw:commentRss>http://erik.doernenburg.com/2009/11/making-build-pain-visible/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Moose MSE for Java and C#</title>
		<link>http://erik.doernenburg.com/2009/07/moose-mse-for-java-and-cs/</link>
		<comments>http://erik.doernenburg.com/2009/07/moose-mse-for-java-and-cs/#comments</comments>
		<pubDate>Fri, 10 Jul 2009 01:58:00 +0000</pubDate>
		<dc:creator>Erik Doernenburg</dc:creator>
				<category><![CDATA[Releases]]></category>
		<category><![CDATA[Visualisation]]></category>

		<guid isPermaLink="false">http://erik.doernenburg.com/?p=276</guid>
		<description><![CDATA[Over the past years I have shown everyone who could not run fast enough some of the tools based on Moose. And even now I cannot resist putting a screenshot of CodeCity into this post. Most of the Moose tools now use the MSE file format as an interchange format. By the way, if you [...]]]></description>
			<content:encoded><![CDATA[<p>Over the past years I have shown everyone who could not run fast enough some of the tools based on <a href="http://moose.unibe.ch/">Moose</a>. And even now I cannot resist putting a screenshot of <a href="http://www.inf.unisi.ch/phd/wettel/codecity.html">CodeCity</a> into this post.</p>
<p><a href="http://www.inf.unisi.ch/phd/wettel/codecity-wof.html"><img src="http://erik.doernenburg.com/wp-content/uploads/2009/07/azureus-2500-coarse-cropped.png" alt="Part of the Azureus city" title="Part of the Azureus city" width="244" height="132" class="alignright size-full wp-image-288" /></a></p>
<p>Most of the Moose tools now use the <a href="http://moose.unibe.ch/docs/mse">MSE file format</a> as an interchange format. By the way, if you are interested in writing your own visualisations or analysis tools it is probably worthwhile looking at MSE, reading this format is so much more convenient than parsing source code.</p>
<p>In <strong>Java</strong> it was always relatively easy to create MSE files. Among many other things, <a href="http://loose.upt.ro/iplasma/">iPlasma</a> can read Java source code and export to MSE. That said, iPlasma has so many interesting features itself that oftentimes no export to an external tool is necessary.</p>
<p>For <strong>C#</strong> the story was different and for one reason or another no tool existed that could create MSE files for C#. This has changed now. As a student project at the University of Lugano such a tool was written and, thanks to <a href="http://www.inf.unisi.ch/faculty/lanza/">Michele Lanza</a>, then donated for general use. I&#8217;ve made a few improvements and put the code into this <a href="http://bitbucket.org/erikdoe/pmcs/">Bitbucket repository</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://erik.doernenburg.com/2009/07/moose-mse-for-java-and-cs/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Making ESB pain visible</title>
		<link>http://erik.doernenburg.com/2009/07/making-esb-pain-visible/</link>
		<comments>http://erik.doernenburg.com/2009/07/making-esb-pain-visible/#comments</comments>
		<pubDate>Wed, 01 Jul 2009 05:40:39 +0000</pubDate>
		<dc:creator>Erik Doernenburg</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Visualisation]]></category>

		<guid isPermaLink="false">http://erik.doernenburg.com/?p=236</guid>
		<description><![CDATA[At some point last year I was asked to review the architecture of the software behind a large and popular website. The resident architect explained how he had followed a modern approach, decoupling the web front-end from back-end services that provide content. To gain further flexibility he had put the front-end and the services on [...]]]></description>
			<content:encoded><![CDATA[<p>At some point last year I was asked to review the architecture of the software behind a large and popular website. The resident architect explained how he had followed a modern approach, decoupling the web front-end from back-end services that provide  content. To gain further flexibility he had put the front-end and the services on an ESB, mostly to cater for the possibility to make the content available to other consumers. In short, the architecture diagram looked a lot like many others: nothing to see here, move on.</p>
<p><img src="http://erik.doernenburg.com/wp-content/uploads/2009/06/high-level-esb.png" alt="Website Architecture" title="Website Architecture" width="320" height="212" class="alignnone size-full wp-image-237" /></p>
<p>The diagram above only shows one of the content services, which for the sake of this article is a service that provides contact details for a person.</p>
<p>Based on conversations with the project sponsors I began to suspect that at least the introduction of the ESB was a case of RDD, ie. Resume-Driven Development, development in which key choices are made with only one question in mind: how good does it look on my CV? Talking to the developers I learned that the ESB had introduced &#8220;nothing but pain.&#8221; But how could something as simple as the architecture in the above diagram cause such pain to the developers? Was this really another case of architect&#8217;s dream, developer&#8217;s nightmare?  </p>
<p><span id="more-236"></span></p>
<p>Often a picture says more than a thousand words, but at the same time traditional architecture diagrams simply do not provide enough information. (I&#8217;ve argued this before, <a href="http://softarch.97things.oreilly.com/wiki/index.php/Get_the_1000ft_view">here</a> and <a href="http://www.infoq.com/interviews/erik-doernenburg-software-visualization">here</a> for example.) The answer to my questions, and the anxious project sponsors&#8217; question whether the project would deliver on time, lay in a different picture.</p>
<p>The picture should not only show the layering of the system in more detail than the architecture diagram but it should also visualise the complexity introduced by the ESB. It&#8217;s often possible to produce more detailed diagrams automatically from source code, configuration, or runtime inspection but in this case I couldn&#8217;t find a way to do that, which is which I drew the following:</p>
<p><a href="http://erik.doernenburg.com/wp-content/uploads/2009/06/low-level-esb.png"><img src="http://erik.doernenburg.com/wp-content/uploads/2009/06/low-level-esb-600x533.png" alt="" title="Detailed ESB diagram" width="600" height="533" class="alignnone size-medium wp-image-237" /></a></p>
<p>In the centre column this diagram shows the layers involved, from the Struts action that needs to look up details by an ID down to the back-end that provides a &#8220;RESTful&#8221; service to retrieve the details. The top box contains the front-end layers while the box in the middle represents the ESB. The layers labelled in italics are frameworks or infrastructure software, the rest is project-specific code.</p>
<p>Key to this diagram are obviously the coloured representations of data travelling up and down the stack. Primitive types are grey, Java objects are purple circles, XML documents are shown as turquoise hexagons, and HTTP messages as green blobs. Mule messages, which are Java objects, are shown in dark purple. If one representation is wrapped in another it&#8217;s displayed inside the wrapper. </p>
<p>The diagram highlights that it&#8217;s a simple piece of data that travels all the way down the stack (actually further until it hits a relational database at some point) and, equally, that the data needed by the action is available in the right form from the service. All that the intermediate layers are doing is wrapping and unwrapping the data in various ways. In fact, out of the four layers of project-specific code three layers do nothing but wrapping. No wonder the developers felt some pain.</p>
<p>As a side note: the diagram also makes it obvious that the wrapping is asymmetric between the WsToRestHandler and the RestServiceWrapperImpl, which makes the point of this layer boundary even more questionable.</p>
<p>This diagram made people more interested in the cost of what had just been a simple, innocuous line in the architecture diagram; which was the point of the exercise. So, what would <em>this</em> diagram look like without the ESB? Easy:</p>
<p><a href="http://erik.doernenburg.com/wp-content/uploads/2009/06/Low-Level-ESB2.png"><img src="http://erik.doernenburg.com/wp-content/uploads/2009/06/Low-Level-ESB2-600x305.png" alt="" title="Without ESB" width="600" height="305" class="alignnone size-medium wp-image-237" /></a></p>
<p>Apart for the obvious simplicity the diagram highlights that every layer has a purpose and there is only one wrapping, which is not undone either. Would it surprise you if I told you that the project went live without the ESB?</p>
<h3>Footnotes</h3>
<p>1) This article is by no means implying that any of the mentioned technologies are useless or harmful. Their use was harmful in this case.</p>
<p>2) The pattern of wrapping and unwrapping the same data through multiple layers is not new, it was also quite common in early 3-tier architectures. (In fact, some people called their architectures n-tier; I guess to show that the number of tiers was somewhat arbitrary but large.) We had a term to describe this pattern: the wormhole pattern.</p>
<p>3) This project is not an isolated incident of a Wormhole ESB. I have personally seen several in the past years.</p>
]]></content:encoded>
			<wfw:commentRss>http://erik.doernenburg.com/2009/07/making-esb-pain-visible/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>How toxic is your code?</title>
		<link>http://erik.doernenburg.com/2008/11/how-toxic-is-your-code/</link>
		<comments>http://erik.doernenburg.com/2008/11/how-toxic-is-your-code/#comments</comments>
		<pubDate>Fri, 21 Nov 2008 00:54:46 +0000</pubDate>
		<dc:creator>Erik Doernenburg</dc:creator>
				<category><![CDATA[Visualisation]]></category>

		<guid isPermaLink="false">http://erik.doernenburg.com/?p=133</guid>
		<description><![CDATA[If you are somebody who writes code you probably know that moment when you look at some code you didn&#8217;t write, or some code you wrote a long time ago, and you think &#8220;that doesn&#8217;t look good.&#8221; Ok, more realistically, you probably think &#8220;WTF? I wouldn&#8217;t want to touch that with a barge-pole!&#8221; It is [...]]]></description>
			<content:encoded><![CDATA[<p>If you are somebody who writes code you probably know that moment when you look at some code you didn&#8217;t write, or some code you wrote a long time ago, and you think &#8220;that doesn&#8217;t look good.&#8221; Ok, more realistically, you probably think &#8220;WTF? I wouldn&#8217;t want to touch that with a barge-pole!&#8221; It is not even so much about whether the code does what it should do&#8212;that takes a bit longer to figure out&#8212;or whether the code is too slow. Even if it&#8217;s perfectly bug free and performs well, there&#8217;s something to the way it&#8217;s written. This is part of the internal quality of a software system, something that the users and development managers can&#8217;t observe directly; yet, it still affects them because code with poor internal quality is hard to maintain and extend.</p>
<p>Now, as a developer, how do you help managers and business people understand the internal quality of code? They generally want a bit more than &#8220;it&#8217;s horrible&#8221; before they prioritise cleaning up the code over implementing new features that directly deliver business value. Or even: how do you figure out for yourself how bad some code actually is in relation to some other code? These were questions that <a href="http://jroller.com/zoonabar/">Chris Brown</a>, <a href="http://darrenhobbs.com/">Darren Hobbs</a>, and myself were asking ourselves a couple of years ago.</p>
<p>The answer came in the form of a simple bar chart, arguably not the most sophisticated visualisation but a very effective one. And our colleague <a href="http://www.alphaitjournal.com/">Ross Pettit</a> had the perfect name for it: The <strong>Toxicity Chart</strong>. Read on to see what it is and how it&#8217;s created.</p>
<p><span id="more-133"></span></p>
<p>As usual, let&#8217;s start with an example. What follows is the toxicity chart for a version of the Hibernate framework.</p>
<p><a href="http://erik.doernenburg.com/wp-content/uploads/2008/11/toxicity_hibernate.png"><img src="http://erik.doernenburg.com/wp-content/uploads/2008/11/toxicity_hibernate-600x331.png" alt="Toxicity Chart for Hibernate" title="toxicity_hibernate" width="600" height="331" class="size-medium wp-image-143" /></a></p>
<p>In a toxicity chart each bar represents a class and the height of the bar shows the toxicity score for that class. The score is based on several metrics (more on that below) and the higher the score the more toxic the class. The individual components of the score are coloured. For example, the contribution of the method length metric to the overall score is shown in orange. This makes it possible to see at a glance not only how toxic a codebase is but also how the problems are distributed. If there is a lot of purple and orange in the chart, this indicates long and complex methods, which means that the code is probably hard to test on a unit level. Lastly, classes that score zero points are left off the chart.</p>
<p>The calculation of the toxicity score is based on metrics and thresholds. For example, the method length metric has a threshold of 30. If a class contains a method that is longer it gets points proportional to the length of the method in relation to the threshold, e.g. a method with 45 lines of code would get 1.5 points because its length is 1.5 times the threshold. The score for all elements is added up. So, if a class comprises two methods, one that is 45 lines and another that is 60 lines long, the method length component of the score for the class will be 3.5 points. This means that elements are not just classified as toxic but the score reflects how toxic the element is.</p>
<p>The following table shows the metrics that make up the toxicity score and the corresponding base threshold on which the multipliers are based.</p>
<table border="0" cellpadding="2">
<tr>
<th>Metric</th>
<th>Level</th>
<th>Threshold</th>
</tr>
<tr>
<td>File Length</td>
<td>file</td>
<td>500</td>
</tr>
<tr>
<td>Class Fan-Out Complexity</td>
<td>class</td>
<td>30</td>
</tr>
<tr>
<td>Class Data Abstraction Coupling</td>
<td>class</td>
<td>10</td>
</tr>
<tr>
<td>Anon Inner Length</td>
<td>inner class</td>
<td>35</td>
</tr>
<tr>
<td>Method Length</td>
<td>method</td>
<td>30</td>
</tr>
<tr>
<td>Parameter Number</td>
<td>method</td>
<td>6</td>
</tr>
<tr>
<td>Cyclomatic Complexity</td>
<td>method</td>
<td>10</td>
</tr>
<tr>
<td>Nested If Depth</td>
<td>statement</td>
<td>3</td>
</tr>
<tr>
<td>Nested Try Depth</td>
<td>statement</td>
<td>2</td>
</tr>
<tr>
<td>Boolean Expression Complexity</td>
<td>statement</td>
<td>3</td>
</tr>
<tr>
<td>Missing Switch Default</td>
<td>statement</td>
<td>1</td>
</tr>
</table>
<p>At this point you might wonder where the selection of metrics and thresholds comes from. Well, we made them up. When we designed the toxicity chart we made a call on what constitutes &#8220;toxic&#8221; as opposed to just bad. Of course, staying with the method length metric, we normally wouldn&#8217;t want to see a 15-line method but that might be disputed. However, we hope that nobody thinks that a 30-line method is acceptable. And in case you&#8217;re really uncomfortable with the thresholds, you can obviously change them to build your own toxicity score. We do suggest, though, that you try the values presented here first.</p>
<p>Like many visualisations the creation of the toxicity chart falls into two steps: data acquisition and rendering.</p>
<p><strong>Step 1:</strong> For Java projects <a href="http://checkstyle.sourceforge.net/">Checkstyle</a> is the easiest way to get the metrics. The  score table above easily translates into a Checkstyle configuration file, which, by the way, is included in the Zip archive attached to this page. Assuming this file is named &#8220;metrics.xml&#8221; Checkstyle can be invoked like this:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">java <span style="color: #660033;">-jar</span> checkstyle.jar <span style="color: #660033;">-c</span> metrics.xml <span style="color: #660033;">-r</span> <span style="color: #000000; font-weight: bold;">&lt;</span>source_dir<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #660033;">-f</span> xml <span style="color: #660033;">-o</span> toxicity.xml</pre></div></div>

<p>Afterwards, the file &#8220;toxicity.xml&#8221; contains a list of all components for the toxicity score in an XML format.</p>
<p>Similar tools for other languages should be able to create similar output. It is not a huge problem if the output is somewhat different as it&#8217;s always possible to change step 2 to handle different input formats.</p>
<p><strong>Step 2:</strong> What is required next is to read the data from XML, aggregate it on a per-class level, and then render the bar chart. Microsoft Excel is good at the latter two but it needs a bit of help to read the XML file. This help comes in the form of a small piece of VBA code. The attached Excel workbook contains a sheet to load the data, an &#8220;Open XML&#8221; button backed by said VBA code, a pivot table to do the aggregation, and the chart based on the table. So, step 2, really is to open the Excel workbook and load the XML file. That&#8217;s all.</p>
<p>Now, if you know me you know that I&#8217;ve been working with Macs for a very long time. So, naturally, I&#8217;d like to use Excel on the Mac but, alas, in its current version it <a href="http://www.schwieb.com/blog/2006/08/08/saying-goodbye-to-visual-basic/">does not support VBA</a> anymore. So, unfortunately, it&#8217;s VMWare Fusion for this one.</p>
<p><strong>This <a href="http://erik.doernenburg.com/wp-content/uploads/2008/11/toxicitychart.zip">Zip archive</a> contains everything needed to create a toxicity chart for Java. The spreadsheet can be adapted to read input file formats created by tools for different languages; just edit the VBA behind the &#8220;Data&#8221; sheet.</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://erik.doernenburg.com/2008/11/how-toxic-is-your-code/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>Call Graph Visualisation with AspectJ and Dot</title>
		<link>http://erik.doernenburg.com/2008/09/call-graph-visualisation-with-aspectj-and-dot/</link>
		<comments>http://erik.doernenburg.com/2008/09/call-graph-visualisation-with-aspectj-and-dot/#comments</comments>
		<pubDate>Sat, 27 Sep 2008 17:26:28 +0000</pubDate>
		<dc:creator>Erik Doernenburg</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Visualisation]]></category>

		<guid isPermaLink="false">http://erik.doernenburg.com/?p=101</guid>
		<description><![CDATA[One of my favourite tools to render graphs is GraphViz Dot and in an earlier entry I described how to use it to visualise Spring contexts. Today I want to showcase a different application. Call graphs show how methods call each other, which can be useful for a variety of reasons. The example I use [...]]]></description>
			<content:encoded><![CDATA[<p>One of my favourite tools to render graphs is <a href="http://www.graphviz.org/">GraphViz Dot</a> and in an <a href="http://erik.doernenburg.com/2008/08/springviz/">earlier entry</a> I described how to use it to visualise Spring contexts. Today I want to showcase a different application.</p>
<p>Call graphs show how methods call each other, which can be useful for a variety of reasons. The example I use here is the graph rooted in a unit test suite, and in this case the graph gives an understanding of how localised the unit tests are, how much they are real unit tests or how close they are to mini-integration tests. In an ideal case the test method should call the method under test and nothing else. However, even with mock objects that&#8217;s not always practical. And if, like myself, you fall into the classicist camp of unit testers, as described by Martin Fowler in <a href="http://martinfowler.com/articles/mocksArentStubs.html">Mocks aren&#8217;t Stubs</a>, you might actually not be too fussed about a few objects being involved in a single test. In either case, looking at the call graph shows you exactly which methods are covered by which unit tests.</p>
<p>There are several ways to generate calls graphs and I&#8217;m opting for dynamic analysis, which simply records the call graph while the code is being executed. A good theoretical reason is that dynamic analysis can handle polymorphism but a more practical reason is that it&#8217;s actually really easy to do dynamic analysis; provided you use the right tools. The approach I describe in this article uses <a href="http://www.eclipse.org/ajdt/">Eclipse AJDT</a> to run the unit tests with a simple Java aspect that records the call graph and writes it out into a format that can be rendered more or less directly with Dot. Of course, this technique is not limited to creating graphs for unit test; it only depends on weaving an AspectJ aspect into a Java application.</p>
<p><span id="more-101"></span></p>
<p>Let&#8217;s start with the result. The following diagram shows the call graph for a few simple unit tests in the test suite for the CruiseControl dashboard. The tests methods are in the leftmost column and method invocations occur from left to right. It&#8217;s clearly visible that the tests are quite localised.</p>
<p><a href="http://erik.doernenburg.com/wp-content/uploads/2008/09/cc_calls_simple.png"><img src="http://erik.doernenburg.com/wp-content/uploads/2008/09/cc_calls_simple-600x250.png" alt="" title="CC Test Suite - Normal Call Graph" width="600" height="250" class="alignnone size-medium wp-image-113" /></a></p>
<p>The next example shows a section that&#8217;s a bit more intertwined. Some tests exercise the same method, some tests call more than one method, and some methods are called indirectly through different paths. All in all pretty reasonable, though.</p>
<p><a href="http://erik.doernenburg.com/wp-content/uploads/2008/09/cc_calls_normal.png"><img src="http://erik.doernenburg.com/wp-content/uploads/2008/09/cc_calls_normal-600x138.png" alt="" title="CC Test Suite - Normal Call Graph" width="600" height="138" class="alignnone size-medium wp-image-110" /></a></p>
<p>Now, I don&#8217;t want to showcase only the good parts. There are also sections in the graph that show how some of the test and dependencies are, well, a bit messy. In fairness, though, this is partly caused by some domain objets that are used across multiple tests.</p>
<p><a href="http://erik.doernenburg.com/wp-content/uploads/2008/09/cc_calls_knot.png"><img src="http://erik.doernenburg.com/wp-content/uploads/2008/09/cc_calls_knot-600x178.png" alt="" title="CC Test Suite - The Knot" width="600" height="178" class="alignnone size-medium wp-image-114" /></a></p>
<p>How hard can it be to create these graphs? Not very, as it turns out.</p>
<p><strong>Step 1</strong> is to create an aspect that intercepts all interesting method calls, which normally means calls to methods that are part of the project. It&#8217;s also possible, though, to include calls to libraries and frameworks. Depending on your experience with AspectJ this is as simple as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.example.callgraph</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> aspect CallInterceptor
<span style="color: #009900;">&#123;</span>
    pointcut allInterestingMethods<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span>
        <span style="color: #339933;">!</span>within<span style="color: #009900;">&#40;</span>CallInterceptor<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span>within<span style="color: #009900;">&#40;</span>CallLogger<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span>
          execution<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #339933;">*</span> com.<span style="color: #006633;">example</span>..<span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span>..<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    before<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> allInterestingMethods<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        CallLogger.<span style="color: #006633;">INSTANCE</span>.<span style="color: #006633;">pushMethod</span><span style="color: #009900;">&#40;</span>thisJoinPointStaticPart.<span style="color: #006633;">getSignature</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        CallLogger.<span style="color: #006633;">INSTANCE</span>.<span style="color: #006633;">logCall</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    after<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> allInterestingMethods<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        CallLogger.<span style="color: #006633;">INSTANCE</span>.<span style="color: #006633;">popMethod</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The first part of the aspect is the definition of the pointcut: it matches all public methods in &#8220;com.example&#8221; and sub-packages but excludes calls to the two classes that form part of the call graph logger itself.</p>
<p>The <em>before</em> advice, which is run before each method that matches the pointcut, places the signature of the current method on a stack maintained by the graph logger class. With the current method signature as the topmost element and the one that called it as the second from the top, the advice asks the logger to log the corresponding call. The <em>after</em> advice simply pops the topmost call from the stack.</p>
<p>This is almost all the excitement. The implementation of <code>CallLogger</code> deals with maintaining the stack and writing out the call in the format required by Dot. The complete implementation with some comments follows below.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.example.callgraph</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.BufferedWriter</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.FileWriter</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.Writer</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Stack</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.HashSet</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Set</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.aspectj.lang.Signature</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> CallLogger
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> CallLogger INSTANCE <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> CallLogger<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>An instance is made available as a public singleton. The main reason for using a singleton, rather than creating an instance in the aspect, is that if coherent logging is required across multiple projects in Eclipse it&#8217;s easiest to add a copy of the aspect to each project and have all aspects use the same logger instance.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    <span style="color: #000000; font-weight: bold;">private</span> Stack<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> callStack <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Stack<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> Set<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> callLog <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HashSet<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Writer</span> writer<span style="color: #339933;">;</span></pre></div></div>

<p>The fields hold the actual stack of method names, a set that contains all calls that have been logged already, and a writer to which the actual output is written. The set is a simple optimisation to prevent the same call from being written over and over again.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">     <span style="color: #000000; font-weight: bold;">public</span> CallLogger<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;">try</span> <span style="color: #009900;">&#123;</span>
            writer <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BufferedWriter</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">FileWriter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;calls.txt&quot;</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: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">IOException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">RuntimeException</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Cannot open 'calls.txt' for writing.&quot;</span>, e<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;">public</span> <span style="color: #000066; font-weight: bold;">void</span> pushMethod<span style="color: #009900;">&#40;</span><span style="color: #003399;">Signature</span> s<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">String</span> type <span style="color: #339933;">=</span> s.<span style="color: #006633;">getDeclaringType</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">String</span> method <span style="color: #339933;">=</span> type.<span style="color: #006633;">substring</span><span style="color: #009900;">&#40;</span>type.<span style="color: #006633;">lastIndexOf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'.'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;.&quot;</span> <span style="color: #339933;">+</span> s.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        callStack.<span style="color: #006633;">push</span><span style="color: #009900;">&#40;</span>method<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>If anyone knows a better way to turn the AspectJ signature into a pretty string, please leave a comment.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> popMethod<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        callStack.<span style="color: #006633;">pop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> logCall<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>callStack.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span>
            <span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">String</span> call <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> <span style="color: #339933;">+</span> top<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span> -&gt; <span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> <span style="color: #339933;">+</span> top<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>callLog.<span style="color: #006633;">contains</span><span style="color: #009900;">&#40;</span>call<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            write<span style="color: #009900;">&#40;</span>call<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            callLog.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>call<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>Following a guard statement that protects the code from trying to log a call when only one method is on the stack, the code creates a description of the call in the format required by Dot for a graph edge, ie. the origin node in double quotes, followed by a stylised arrow, followed by the target node, again in double quotes.</p>
<p>If this call is not in the call log, the code writes it to the writer and then stores the call in the log so that it does not get written again.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Signature</span> top<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> callStack.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>callStack.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> write<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> line<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>
            writer.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>line <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            writer.<span style="color: #006633;">flush</span><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: #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>
            <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">RuntimeException</span><span style="color: #009900;">&#40;</span>e<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: #009900;">&#125;</span></pre></div></div>

<p>Running some code, the unit tests in this example, will now create a file named &#8220;calls.txt&#8221; that contains all calls to methods that match the pointcut description in the aspect. The calls are written in Dot format but to make the output a valid input file for Dot a header and a closing bracket at the end are required.</p>
<p>It would be easy to write the required header when the writer is initialised but realistically it&#8217;s not possible to know when <code>logCall</code> is called for the last time, and thus it&#8217;s not possible to write the closing bracket from the Java code. Therefore&#8230;</p>
<p><strong>Step 2</strong> is a small shell script, or a script in your favourite scripting language, that wraps the required header and footer around the raw graph edges.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
&nbsp;
<span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #cc0000; font-style: italic;">&lt;&lt;-EOS
strict digraph G {
    graph [ ratio=&quot;auto compressed&quot;,
            rankdir=&quot;LR&quot;,
            ranksep=2,
            remincross=&quot;true&quot;,
            ];
    node  [ shape=box,
            style=filled,
            fillcolor=white,
            fontname=helvetica
            fontsize=10,
            fontcolor=black
            ];
EOS</span>
&nbsp;
<span style="color: #c20cb9; font-weight: bold;">cat</span> calls.txt
&nbsp;
<span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #cc0000; font-style: italic;">&lt;&lt;-EOS
    }
EOS</span></pre></div></div>

<p>I have an admission to make: This script is simplified and would not create the output I&#8217;ve shown further up. With a graph definition like this Dot would not put all unit tests nodes into the leftmost column but instead it would move them just left of the method they call. If, for example, test 1 called method A, which in turn called method B, and test 2 called method B directly, then test 1 would be in the first column, method A and test 2 in the second column and method B in the third column. The following UNIX command line wizardry, when added just below the <code>cat calls.txt</code> line in the script above, solves this problem. The explanation is left as an exercise to the reader&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">'{ rank = same; '</span>
<span style="color: #c20cb9; font-weight: bold;">fgrep</span> Test.test calls.txt <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #660033;">-F</span> <span style="color: #ff0000;">&quot; -&gt; &quot;</span> <span style="color: #ff0000;">'{ print $1 }'</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sort</span> <span style="color: #660033;">-u</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{ printf &quot;%s; &quot;, $1 }'</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">'}'</span></pre></div></div>

<p>I hope this article shows how two simple but powerful tools combined make it easy to create a really useful visualisation; and how clever but cryptic UNIX command lines can be.</p>
]]></content:encoded>
			<wfw:commentRss>http://erik.doernenburg.com/2008/09/call-graph-visualisation-with-aspectj-and-dot/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>SpringViz and the 1000ft view</title>
		<link>http://erik.doernenburg.com/2008/08/springviz/</link>
		<comments>http://erik.doernenburg.com/2008/08/springviz/#comments</comments>
		<pubDate>Wed, 13 Aug 2008 02:07:30 +0000</pubDate>
		<dc:creator>Erik Doernenburg</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Visualisation]]></category>

		<guid isPermaLink="false">http://erik.doernenburg.com/?p=47</guid>
		<description><![CDATA[The Spring framework has become ubiquitous in the Java world, and there are a large number of tools supporting developers of Spring-based applications. In this post I describe SpringViz; or, more accurately, my variant of it. SpringViz helps developers with what is at the heart of a Spring-based application, the container and the contexts files [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://www.springframework.org/about">Spring framework</a> has become ubiquitous in the Java world, and there are a large number of tools supporting developers of Spring-based applications. In this post I describe <a href="http://www.samoht.com/wiki/wiki.pl?SpringViz">SpringViz</a>; or, more accurately, my variant of it.</p>
<p>SpringViz helps developers with what is at the heart of a Spring-based application, the container and the contexts files that describe the beans. In larger projects these context files can grow quite a bit. Newer versions of the Spring framework introduced features that help reduce the clutter and there are vast numbers of blog posts voicing different opinions on what should and what shouldn&#8217;t be in a context file but, no matter what, the number of beans in the context files will grow with the size of a project, and at some point it becomes difficult to understand the overall structure. This is no different from trying to maintain an understanding of a large codebase. In fact, I consider the context files to be code rather than configuration.</p>
<p>I&#8217;ve argued before (<a href="http://softarch.97things.oreilly.com/wiki/index.php/Get_the_1000ft_view">here</a> and <a href="http://www.infoq.com/interviews/erik-doernenburg-software-visualization">here</a> for example) that to deal with the complexity and sheer size of software systems we need a 1000ft view. This is a view that uses visualisation techniques to aggregate large amounts of data and multiple metrics into one big picture. SpringViz provides that 1000ft view for Spring context files.</p>
<p><span id="more-47"></span></p>
<p>Let&#8217;s look at an example first. The following diagram shows the context files for the Spring JPetStore example application (click on the image for the full-size version):</p>
<p><a href="http://erik.doernenburg.com/wp-content/uploads/2008/08/jpetstore_beans.png"><img src="http://erik.doernenburg.com/wp-content/uploads/2008/08/jpetstore_beans_small.png" alt="Diagram showing the context files and beans contained in them for the Spring JPetSore example application" title="JPetStore Beans" width="600" height="424" class="size-full wp-image-81" /></a></p>
<p>I find that this diagram presents the architecture of the application quite nicely. The web controllers are defined in the petstore-servlet context in the top left and many of them have a dependency on the petStore bean, defined in the main application context. The petStore bean in turn has dependencies on all the DAOs, which are defined in the data access context. In that context we can clearly see how the top four DAOs share the same data source while the <em>Order</em> and <em>Sequence</em> DAOs use a different one. In addition to the individual dependencies we also get an impression of the overall architecture: a simple, multi-layered design with a single central choke point that acts as an adaptor between the presentation and data access layers.</p>
<p>From personal experience I can say that the bigger and more complex a system gets, the more valuable these diagrams become. In many cases they not only help share a common understanding of the architecture in the development team but they also help spot inconsistencies in the design and often trigger and support discussions around important restructurings. Ok, this is all I&#8217;m going to say to convince you to try out SpringViz. In the following paragraphs I&#8217;ll explain how to use it on your own projects.</p>
<p><strong>Step 1:</strong> You need a copy of SpringViz. The version I&#8217;m discussing in this article can be downloaded <a href="http://erik.doernenburg.com/wp-content/uploads/2008/08/springviz-20080813.zip">here</a>. When you unzip the archive you should have an Ant build file, a <em>springviz</em> folder with a few files in it and an <em>example</em> folder that contains the context files for JPetStore.</p>
<p><strong>Step 1b:</strong> SpringViz uses <a href="http://www.graphviz.org/">GraphViz Dot</a> to render the diagram. So, if you don&#8217;t have this on your system you need to download and install it. You should also open the build file and change the <em>dot.command</em> property to point to the Dot executable. On the Mac you should really get <a href="http://www.pixelglow.com/graphviz/">Pixelglow&#8217;s version</a> of GraphViz.</p>
<p><strong>Step 2:</strong> You can now run SpringViz by calling Ant with the default target. This will create a <em>target</em> folder, process the context files with the SpringViz XSLT sheet to create an input file for GraphViz Dot, and then invoke Dot to create <em>beans.png</em>, the final diagram.</p>
<p><strong>Step 3:</strong> To integrate SpringViz into your own projects you can simply add the contents of the build file to your build file, place the <em>springviz</em> folder in an appropriate place, and adjust the configuration properties at the top of the build file.</p>
<p>If you are adventurous you can have a look at the Dot documentation and change the XSLT sheet to create slightly different input for Dot. You could colour the beans according to all sorts of criteria or you could change other aspects of their appearance depending on bean attributes.</p>
<p>There are a few limitations that you should be aware of: In its current version the system can&#8217;t deal with multiple beans with the same name defined in different contexts. So, if you have a <em>defaultUrlMapping</em> bean or similar in multiple files and you have dependencies to/from these the resulting diagram will be wrong. In this case it&#8217;s easiest to modify the style sheet to filter out the problematic beans. Another important point is that in its current form the sheet simply tries to use a bean&#8217;s id and name, which usually works out but will cause problems when beans have both, name and id. And lastly, the XSLT processor will need access to the web to fetch the schema files. I guess this can be avoided but so far this hasn&#8217;t been a problem for me.</p>
<p><strong>If you missed the link above you can download the specific version of SpringViz discussed in this article from <a href="http://erik.doernenburg.com/wp-content/uploads/2008/08/springviz-20080813.zip">this site</a>. The latest version of SpringViz as well as more documentation and resources are available on <a href="http://www.samoht.com/wiki/wiki.pl?SpringViz">Mike Thomas&#8217;s site</a></strong>.</p>
]]></content:encoded>
			<wfw:commentRss>http://erik.doernenburg.com/2008/08/springviz/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Interview on Software Visualisation</title>
		<link>http://erik.doernenburg.com/2007/11/interview-on-software-visualisation/</link>
		<comments>http://erik.doernenburg.com/2007/11/interview-on-software-visualisation/#comments</comments>
		<pubDate>Tue, 27 Nov 2007 02:28:21 +0000</pubDate>
		<dc:creator>Erik Doernenburg</dc:creator>
				<category><![CDATA[Clippings]]></category>
		<category><![CDATA[Visualisation]]></category>

		<guid isPermaLink="false">http://erik.doernenburg.com/2007/11/27/interview-on-software-visualisation/</guid>
		<description><![CDATA[I have looked at applications of visualisations techniques to improve software quality for good while now, and at a QCon conference earlier this year Floyd Marinescu and I chatted about my current thinking. If you are interested, the full interview has been published on InfoQ now. More here.]]></description>
			<content:encoded><![CDATA[<p>I have looked at applications of visualisations techniques to improve software quality for good while now, and at a QCon conference earlier this year Floyd Marinescu and I chatted about my current thinking. If you are interested, the full interview has been published on InfoQ now. <a href="http://www.infoq.com/news/2007/10/erik-doernenburg-soft-visual">More here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://erik.doernenburg.com/2007/11/interview-on-software-visualisation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Lugano Visit</title>
		<link>http://erik.doernenburg.com/2007/01/lugano-visit/</link>
		<comments>http://erik.doernenburg.com/2007/01/lugano-visit/#comments</comments>
		<pubDate>Mon, 22 Jan 2007 11:16:13 +0000</pubDate>
		<dc:creator>Erik Doernenburg</dc:creator>
				<category><![CDATA[Visualisation]]></category>

		<guid isPermaLink="false">http://blogs.doernenburg.com/2007/03/20/lugano-visit/</guid>
		<description><![CDATA[As you might know one of my main interests at the moment is software visualisation, and the success of the talk that Gregor and I are giving at conferences at the moment shows that there&#8217;s something to it; I guess. So far, we&#8217;ve mostly been using ad-hoc tools, made up from small building blocks such [...]]]></description>
			<content:encoded><![CDATA[<p>As you might know one of my main interests at the moment is software visualisation, and the success of the <a href="/talks/#softviz">talk</a> that Gregor and I are giving at conferences at the moment shows that there&#8217;s something to it; I guess.</p>
<p>So far, we&#8217;ve mostly been using ad-hoc tools, made up from small building blocks such as <a href="http://www.graphviz.org/">GraphViz Dot</a> and custom scripts to munge file formats. This can get you quite far, but how much further can you get with real tools?</p>
<p>With this question in mind, I visited Michele Lanza at the Univerity of Lugano when I was in Switzerland last week. I had been playing with his CodeCrawler for a while, especially since it started working with Java codebases courtesy of <a href="http://java2cdif.sourceforge.net/">java2cdif</a>, but I was curious what Michele and his PhD students are working on now.</p>
<p>The individual projects are a clear evolution of the ideas behind CodeCrawler, making use of interaction and filtering to cope with large amounts of data, and some of them add time as another dimension, which is interesting because I had also found identifying trends one of the most powerful uses of visualisation on several projects.</p>
<p>If you are interested in this field, have a look at <a href="http://www.inf.unisi.ch/faculty/lanza/">Michele&#8217;s site</a>, all the tools and related papers are linked from there.</p>
]]></content:encoded>
			<wfw:commentRss>http://erik.doernenburg.com/2007/01/lugano-visit/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

