jscruggs-metric_fu 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,30 @@
1
+ === 0.4.2 / 2008-07-01
2
+
3
+ * Changed rcov output directory so that it is no longer 'coverage/unit' but just 'coverage' for better integration with CC.rb
4
+
5
+ === 0.4.1 / 2008-06-13
6
+
7
+ * Rcov tests now extend beyond one level depth directory by using RcovTask instead of the shell
8
+
9
+ === 0.4.0 / 2008-06-13
10
+
11
+ * Implementing functionality for use as a gem
12
+ * Added Rakefile to facilitate testing
13
+
14
+ === 0.3.0 / 2008-06-11
15
+
16
+ * Generated reports now open on darwin automatically
17
+ * Generated reports reside under tmp/metricks unless otherwise specified by ENV['CC_BUILD_ARTIFACTS']
18
+ * MD5Tracker works with Flog reports for speed optimization
19
+
20
+ === 0.2.0 / 2008-06-11
21
+
22
+ * Integrated use of base directory constant
23
+ * Have all reports automatically open in a browser if platform is darwin
24
+ * Namespaced under Metricks
25
+ * Dropped use of shell md5 command in favor of Ruby's Digest::MD5 libraries
26
+
27
+ === 0.1.0 / 2008-06-10
28
+
29
+ * Initial integration of metric_fu and my enhancements to flog
30
+ * Metrics are generated but are all over the place
data/MIT-LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2008 Jake Scruggs
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
data/Manifest.txt ADDED
@@ -0,0 +1,28 @@
1
+ History.txt
2
+ Manifest.txt
3
+ metric_fu-0.7.gem
4
+ metric_fu.gemspec
5
+ MIT-LICENSE
6
+ Rakefile
7
+ README
8
+ TODO.txt
9
+ lib/metric_fu.rb
10
+ lib/metric_fu/flog_reporter.rb
11
+ lib/metric_fu/md5_tracker.rb
12
+ lib/metric_fu/flog_reporter/base.rb
13
+ lib/metric_fu/flog_reporter/flog_reporter.css
14
+ lib/metric_fu/flog_reporter/generator.rb
15
+ lib/metric_fu/flog_reporter/operator.rb
16
+ lib/metric_fu/flog_reporter/page.rb
17
+ lib/metric_fu/flog_reporter/scanned_method.rb
18
+ lib/metric_fu/saikuro/saikuro.rb
19
+ lib/metric_fu/saikuro/SAIKURO_README
20
+ lib/tasks/churn.rake
21
+ lib/tasks/coverage.rake
22
+ lib/tasks/flog.rake
23
+ lib/tasks/metric_fu.rake
24
+ lib/tasks/metric_fu.rb
25
+ lib/tasks/saikuro.rake
26
+ lib/tasks/stats.rake
27
+ test/test_helper.rb
28
+ test/test_md5_tracker.rb
data/README ADDED
@@ -0,0 +1,100 @@
1
+ Version 0.7.0
2
+ http://github.com/jscruggs/metric_fu
3
+
4
+ Metric_fu began its life as a plugin for Rails that generated code metrics reports. As of version 0.7.0, metric_fu will become a gem owing to the excellent work done by Sean Soper.
5
+
6
+ Metric_fu is a set of rake tasks that make it easy to generate metrics reports. It uses Saikuro, Flog, Rcov, 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.
7
+
8
+ *Installation
9
+ sudo gem install jscruggs-metric_fu -s http://gems.github.com
10
+
11
+ Then in your Rakefile:
12
+ require 'metric_fu'
13
+
14
+ *Important note:
15
+ You must have Rcov and Flog installed to get coverage and flog reports. You can do this through ruby gems at the command line like so:
16
+ sudo gem install rcov
17
+ sudo gem install flog
18
+
19
+
20
+ *Usage
21
+
22
+ Out of the box metrics provides these tasks:
23
+ rake metrics:all
24
+ rake metrics:all_with_migrate
25
+ rake metrics:coverage
26
+ rake metrics:saikuro
27
+ rake metrics:flog
28
+ rake metrics:stats
29
+ rake metrics:churn
30
+
31
+ See below for more detail on the individual tasks. It's recommended to use 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:
32
+
33
+ project.rake_task = 'metrics:all_with_migrate'
34
+ project.scheduler.polling_interval = 24.hours
35
+
36
+ Which will check for updates every 24 hours and run all the metrics rake tasks (migrating your test db first). The output will be visible from an individual build's detail page.
37
+
38
+
39
+ *Notes on metrics:coverage
40
+
41
+ When creating a coverage report, metric_fu runs all the tests in the test folder using Rcov. If you use RSpec you can change the default by putting this in your Rakefile:
42
+
43
+ namespace :metrics do
44
+ TEST_PATHS_FOR_RCOV = ['spec/**/*_spec.rb']
45
+ end
46
+
47
+ The namespace is only there for intentional purposes and isn't necessary. If you have multiple paths to test, then you can do this:
48
+
49
+ TEST_PATHS_FOR_RCOV = ['spec/**/*_spec.rb', 'test/**/*_test.rb']
50
+
51
+ The coverage task will iterate over all the paths and aggregate the results into one report. You'll see a coverage.data file in the root of your project.
52
+
53
+ If you want to change the options that Rcov is run with, then set this constant in your Rakefile:
54
+
55
+ RCOV_OPTIONS = { "--sort" => "loc" }
56
+
57
+ It's a hash that gets merged with the default options. This particular change will sort the coverage report by lines of code (loc). Check out the Rcov documentation for more options. If you want to see the default options metric_fu runs, open up the rake files in the plugin.
58
+
59
+
60
+ *Notes on metrics:cyclomatic_complexity
61
+
62
+ 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. If you wish to change the options Saikuro is run with, then set this constant in your Rakefile:
63
+
64
+ namespace :metrics do
65
+ SAIKURO_OPTIONS = { "--warn_cyclo" => "3", "--error_cyclo" => "4" }
66
+ end
67
+
68
+ Like RCOV_OPTIONS, SAIKURO_OPTIONS is a hash that gets merged with the default options hash. The above example will set the warn_cyclo to 3 and the error_cyclo to 4 (which is way too low -- it's just an example) instructing Saikuro to flag methods with a higher cyclomatic complexity in it's report.
69
+
70
+
71
+ *Notes on metrics:flog
72
+
73
+ Flog is another way of measuring complexity (or tortured code as the Flog authors like to put it). Metric_fu takes the output of Flog run on the 'app' folder, puts it in between some <pre> tags, calculates the average Flog score per method, and jams all that into an index.html file. You should check out the awesome, and a little scary, Flog website for more info.
74
+
75
+
76
+ *Notes on metrics:stats
77
+
78
+ 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.
79
+
80
+
81
+ *Notes on metrics:churn
82
+
83
+ Files that change a lot in your project may be bad a sign. This task uses "svn log" to identify those files and put them in a report. The default is to start counting changes from the beginning of your project, which might be too far back so you can change like so:
84
+
85
+ namespace :metrics do
86
+ CHURN_OPTIONS = { :start_date => lambda{3.months.ago} }
87
+ end
88
+
89
+ The Proc is there because '3.months.ago' only works when after the Rails Environment is loaded (and Rails extends Fixnum) which I didn't want to do every time you run a rake task.
90
+
91
+ You can also change the minimum churn count like so:
92
+
93
+ namespace :metrics do
94
+ CHURN_OPTIONS = { :minimum_churn_count => 3 }
95
+ end
96
+
97
+
98
+ *Thanks
99
+
100
+ I'd like to thank the authors of Saikuro, Flog, Rcov, CruiseControl.rb, and Rails for creating such excellent open source products. Also Michael Schubert, Kurtis Seebaldt, Toby Tripp, Paul Gross, and Chirdeep Shetty for their help and advice.
data/TODO.txt ADDED
@@ -0,0 +1,4 @@
1
+ == TODO list
2
+
3
+ * Integrate MD5 hashing with remainder of reports
4
+ * Update README to more accurately reflect current development
data/lib/metric_fu.rb ADDED
@@ -0,0 +1,7 @@
1
+ module MetricFu
2
+ BASE_DIRECTORY = ENV['CC_BUILD_ARTIFACTS'] || 'tmp/metric_fu'
3
+ end
4
+
5
+ require File.join(File.dirname(__FILE__), 'metric_fu', 'md5_tracker')
6
+ require File.join(File.dirname(__FILE__), 'metric_fu', 'flog_reporter')
7
+ require File.join(File.dirname(__FILE__), 'tasks', 'metric_fu')
@@ -0,0 +1,5 @@
1
+ require File.join(File.dirname(__FILE__), 'flog_reporter', 'base')
2
+ require File.join(File.dirname(__FILE__), 'flog_reporter', 'page')
3
+ require File.join(File.dirname(__FILE__), 'flog_reporter', 'scanned_method')
4
+ require File.join(File.dirname(__FILE__), 'flog_reporter', 'operator')
5
+ require File.join(File.dirname(__FILE__), 'flog_reporter', 'generator')
@@ -0,0 +1,59 @@
1
+ module MetricFu::FlogReporter
2
+
3
+ THRESHOLD = (ENV['FLOG_THRESHOLD'] || 120)
4
+ SCORE_FORMAT = "%0.2f"
5
+
6
+ class InvalidFlog < RuntimeError
7
+ end
8
+
9
+ class Base
10
+ MODULE_NAME = "([A-Z][a-z]+)+"
11
+ METHOD_NAME = "#([a-z]+_?)+"
12
+ SCORE = "\\d+\\.\\d+"
13
+
14
+ METHOD_NAME_RE = Regexp.new("#{MODULE_NAME}#{METHOD_NAME}")
15
+ SCORE_RE = Regexp.new(SCORE)
16
+
17
+ METHOD_LINE_RE = Regexp.new("#{MODULE_NAME}#{METHOD_NAME}:\\s\\(#{SCORE}\\)")
18
+ OPERATOR_LINE_RE = Regexp.new("\\s+(#{SCORE}):\\s(.*)$")
19
+
20
+ class << self
21
+ def cycle(first_value, second_value, iteration)
22
+ return first_value if iteration % 2 == 0
23
+ return second_value
24
+ end
25
+
26
+ def load_css(css_file = nil)
27
+ filepath = css_file || File.join(File.dirname(__FILE__), 'flog_reporter.css')
28
+ css = ""
29
+ file = File.open(filepath, "r")
30
+ file.each_line { |line| css << line }
31
+ file.close
32
+ css
33
+ end
34
+
35
+ def parse(text)
36
+ score = text[/score = (\d+\.\d+)/, 1]
37
+ return nil unless score
38
+ page = Page.new(score)
39
+
40
+ text.each_line do |method_line|
41
+ if METHOD_LINE_RE =~ method_line and
42
+ method_name = method_line[METHOD_NAME_RE] and
43
+ score = method_line[SCORE_RE]
44
+ page.scanned_methods << ScannedMethod.new(method_name, score)
45
+ end
46
+
47
+ if OPERATOR_LINE_RE =~ method_line and
48
+ operator = method_line[OPERATOR_LINE_RE, 2] and
49
+ score = method_line[SCORE_RE]
50
+ raise InvalidFlog if page.scanned_methods.empty?
51
+ page.scanned_methods.last.operators << Operator.new(score, operator)
52
+ end
53
+ end
54
+
55
+ page
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,39 @@
1
+ body {
2
+ background-color: rgb(240, 240, 245);
3
+ font-family: verdana, arial, helvetica;
4
+ }
5
+
6
+ table {
7
+ border-collapse: collapse;
8
+ }
9
+
10
+ table.report {
11
+ width: 100%;
12
+ }
13
+
14
+ table th {
15
+ text-align: center;
16
+ }
17
+
18
+ table td.score {
19
+ text-align: right;
20
+ }
21
+
22
+ table th {
23
+ background: #dcecff;
24
+ border: #d0d0d0 1px solid;
25
+ font-weight: bold;
26
+ }
27
+
28
+ table td {
29
+ border: #d0d0d0 1px solid;
30
+ }
31
+
32
+ table tr.light {
33
+ background-color: rgb(240, 240, 245);
34
+ }
35
+
36
+ table tr.dark {
37
+ background-color: rgb(230, 230, 235);
38
+ }
39
+
@@ -0,0 +1,71 @@
1
+ module MetricFu::FlogReporter
2
+ class Generator
3
+ class << self
4
+ def generate_report(base_dir)
5
+ flog_hashes = []
6
+ Dir.glob("#{base_dir}/**/*.txt").each do |filename|
7
+ content = ""
8
+ File.open(filename, "r").each_line do |file|
9
+ content << file
10
+ end
11
+
12
+ begin
13
+ page = Base.parse(content)
14
+ rescue InvalidFlog
15
+ puts "Invalid flog for #{filename}"
16
+ next
17
+ end
18
+
19
+ next unless page
20
+
21
+ if MetricFu::MD5Tracker.file_already_counted?(filename)
22
+ flog_hashes << {
23
+ :page => page,
24
+ :path => filename.sub('.txt', '.html').sub("#{base_dir}/", "")
25
+ }
26
+ else
27
+ flog_hashes << generate_page(filename, page, base_dir)
28
+ end
29
+ end
30
+
31
+ generate_index(flog_hashes, base_dir)
32
+ end
33
+
34
+ def generate_page(filename, page, base_dir)
35
+ html_file = File.new(filename.gsub(/\.txt/, '.html'), "w")
36
+ html_file.puts page.to_html
37
+ html_file.close
38
+ return { :path => html_file.path.sub("#{base_dir}/", ''),
39
+ :page => page }
40
+ end
41
+
42
+ def generate_index(flog_hashes, base_dir)
43
+ html = "<html><head><title>Flog Reporter</title><style>"
44
+ html << Base.load_css
45
+ html << "</style></head><body>"
46
+ html << "<p><strong>Flogged files</strong></p>\n"
47
+ html << "<p>Generated on #{Time.now.localtime} with <a href='http://ruby.sadi.st/Flog.html'>flog</a></p>\n"
48
+ html << "<table class='report'>\n"
49
+ html << "<tr><th>File</th><th>Total score</th><th>Methods</th><th>Average score</th><th>Highest score</th></tr>"
50
+ count = 0
51
+ flog_hashes.each do |flog_hash|
52
+ html << <<-EOF
53
+ <tr class='#{Base.cycle("light", "dark", count)}'>
54
+ <td><a href='#{flog_hash[:path]}'>#{flog_hash[:path].sub('.html', '.rb')}</a></td>
55
+ <td class='score'>#{sprintf(SCORE_FORMAT, flog_hash[:page].score)}</td>
56
+ <td class='score'>#{flog_hash[:page].scanned_methods.length}</td>
57
+ <td class='score'>#{sprintf(SCORE_FORMAT, flog_hash[:page].average_score)}</td>
58
+ <td class='score'>#{sprintf(SCORE_FORMAT, flog_hash[:page].highest_score)}</td>
59
+ </tr>
60
+ EOF
61
+ count += 1
62
+ end
63
+ html << "</table>\n"
64
+ html << "</body></html>\n"
65
+ index = File.new("#{base_dir}/index.html", "w")
66
+ index.puts html
67
+ index.close
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,10 @@
1
+ module MetricFu::FlogReporter
2
+ class Operator
3
+ attr_accessor :score, :operator
4
+
5
+ def initialize(score, operator)
6
+ @score = score.to_f
7
+ @operator = operator
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,36 @@
1
+ module MetricFu::FlogReporter
2
+ class Page
3
+ attr_accessor :score, :scanned_methods
4
+
5
+ def initialize(score, scanned_methods = [])
6
+ @score = score.to_f
7
+ @scanned_methods = scanned_methods
8
+ end
9
+
10
+ def to_html
11
+ output = "<html><head><style>"
12
+ output << Base.load_css
13
+ output << "</style></head><body>"
14
+ output << "Score: #{score}\n"
15
+ scanned_methods.each do |sm|
16
+ output << sm.to_html
17
+ end
18
+ output << "</body></html>"
19
+ output
20
+ end
21
+
22
+ def average_score
23
+ sum = 0
24
+ scanned_methods.each do |m|
25
+ sum += m.score
26
+ end
27
+ sum / scanned_methods.length
28
+ end
29
+
30
+ def highest_score
31
+ scanned_methods.inject(0) do |highest, m|
32
+ m.score > highest ? m.score : highest
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,28 @@
1
+ module MetricFu::FlogReporter
2
+ class ScannedMethod
3
+ attr_accessor :name, :score, :operators
4
+
5
+ def initialize(name, score, operators = [])
6
+ @name = name
7
+ @score = score.to_f
8
+ @operators = operators
9
+ end
10
+
11
+ def to_html
12
+ output = "<p><strong>#{name} (#{score})</strong></p>\n"
13
+ output << "<table>\n"
14
+ output << "<tr><th>Score</th><th>Operator</th></tr>\n"
15
+ count = 0
16
+ operators.each do |operator|
17
+ output << <<-EOF
18
+ <tr class='#{Base.cycle("light", "dark", count)}'>
19
+ <td class='score'>#{sprintf(SCORE_FORMAT, operator.score)}</td>
20
+ <td class='score'>#{operator.operator}</td>
21
+ </tr>
22
+ EOF
23
+ count += 1
24
+ end
25
+ output << "</table>\n\n"
26
+ end
27
+ end
28
+ end