The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Dead Simple Rails Metrics with metric_fu

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Jake Scruggs

Posts: 336
Nickname: aurorae314
Registered: Feb, 2008

Jake Scruggs is a developer at ThoughtWorks
Dead Simple Rails Metrics with metric_fu Posted: Apr 27, 2008 5:06 PM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Jake Scruggs.
Original Post: Dead Simple Rails Metrics with metric_fu
Feed Title: Jake Scruggs
Feed URL: http://jakescruggs.blogspot.com/feeds/posts/default
Feed Description: Ruby, Rails, Metrics, Testing, and pursuit of clean code.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Jake Scruggs
Latest Posts From Jake Scruggs

Advertisement
Every time I create a new rails project I usually put off writing tasks to analyze the code's quality 'cause it takes time and time is, you know, finite. So I've decided to extract some code into a rails plugin which I call metric_fu.

It's a bunch of rake tasks that produce reports on code coverage (using Rcov), cyclomatic complexity (using Saikuro), flog scores (using Flog), and rails stats (using 'rake stats'). It knows if it's being run inside a CruiseControl.rb build and puts the output in the Custom Build Artifacts folder so when you view a build you see this:

Cruise control build page

The coverage report is your standard rcov report:
Rcov output

Flog output is thrown into an html file:
Flog output

At the end metric_fu calculates the average flog score per method:
Flog average score
You might want to check out my previous posts on what to do with a Flog report: The Method Hit List and When You Should Ignore Metrics

Saikuro's output is the same as always:
Cyclomatic complexity output
(I changed the warning and error levels for this pic -- more on how I did that later)

And 'rake stats' is always useful:
Stats output

So how do you get all these reports?
1. install Flog
sudo gem install flog

2. install rcov
sudo gem install rcov

3. install metric_fu
ruby script/plugin install \
http://metric-fu.rubyforge.org/svn/tags/REL_0_5_1/metric_fu/
(in the base of your rails app)

4. rake metrics:all

Which should work fine if you have standard Rails testing and you like my defaults. But what if you use a combination of RSpec and stock Rails testing? Then you can insert this into your Rakefile:

namespace :metrics do
TEST_PATHS_FOR_RCOV = ['spec/**/*_spec.rb', 'test/**/*_test.rb']
end
The namespace isn't strictly necessary, but I like it for intentional purposes. Multiple paths are useful if, like on my last project, you need to be specific about which tests to run as some tests go after external services (and the people who manage them get cranky if you hammer 'em a lot).

If you also want Rcov to sort by lines of code (loc) and have more aggressive cyclomatic complexity settings then do this:

namespace :metrics do
TEST_PATHS_FOR_RCOV = ['spec/**/*_spec.rb', 'test/**/*_test.rb']
RCOV_OPTIONS = { "--sort" => "loc" }
SAIKURO_OPTIONS = { "--warn_cyclo" => "3", "--error_cyclo" => "4" }
end

That's it -- hope you find it useful, lemme know if you find a bug, and check out the project home page at:
http://metric-fu.rubyforge.org

Oh, and thanks to all my co-workers who helped write the original code, in its various forms, that became this plugin.

Read: Dead Simple Rails Metrics with metric_fu

Topic: Learn Ruby In Steel With Movies Previous Topic   Next Topic Topic: Classes on the Fly

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use