class ActiveRecordFormatterHelpers::Report

Attributes

collector[R]
default_path[R]
report_dir[R]
report_path[R]

Public Class Methods

new(collector) click to toggle source
# File lib/rspec/activerecord/helpers/report.rb, line 5
def initialize(collector)
  @collector = collector

  @report_dir   = Rails.root.join("tmp", "profile")
  @report_path  = report_dir.join(timestamped_filename)
  @default_path = report_dir.join('ar_most_recent.txt')
end

Public Instance Methods

write() click to toggle source
# File lib/rspec/activerecord/helpers/report.rb, line 13
def write
  write_file(file_path: report_path)
  write_file(file_path: default_path)
end

Private Instance Methods

timestamped_filename() click to toggle source
# File lib/rspec/activerecord/helpers/report.rb, line 20
def timestamped_filename
  Time.now.strftime("ar_%Y_%m_%d_%H_%m_%S.txt")
end
write_file(file_path:) click to toggle source
# File lib/rspec/activerecord/helpers/report.rb, line 24
def write_file(file_path:)
  Dir.mkdir(report_dir) unless File.exists?(report_dir)

  File.open(file_path, "wb") do |f|
    f.puts "#{collector.total_objects} AR objects, #{collector.total_queries} AR queries"
    f.puts "#{collector.groups_encountered} example groups executed"

    f.puts ""
    f.puts "Worst Example Groups by Object Creation"
    collector.most_expensive_groups.first(50).each do |name, count|
      f.puts "%-5s %s" % [count, name]
    end

    f.puts ""
    f.puts "Most Common Queries"
    collector.most_common_query_names.first(50).each do |name, count|
      f.puts "%-5s %s" % [count, name]
    end
  end
end