SourceMeter Logo


[Download] [Motivation] [Architecture] [Sample output] [SourceForge Project Site]

SourceMeter is a tool for calculating metrics on source code bases in any programming language. It provides a number of built-in metrics, but has also been designed to make the implementation of new metrics as simple as possible.

The tool has an easy-to-use command-line interface and generates tabular or semicolon-separated output that can easily be imported into spreadsheet or database software.

SourceMeter is entirely written in Java and does not depend on any components other than a JVM.


A first release of the SourceMeter system is available in the project file release area.

Download SourceMeter v0.5


A significant amount of research in the field of software engineering has gone into software metrics. It is evident that software metrics are a useful tool for analysing code bases, assessing quality and ensuring standards.

Although there are commercial products for calculating and analysing software metrics, I am not aware of an open-source project that provides a general framework for capturing software metrics. What I have seen so far have been programs for calculating some specific metrics for some specific programming language.

The aim of the SourceMeter project is to provide an open-source framework, implemented in Java, that provides basic services useful for all metrics, and that includes some basic metrics that serve as examples and should in itself be useful. Using the framework, it should be reasonably easy to write a class that implements a new metric or group of metrics. SourceMeter should be useful for people that want to gather metrics about their software projects, but so far haven't done it because they were lacking a good (and inexpensive) tool. The SourceMeter project could in turn benefit from people who have implemented parsers or metrics and are willing to share them with others.


The envisioned SourceMeter architecture is as follows. A configuration file holds data about gauges. A gauge is a class that calculates one or more metrics. Gauges are pluggable by implementing a simple interface. Gauges define what parser they want to use. Parsers are also pluggable.

For example, a LineCounter gauge could calculate lines of code (LOC), using a simple LineParser that breaks the input source file into lines. Another gauge could use the same parser to calculate the non-empty lines of code (LOCNE). A more complex gauge could count the average method length for Java source files, using a Java parser.

The SourceMeter kernel provides a simple command-line interface. It recursively traverses a directory structure containing the source code and invokes a file processor for each file. The file processor identifies all gauges that are relevant for the file type (identified by the extension). It then invokes the cooresponding parsers, which generate parse events that are handled by the gauges. After all files have been processed, the kernel calculates aggregate data (sums, averages) for all directories hierarchically, ending in the grand totals for the root directory. The results are shown in the level of detail desired, and can be displayed or saved in tabular or CSV format.

Note that SourceMeter is only designed for 'static' metrics that can be calculated from a specific version of the software. 'Dynamic' metrics, like number of changes, bugs fixed, etc, that would need access to a version control system are not in scope.

Sample output

Example of directory-level output:

> sourcemeter -dir -metrics "FC LOT LOTNE JFLD" .
FC    Number of processed files
LOT   Lines of text
LOTNE Non-empty lines of text
JFLD  Number of Java field definitions

Dir             FC    LOT  LOTNE JFLD
root             5    485    383    -
  bin            1     28     19    -
  src            -      -      -    -
    sourcemeter 10  1,538  1,391   40
      parser     1     34     29    0
        java     7  9,649  9,004  209
      parser     8  9,683  9,033  209
      util       2    125    109    1
      gauge      4    206    168    8
    sourcemeter 24 11,552 10,701  258
  src           33 22,028 20,480  489
root            39 22,541 20,882  489

Example of file-level output:

> sourcemeter -f -file -metrics "FC LOT LOTNE JFLD" src/sourcemeter
FC    Number of processed files
LOT   Lines of text
LOTNE Non-empty lines of text
JFLD  Number of Java field definitions

File                        FC   LOT LOTNE JFLD
root                        10 1,538 1,391   40         1   216   191    4               1    54    52    0             1   188   169    8          1   225   206    4             1    88    78    7                  1   255   230    8              1   144   128    4           1   259   238    5  1    21    17    0                1    88    82    0
root                        10 1,538 1,391   40

[Download] [Motivation] [Architecture] [Sample output] [SourceForge Project Site]

Chris Broecker, 12 October 2001

The SourceMeter project is kindly hosted by SourceForge:
SourceForge Logo