Class: ActiveRecordQueryCounter::Counter

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record_query_counter/counter.rb

Overview

Data structure for storing query information encountered within a block.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCounter

Returns a new instance of Counter.



9
10
11
12
13
14
15
16
17
# File 'lib/active_record_query_counter/counter.rb', line 9

def initialize
  @query_count = 0
  @row_count = 0
  @query_time = 0.0
  @cached_query_count = 0
  @transactions_hash = {}
  @thresholds = ActiveRecordQueryCounter.default_thresholds.dup
  @rollback_count = 0
end

Instance Attribute Details

#cached_query_countObject

Returns the value of attribute cached_query_count.



6
7
8
# File 'lib/active_record_query_counter/counter.rb', line 6

def cached_query_count
  @cached_query_count
end

#query_countObject

Returns the value of attribute query_count.



6
7
8
# File 'lib/active_record_query_counter/counter.rb', line 6

def query_count
  @query_count
end

#query_timeObject

Returns the value of attribute query_time.



6
7
8
# File 'lib/active_record_query_counter/counter.rb', line 6

def query_time
  @query_time
end

#rollback_countObject

Returns the value of attribute rollback_count.



6
7
8
# File 'lib/active_record_query_counter/counter.rb', line 6

def rollback_count
  @rollback_count
end

#row_countObject

Returns the value of attribute row_count.



6
7
8
# File 'lib/active_record_query_counter/counter.rb', line 6

def row_count
  @row_count
end

#thresholdsObject (readonly)

Returns the value of attribute thresholds.



7
8
9
# File 'lib/active_record_query_counter/counter.rb', line 7

def thresholds
  @thresholds
end

Instance Method Details

#add_transaction(trace:, start_time:, end_time:) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Add a tracked transaction.

Parameters:

  • trace (Array<String>)

    the trace of the transaction

  • start_time (Float)

    the monotonic time when the transaction began

  • end_time (Float)

    the monotonic time when the transaction ended



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/active_record_query_counter/counter.rb', line 46

def add_transaction(trace:, start_time:, end_time:)
  trace_transactions = @transactions_hash[trace]
  if trace_transactions
    # Memory optimization so that we don't store duplicate traces for every transaction in a loop.
    trace = trace_transactions.first.trace
  else
    trace_transactions = []
    @transactions_hash[trace] = trace_transactions
  end

  trace_transactions << TransactionInfo.new(start_time: start_time, end_time: end_time, trace: trace)
end

#cache_hit_rateFloat

Return the percentage of queries that used the query cache instead of going to the database.

Returns:

  • (Float)


22
23
24
25
26
27
28
29
# File 'lib/active_record_query_counter/counter.rb', line 22

def cache_hit_rate
  total_queries = query_count + cached_query_count
  if total_queries > 0
    (cached_query_count.to_f / total_queries)
  else
    0.0
  end
end

#first_transaction_start_timeFloat?

Get the time when the first transaction began.

Returns:

  • (Float, nil)

    the monotonic time when the first transaction began, or nil if no transactions have been tracked



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

def first_transaction_start_time
  transactions.first&.start_time
end

#last_transaction_end_timeFloat?

Get the time when the last transaction completed.

Returns:

  • (Float, nil)

    the monotonic time when the first transaction completed, or nil if no transactions have been tracked



85
86
87
# File 'lib/active_record_query_counter/counter.rb', line 85

def last_transaction_end_time
  transactions.max_by(&:end_time)&.end_time
end

#transaction_countInteger

Return the number of transactions that have been tracked by the counter.

Returns:

  • (Integer)


62
63
64
# File 'lib/active_record_query_counter/counter.rb', line 62

def transaction_count
  @transactions_hash.values.flatten.size
end

#transaction_timeFloat

Return the total time spent in transactions that have been tracked by the counter.

Returns:

  • (Float)


69
70
71
# File 'lib/active_record_query_counter/counter.rb', line 69

def transaction_time
  @transactions_hash.values.flatten.sum(&:elapsed_time)
end

#transactionsArray<ActiveRecordQueryCounter::TransactionInfo>

Return an array of transaction information for any transactions that have been tracked by the counter.



35
36
37
# File 'lib/active_record_query_counter/counter.rb', line 35

def transactions
  @transactions_hash.values.flatten.sort_by(&:start_time)
end