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.
-
#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 |
# 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 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 |
#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.
45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/active_record_query_counter/counter.rb', line 45 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.
21 22 23 24 25 26 27 28 |
# File 'lib/active_record_query_counter/counter.rb', line 21 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.
76 77 78 |
# File 'lib/active_record_query_counter/counter.rb', line 76 def first_transaction_start_time transactions.first&.start_time end |
#last_transaction_end_time ⇒ Float?
Get the time when the last transaction completed.
84 85 86 |
# File 'lib/active_record_query_counter/counter.rb', line 84 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.
61 62 63 |
# File 'lib/active_record_query_counter/counter.rb', line 61 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.
68 69 70 |
# File 'lib/active_record_query_counter/counter.rb', line 68 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.
34 35 36 |
# File 'lib/active_record_query_counter/counter.rb', line 34 def transactions @transactions_hash.values.flatten.sort_by(&:start_time) end |