public
Rubygem
Description: simple database query profiling tool
Clone URL: git://github.com/quake/db_profiling.git
db_profiling / lib / db_profiling / db_profiling.rb
100644 59 lines (51 sloc) 1.494 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
ActiveRecord::ConnectionAdapters::QueryCache.module_eval do
  @@stats_hits = []
  @@stats_misses = []
  @@stats_bytes = @@stats_rows = 0
  
  def self.get_stats
    { :hits => @@stats_hits,
      :misses => @@stats_misses,
      :rows => @@stats_rows,
      :bytes => @@stats_bytes,
      :queries => @@stats_hits.size + @@stats_misses.size,
    }
  end
  
  def self.reset_stats
    @@stats_hits = []
    @@stats_misses = []
    @@stats_bytes = @@stats_rows = 0
  end
  
  def cache_sql_with_stats(sql, &block)
    if @query_cache.has_key?(sql)
      @@stats_hits << [sql, extract_app_caller]
    else
      @@stats_misses << [sql, extract_app_caller]
    end
    bytes = 0
    rows = cache_sql_without_stats(sql, &block)
    rows.each do |row|
      row.each do |key, value|
        bytes += key.length
        bytes += value.length if value
      end
    end
    @@stats_rows += rows.length
    @@stats_bytes += bytes
    rows
  end
  alias_method_chain :cache_sql, :stats
 
  def extract_app_caller
    caller.select {|s| s.index("#{RAILS_ROOT}/app") == 0}
  end
end
 
ActionController::Base.module_eval do
  def perform_action_with_reset
    ActiveRecord::ConnectionAdapters::QueryCache::reset_stats
    perform_action_without_reset
  end
 
  alias_method_chain :perform_action, :reset
 
  def active_record_runtime(runtime)
    stats = ActiveRecord::ConnectionAdapters::QueryCache::get_stats
    "#{super} #{sprintf("%.1fk", stats[:bytes].to_f / 1024)} queries: #{stats[:queries]}"
  end
end