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 ([1] and [2]) 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 ;-)

  • Simply awesome!

  • Hi

    Soanr is set for nightly build by enabling hudson “Do not run Sonar when the build is trigged by SCM changes.” check box but please let me know when sonar will take the report or what is the right time to see sonar report.
    Is there is any configuration done in sonar to take the reports?

  • Hi Newman, I’m not sure to understand what you mean by report. Please swith to the sonar user mailing list to get support.
    Freddy

  • We have taken snapshots about a month apart, and find that, for most of our code modules, we get reasonable Code Coverage percentages for the June 6 snapshot (anywhere between 30% and 60%), but for all snapshots since, we get 0% Code Coverage for almost all of the code modules we are tracking (about 30). The other Tests metrics (Unit tests duration, skipped unit tests, etc.) look fine for all snapshots. Can anybody explain why the Code Coverage percentages suddenly drop to 0?

  • Perhaps because you use Maven Cobertura version 2.2 with Java 6 ? (http://jira.codehaus.org/browse/SONAR-989). If this is not the case, please switch to the user mailing list.

  • You should update the command line to the new format.

    mvn org.codehaus.sonar:sonar-maven-plugin:1.x:sonar -Dsonar.projectDate=2007-01-12

    Should be mvn sonar:sonar -Dsonar.projectDate=2007-01-12

    or I missed something.

  • You are correct Bruno. We cannot really amend the article, otherwise feed reader will be notified, but your comment will act as an update :-)

  • Hi,

    Is it possible to get drilled down details from Time Machine. For Example: If we get a difference of 20 Rules violated between 2 snapshots, can we get the details over these 20 rules alone and not the entire list so that they can be addressed with high priority?.

  • Arun, this is not currently possible. Feel free to create a Jira issue for this good addition to current functionality.

Leave a reply


9 − two =