About metric_fu 1.3.0


Metric_fu is a set of rake tasks that make it easy to generate metrics reports. It uses Saikuro, Flog, Flay, Rcov, Reek, Roodi, Churn, Subversion, Git, and Rails built-in stats task to create a series of reports. It's designed to integrate easily with CruiseControl.rb by placing files in the Custom Build Artifacts folder.


New in metric_fu 1.3.0 is the ability to configure what RAILS_ENV you what Rcov to run under so it won't clobber any other tests that might be running (in continuous integration). You can also configure the minimum Flay score to record (the default minimum score 100). There's now some cool tooltip hover info in the graphs (thanks Édouard Brière).


In metric_fu 1.2.0 the graphing has been overhauled. Now you can use either Bluff (the default) or Google Charts to make your graphs. An upside of moving away from Gruff is that metric_fu no longer depends on ImageMagick/rmagick. Carl Youngblood submitted this feature so thank him if you like it. See the Graphing section below for how to configure.


Installation:

sudo gem install metric_fu

Then in your Rakefile:


require 'metric_fu'

If you like to vendor gems, you can unpack metric_fu into vendor/gems and require it like so:


require(File.join(RAILS_ROOT, 'vendor', 'gems', 'metric_fu-1.3.0', 'lib', 'metric_fu'))

Then you don't have to install it on every box you run it on.


Later versions of Rails like to manage your gems for you, so you can put this in your test.rb file:


config.gem 'metric_fu', :version => '1.3.0', :lib => 'metric_fu'

And then issue this command:


  $ rake gems:unpack RAILS_ENV=test

That way Rails won't yell at you every time you run a Rake task:


  config.gem: Unpacked gem metric_fu-1.3.0 in vendor/gems has no specification file. Run 'rake gems:refresh_specs' to fix this.

I hate being yelled at.

Output:


Usage:

Out of the box metric_fu provides this tasks:
  • rake metrics:all # Generate coverage, cyclomatic complexity, flog, flay, reek, roodi, stats...

I recommend using CruiseControl.rb to set up a metrics build. See the CruiseControl.rb online docs for more info on how to set up cc.rb and, once you've got that figured out, change the cruise_config.rb file inside your project to have these lines:

project.rake_task = 'metrics:all'
project.scheduler.polling_interval = 24.hours

Which will check for updates every 24 hours and run all the metric_fu rake tasks (migrating your test db first). The output will be visible from an individual build's detail page.


Configuration:

Metric_fu can be customized to your liking by altering the defaults in your Rakefile:

      MetricFu::Configuration.run do |config|
        #define which metrics you want to use
        config.metrics  = [:churn, :saikuro, :stats, :flog, :flay, :reek, :roodi, :rcov]
        config.graphs   = [:flog, :flay, :reek, :roodi, :rcov]
        config.flay     = { :dirs_to_flay => ['app', 'lib'],
                            :minimum_score => 100  } 
        config.flog     = { :dirs_to_flog => ['app', 'lib']  }
        config.reek     = { :dirs_to_reek => ['app', 'lib']  }
        config.roodi    = { :dirs_to_roodi => ['app', 'lib'] }
        config.saikuro  = { :output_directory => 'scratch_directory/saikuro', 
                            :input_directory => ['app', 'lib'],
                            :cyclo => "",
                            :filter_cyclo => "0",
                            :warn_cyclo => "5",
                            :error_cyclo => "7",
                            :formater => "text"} #this needs to be set to "text"
        config.churn    = { :start_date => "1 year ago", :minimum_churn_count => 10}
        config.rcov     = { :environment => 'test',
                            :test_files => ['test/**/*_test.rb', 
                                            'spec/**/*_spec.rb'],
                            :rcov_opts => ["--sort coverage", 
                                           "--no-html", 
                                           "--text-coverage",
                                           "--no-color",
                                           "--profile",
                                           "--rails",
                                           "--exclude /gems/,/Library/,spec"]}
        config.graph_engine = :bluff
    end
    
Note: if you don't want one of the metrics to run (like rcov), make sure you don't try to graph it (which will explode). Set config.graphs to exactly the graphs you want. Set config.graphs to an empty array (config.graphs = []) if you want no graphing at all.


Graphing

MetricFu uses Bluff for graphing (a Javascript library) by default. If you'd rather use Google Charts you can set 'config.graph_engine = :gchart' in the config. You'll have to install the googlecharts gem, of course.


Notes on metrics:coverage

When creating a coverage report, metric_fu runs all the tests in the test folder and specs in spec folder using Rcov. You can configure what files it should run and the RAILS_ENV (by setting 'environment') it runs under. See the configuration section above for details.


Notes on metrics:saikuro

Saikuro is bundled with metric_fu so you don't have to install it. Look at the SAIKURO_README (or the internet) for more documentation on Saikuro.


Notes on metrics:flay

Flay analyzes ruby code for structural similarities. You can configure which directories need to be flayed. The defaults are 'lib' for non Rails projects and ['app', 'lib'] for Rails projects. By default, metric_fu ignores scores under 100. You can configure the minimum_score (see configuration section above).


Notes on metrics:flog

Note: Flog 2.1.2 has some issues with Ruby 1.8.6 -- try using Flog 2.1.0 (Flog 2.1.2 does, however, work fine with Ruby 1.9.1)
Flog is another way of measuring complexity (or tortured code as the Flog authors like to put it). You should check out the awesome, and a little scary, Flog website for more info.


Notes on metrics:reek

Reek detects common code smells in ruby code. You can configure which directories need to be checked. The defaults are 'lib' for non Rails projects and ['app', 'lib'] for Rails projects.


Notes on metrics:roodi

Roodi parses your Ruby code and warns you about design issues you have based on the checks that is has configured. You can configure which directories need to be checked. The defaults are 'lib' for non Rails projects and ['app', 'lib'] for Rails projects.


Notes on metrics:stats

This is just 'rake stats' put into a file. On my projects I like to be able to look at CruiseControl and get stats about the app at different points in time.


Notes on metrics:churn

Files that change a lot in your project may be bad a sign. This task uses svn or git's log to identify those files and put them in a report.


Metric_fu began its life as a plugin for Rails that generated code metrics reports. As of version 0.7.0, metric_fu is a gem (owing to the excellent work done by Sean Soper) and is hosted on GitHub at http://github.com/jscruggs/metric_fu.


Questions?

Use the Google Group: http://groups.google.com/group/metric_fu


Thanks

I'd like to thank the authors of Saikuro, Subversion, Flog, Rcov, CruiseControl.rb, and Rails for creating such excellent open source products. Also many thanks go to Andre Arko, Petrik de Heus, Sean Soper, Erik St Martin, Andy Gregorowicz, Bastien, Michael Schubert, Kurtis Seebaldt, Toby Tripp, Ali Aghareza, Paul Gross, and Chirdeep Shetty for their contributions to metric_fu.