Skip to content
This repository has been archived by the owner on Mar 6, 2023. It is now read-only.

Commit

Permalink
Merge pull request #8 from TheTeaNerd/better_summary
Browse files Browse the repository at this point in the history
Better summary reporting
  • Loading branch information
TheTeaNerd committed Dec 1, 2014
2 parents 74e582a + 1a33440 commit afb0427
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 32 deletions.
14 changes: 9 additions & 5 deletions lib/factory_inspector/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,24 @@ def self.default_report_path
File.expand_path default_report_file, root_path
end

def self.default_warnings_log_path
File.expand_path default_warnings_log_file, root_path
def self.default_warnings_path
File.expand_path default_warnings_file, root_path
end

def self.default_report_file
'log/factory_inspector.log'
'log/factory_inspector.txt'
end

def self.default_warnings_log_file
'log/factory_inspector_warnings.log'
def self.default_warnings_file
'log/factory_inspector_warnings.txt'
end

def self.root_path
File.expand_path Dir.getwd
end

def self.summary_size
5
end
end
end
50 changes: 27 additions & 23 deletions lib/factory_inspector/inspector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
module FactoryInspector
# Inspects the Factory via the callback `analyze`
class Inspector
include Term::ANSIColor
class ::String
include Term::ANSIColor
end

def initialize
@here = Dir.getwd
@local_call = /\A#{@here}/
@reports = {}
instrument_factory_girl
@warnings = []
Expand All @@ -19,10 +22,13 @@ def initialize
def generate_summary
return if @reports.empty?

puts
puts bold + header + clear
sorted_reports.take(summary_size).each { |_, report| puts report }
puts " (Slowest sorted by #{cyan + sort_description + clear}.)"
puts "\n#{header(highlighted: true)}"
slowest_reports.each { |_, report| puts report }
puts " (Slowest sorted by #{sort_description.to_s.cyan}.)"
end

def slowest_reports
sorted_reports.take Configuration.summary_size
end

def generate_report(filename: Configuration.default_report_path)
Expand All @@ -41,10 +47,10 @@ def generate_report(filename: Configuration.default_report_path)
end
file.close

print "\nFull report in '#{cyan + relative(filename) + clear}'"
print "\nFull report in '#{relative(filename).to_s.cyan}'"
end

def generate_warnings_log(filename: Configuration.default_warnings_log_path)
def generate_warnings_log(filename: Configuration.default_warnings_path)
return if @warnings.empty?

file = File.open(filename, 'w')
Expand All @@ -55,7 +61,7 @@ def generate_warnings_log(filename: Configuration.default_warnings_log_path)
end
file.close

puts "#{@warnings.size} warnings in '#{cyan + relative(filename) + clear}'"
print "\n#{@warnings.size} warning(s) in '#{relative(filename).to_s.cyan}'"
end

# Callback for use by ActiveSupport::Notifications.
Expand Down Expand Up @@ -95,31 +101,29 @@ def relative(filename)
end

def call_stack
caller.grep(/#{@here}/).map do |call|
call.gsub(/\A#{@here}\/(.+)(:\d+):.+\z/, '\1\2')
caller.grep(@local_call).map do |call|
call.gsub(/#{@local_call}\/(.+):(\d+):.+\z/, '\1:\2')
end
end

def summary_size
5
end

def sorted_reports
@sorted_reports ||= @reports.sort_by { |_, v| v }.reverse
@sorted_reports ||= @reports.sort_by { |_, report| report }.reverse
end

def sort_description
FactoryInspector::Report.sort_description
end

def header
'FACTORY INSPECTOR: ' \
"#{@reports.size} factories used, " \
"#{total_calls} calls made over #{pretty_total_time}\n\n" \
' FACTORY NAME ' \
"TOTAL TOTAL TIME PER LONGEST STRATEGIES\n" \
' ' \
"CALLS TIME (s) CALL (s) CALL (s) USED\n"
def header(highlighted: false)
string = "FACTORY INSPECTOR: ".bold +
@reports.size.to_s.cyan + " factories used, ".bold +
total_calls.to_s.cyan + " calls made over ".bold +
pretty_total_time.to_s.cyan + "\n\n" +
" FACTORY NAME TOTAL TOTAL TIME PER LONGEST STRATEGIES\n".bold +
' CALLS TIME (s) CALL (s) CALL (s) USED'.bold +
"\n".reset

highlighted ? string : string.uncolored
end

def instrument_factory_girl
Expand Down
14 changes: 11 additions & 3 deletions lib/factory_inspector/report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ def initialize(factory_name: 'unknown')
end

def all_calls
callers.map do |stack|
stack.join(' -> ').gsub(/\A/, ' ')
end.join("\n") + "\n"
calls_by_count.reduce('') do |memo, (call, count)|
memo += "%5s call(s):#{call}\n" % count
memo
end
end

def time_per_call
Expand Down Expand Up @@ -66,5 +67,12 @@ def record_time(time: 0)
@worst_time = time if time > @worst_time
@total_time += time
end

def calls_by_count
callers.map { |stack| stack.join(' -> ').gsub(/\A/, ' ') }
.each_with_object(Hash.new(0)) { |call, counts| counts[call] += 1 }
.sort_by { |_call, count| count }
.reverse
end
end
end
2 changes: 1 addition & 1 deletion lib/factory_inspector/version.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Gem version number
module FactoryInspector
VERSION = '0.6.0'
VERSION = '0.7.0'
end

0 comments on commit afb0427

Please sign in to comment.