Sonar Time Machine : replaying the past
When talking about source code quality, at first you might think that the only data of interest is the result of the last code analysis. However, you realize quickly that this information is not sufficient on its own and should be compared with similar data in the past.
Let's pretend for a few moment that you get a new job as team leader of a development team and let's make a few assumptions for the sake of argument : your main objective is to increase global quality/stability of applications and you are addicted to Sonar (probably not an assumption ;-)).
The first thing to be done is to analyze source code to quickly get a synthetic insight of the situation and define short term priorities. Therefore you take the following actions :
- You evaluate the source code quality according to the three main Sonar quality axes : code coverage, coding rules compliance, and cyclomatic complexity distribution.
- Then you optionality deeply analyze each axe respectively with coverage clouds, violations drilldown and measure drilldown services
You get the results : 35% of code coverage, 60% of coding rules compliance and an average of 4.5 cyclomatic complexity by method. You think it is a good start but insufficient.
How are you going to debrief those results with the team without more information ? Getting the values of those same metrics as they were 1 month, 6 months and for instance 2 years ago, would greatly help you understand where the team comes from : having a code coverage of 15% or 70% a year ago makes a big difference... in your approach. In one case, you're for sure going to support the team in its current effort. In the other one, you will want to understand what is going on and change it !
That is where the Time Machine functionality in Sonar can give you a big hand by replaying the past and by showing you how metrics have evolved over the last months/years. On the below image, you will find an example of such an history for the LogBack project (this great logging engine used by Sonar and successor of Log4j) and displaying metrics evolution since December 2006 :
Here is a two minutes tutorial to master the Sonar Time Machine engine :
- Retrieve your source code as it was at a given time in the past. If you use Subversion, you can go for instance to tags/ directory to get significant versions of your source code.
- Launch Sonar Maven goal on this source code and add a new Maven parameter "sonar.projectDate" to the command line to specify the date of this source code snapshot. You can optionally specify the version of this source code with "sonar.projectVersion" parameter ( As Sonar will use by default the Maven pom version, specifying this second parameter is most often useless) :
mvn org.codehaus.sonar:sonar-maven-plugin:1.x:sonar -Dsonar.projectDate=yyyy-mm-dd
- Go to Sonar web interface, click on your project and then on Time Machine service : your past snapshot is now displayed in the top history chart and in the bottom associated table. The next example shows what had appeared ( and ) after launching the following command on Logback source tag "0.8":
mvn org.codehaus.sonar:sonar-maven-plugin:1.x:sonar -Dsonar.projectDate=2007-01-12
Of course historical data is not restricted to code coverage, rules compliance or complexity. You can access more than 30 different metrics. To put emphasis on unit tests you can for example :
- Select "Unit tests duration", "Number of unit tests" and "Unit tests success (%)" in addition to "Code coverage"
- Click on the button located at the bottom of the page to see historical trends on metrics related to unit tests :
- Use the "Set as default" link (only available when logged in as an admin) in order to keep the selected chart as the default one for the Time Machine
So feel free to replay the past with Sonar Time Machine, it's safe and free ;-)