Class: ActiveRecordQueryCounter::Counter
- Inherits:
-
Object
- Object
- ActiveRecordQueryCounter::Counter
- Defined in:
- lib/active_record_query_counter/counter.rb
Overview
Data structure for storing query information encountered within a block.
Instance Attribute Summary collapse
-
#cached_query_count ⇒ Object
Returns the value of attribute cached_query_count.
-
#query_count ⇒ Object
Returns the value of attribute query_count.
-
#query_time ⇒ Object
Returns the value of attribute query_time.
-
#rollback_count ⇒ Object
Returns the value of attribute rollback_count.
-
#row_count ⇒ Object
Returns the value of attribute row_count.
-
#thresholds ⇒ Object
readonly
Returns the value of attribute thresholds.
Instance Method Summary collapse
-
#add_transaction(trace:, start_time:, end_time:) ⇒ void
private
Add a tracked transaction.
-
#cache_hit_rate ⇒ Float
Return the percentage of queries that used the query cache instead of going to the database.
-
#first_transaction_start_time ⇒ Float?
Get the time when the first transaction began.
-
#initialize ⇒ Counter
constructor
A new instance of Counter.
-
#last_transaction_end_time ⇒ Float?
Get the time when the last transaction completed.
-
#transaction_count ⇒ Integer
Return the number of transactions that have been tracked by the counter.
-
#transaction_time ⇒ Float
Return the total time spent in transactions that have been tracked by the counter.
-
#transactions ⇒ Array<ActiveRecordQueryCounter::TransactionInfo>
Return an array of transaction information for any transactions that have been tracked by the counter.
Constructor Details
#initialize ⇒ Counter
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_count ⇒ Object
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_count ⇒ Object
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_time ⇒ Object
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_count ⇒ Object
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_count ⇒ Object
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 |
#thresholds ⇒ Object (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.
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_rate ⇒ Float
Return the percentage of queries that used the query cache instead of going to the database.
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_time ⇒ Float?
Get the time when the first transaction began.
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_time ⇒ Float?
Get the time when the last transaction completed.
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_count ⇒ Integer
Return the number of transactions that have been tracked by the counter.
62 63 64 |
# File 'lib/active_record_query_counter/counter.rb', line 62 def transaction_count @transactions_hash.values.flatten.size end |
#transaction_time ⇒ Float
Return the total time spent in transactions that have been tracked by the counter.
69 70 71 |
# File 'lib/active_record_query_counter/counter.rb', line 69 def transaction_time @transactions_hash.values.flatten.sum(&:elapsed_time) end |
#transactions ⇒ Array<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 |