Module: ActiveRecordQueryCounter

Defined in:
lib/active_record_query_counter.rb

Overview

Everything you need to count ActiveRecord queries and row counts within a block.

Usage:

ActiveRecordQueryCounter.count_queries do
  yield
  puts ActiveRecordQueryCounter.query_count
  puts ActiveRecordQueryCounter.row_count
end

Defined Under Namespace

Modules: ConnectionAdapterExtension, TransactionManagerExtension Classes: Counter, RackMiddleware, SidekiqMiddleware

Class Method Summary collapse

Class Method Details

.count_queriesObject

Enable query counting within a block.



35
36
37
38
39
40
41
42
43
# File 'lib/active_record_query_counter.rb', line 35

def count_queries
  current = Thread.current[:database_query_counter]
  begin
    Thread.current[:database_query_counter] = Counter.new
    yield
  ensure
    Thread.current[:database_query_counter] = current
  end
end

.enable!(connection_class) ⇒ Object

Enable the query counting behavior on a connection adapter class.



123
124
125
126
127
128
129
130
# File 'lib/active_record_query_counter.rb', line 123

def enable!(connection_class)
  unless connection_class.include?(ConnectionAdapterExtension)
    connection_class.prepend(ConnectionAdapterExtension)
  end
  unless ActiveRecord::ConnectionAdapters::TransactionManager.include?(TransactionManagerExtension)
    ActiveRecord::ConnectionAdapters::TransactionManager.prepend(TransactionManagerExtension)
  end
end

.increment(row_count, elapsed_time) ⇒ Object

Increment the query counters



46
47
48
49
50
51
52
53
# File 'lib/active_record_query_counter.rb', line 46

def increment(row_count, elapsed_time)
  counter = Thread.current[:database_query_counter]
  if counter.is_a?(Counter)
    counter.query_count += 1
    counter.row_count += row_count
    counter.query_time += elapsed_time
  end
end

.increment_transaction(elapsed_time) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/active_record_query_counter.rb', line 55

def increment_transaction(elapsed_time)
  counter = Thread.current[:database_query_counter]
  if counter.is_a?(Counter)
    trace = caller
    index = 0
    caller.each do |line|
      break unless line.start_with?(__FILE__)
      index += 1
    end
    trace = trace[index, trace.length]
    info = counter.transactions[trace]
    if info
      info[0] += 1
      info[1] += elapsed_time
    else
      info = [1, elapsed_time]
      counter.transactions[trace] = info
    end
  end
end

.infoObject

Return the query info as a hash with keys :query_count, :row_count, :query_time :transaction_count, and :transaction_type or nil if not inside a block where queries are being counted.



109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/active_record_query_counter.rb', line 109

def info
  counter = Thread.current[:database_query_counter]
  if counter.is_a?(Counter)
    {
      query_count: counter.query_count,
      row_count: counter.row_count,
      query_time: counter.query_time,
      transaction_count: counter.transaction_count,
      transaction_time: counter.transaction_time
    }
  end
end

.query_countObject



76
77
78
79
# File 'lib/active_record_query_counter.rb', line 76

def query_count
  counter = Thread.current[:database_query_counter]
  counter.query_count if counter.is_a?(Counter)
end

.query_timeObject



86
87
88
89
# File 'lib/active_record_query_counter.rb', line 86

def query_time
  counter = Thread.current[:database_query_counter]
  counter.query_time if counter.is_a?(Counter)
end

.row_countObject



81
82
83
84
# File 'lib/active_record_query_counter.rb', line 81

def row_count
  counter = Thread.current[:database_query_counter]
  counter.row_count if counter.is_a?(Counter)
end

.transaction_countObject



91
92
93
94
# File 'lib/active_record_query_counter.rb', line 91

def transaction_count
  counter = Thread.current[:database_query_counter]
  counter.transaction_count if counter.is_a?(Counter)
end

.transaction_timeObject



96
97
98
99
# File 'lib/active_record_query_counter.rb', line 96

def transaction_time
  counter = Thread.current[:database_query_counter]
  counter.transaction_time if counter.is_a?(Counter)
end

.transactionsObject



101
102
103
104
# File 'lib/active_record_query_counter.rb', line 101

def transactions
  counter = Thread.current[:database_query_counter]
  counter.transactions.dup if counter.is_a?(Counter)
end