I wrote a simple ruby script that gathers stats from my app
def file_stats(files)
stats = {"lines" => 0, "comments" => 0, "loc" => 0, "classes" => 0, "methods" => 0}
files.each do |filename|
File.open(filename, "r") do |file|
while line = file.gets
stats["lines"] += 1
next if line =~ /^\s*$/
if line =~ /^\s*#/
stats["comments"] += 1
elsif line !~ /^\s*$/
stats["loc"] += 1
stats["classes"] += 1 if line =~ /class [A-Z]/
stats["methods"] += 1 if line =~ /def [a-z]/
end
end
end
end
stats
end
Nothing special here. Further work was to write some script that browsed through code revisions and generated the stats for each rev. This was also pretty easy, even shell script would be sufficient, but I also ruby-scripted it:
if __FILE__ == $0
revno = `bzr revno`.to_i
1.upto(revno) do |i|
`bzr revert -r revno:#{i}`
stats = CodeStatistics.new(
["app/controllers", "spec/controllers"],
["app/models", "spec/models"],
["app/helpers", "spec/helpers"],
["lib", "spec/libs"],
[nil, "spec/routes"],
[nil, "spec/other"],
[nil, "spec/views"])
File.open("data.txt", "a") do |f|
f.write("#{i}\t#{stats.code_loc}\t#{stats.test_loc}\n")
end
end
end
First it gets number of all revisions (237). Then iterates from 1 upto it and reverts the code to i-th revision. Simple. The output is appended to file in some easy-to-read-by-gnuplot format.
That part of work proved once again that bazaar version control is a great tool. It's very easy to use, no hard-to-remember switches, if you want to revert your code, just type bzr revert. And that's it, _all_ your code gets reverted. No 'recursive' switch required, no need to specify folder (like in svn) -- what would you expect to be done with bzr revert?! Revert first file, or some other repository?
Ok, back to generating stats. I was pretty much ready with everything I needed, opening gnuplot and writing
# set axis, ticks, png output etc.
plot 'data.txt' using 1:3 title 'Tests' with lines, 'data.txt' using 1:2 title 'Code' with lines
created this one:

As You can see the whole app is about 1.5k LOC, with 237 revisions, that gives us approx. 6.3 lines of code added per revision. Test to code ratio is currently slightly over 2.0.
Also interesting thing is the latest 20-30 revisions. I refactored a bit of code and removed unnecessary one reducing the line count adding few tests at the same time. This is motivating ;)

The test to code ratio throughout time shows that it's generally increasing, though there were some hops on the beginning. It's because there weren't lots of code there and the ratio could be changed a lot by adding few tests. It settled about 1.3 at rev 60-70 and increased since then to reach 2.0. You can see the refactoring here (slope is big at the end).
I'll try to come up with other things related to this topic as it's very interesting, though it would be even more if it was done on big project with more than one programmer, agile methodology (burndown chart for example).
0 comment(s):
Post a Comment