active_record_query_counter 2.0.0 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +10 -0
- data/VERSION +1 -1
- data/active_record_query_counter.gemspec +1 -1
- data/lib/active_record_query_counter/counter.rb +14 -1
- data/lib/active_record_query_counter.rb +20 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9d653eb9665511008bc78023c7e39ae8eb189e051847d83e16244424b5cbe595
|
4
|
+
data.tar.gz: 6e9643c840a635228ea0a04cb02277b91c34b1ea5e69bd5b93753066be8ab4fb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: df192239b02e3ed039eac91a64393d5332cad36dce242e9e02948b3ec0cbe23e7ae5d4b23dc58ebf01158fa885680135e6719f444a871abd37779e85a9f85d2d
|
7
|
+
data.tar.gz: '069e6959ba3f7ead49824ae91d35774b3b336afe18248c84800eaca65115daa040bdf5871d0c3e591b648cce2b0d5414078fe51ef39bef8d9f0e777be8e7330d'
|
data/CHANGELOG.md
CHANGED
@@ -4,6 +4,16 @@ All notable changes to this project will be documented in this file.
|
|
4
4
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
5
5
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
6
6
|
|
7
|
+
## 2.1.0
|
8
|
+
|
9
|
+
### Added
|
10
|
+
|
11
|
+
- Added count of queries that hit the query cache instead of being sent to the database.
|
12
|
+
|
13
|
+
### Removed
|
14
|
+
|
15
|
+
- Dropped support for ActiveRecord 5.0.
|
16
|
+
|
7
17
|
## 2.0.0
|
8
18
|
|
9
19
|
### Added
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.
|
1
|
+
2.1.0
|
@@ -3,17 +3,30 @@
|
|
3
3
|
module ActiveRecordQueryCounter
|
4
4
|
# Data structure for storing query information encountered within a block.
|
5
5
|
class Counter
|
6
|
-
attr_accessor :query_count, :row_count, :query_time
|
6
|
+
attr_accessor :query_count, :row_count, :query_time, :cached_query_count
|
7
7
|
attr_reader :thresholds
|
8
8
|
|
9
9
|
def initialize
|
10
10
|
@query_count = 0
|
11
11
|
@row_count = 0
|
12
12
|
@query_time = 0.0
|
13
|
+
@cached_query_count = 0
|
13
14
|
@transactions_hash = {}
|
14
15
|
@thresholds = ActiveRecordQueryCounter.default_thresholds.dup
|
15
16
|
end
|
16
17
|
|
18
|
+
# Return the percentage of queries that used the query cache instead of going to the database.
|
19
|
+
#
|
20
|
+
# @return [Float]
|
21
|
+
def cache_hit_rate
|
22
|
+
total_queries = query_count + cached_query_count
|
23
|
+
if total_queries > 0
|
24
|
+
(cached_query_count.to_f / total_queries)
|
25
|
+
else
|
26
|
+
0.0
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
17
30
|
# Return an array of transaction information for any transactions that have been tracked
|
18
31
|
# by the counter.
|
19
32
|
#
|
@@ -19,7 +19,7 @@ require_relative "active_record_query_counter/version"
|
|
19
19
|
# puts ActiveRecordQueryCounter.row_count
|
20
20
|
# end
|
21
21
|
module ActiveRecordQueryCounter
|
22
|
-
IGNORED_STATEMENTS = %w[
|
22
|
+
IGNORED_STATEMENTS = %w[SCHEMA EXPLAIN].freeze
|
23
23
|
private_constant :IGNORED_STATEMENTS
|
24
24
|
|
25
25
|
class << self
|
@@ -107,6 +107,16 @@ module ActiveRecordQueryCounter
|
|
107
107
|
counter.query_count if counter.is_a?(Counter)
|
108
108
|
end
|
109
109
|
|
110
|
+
# Return the number of queries that hit the query cache and were not sent to the database
|
111
|
+
# that have been counted within the current block. Returns nil if not inside a block where
|
112
|
+
# queries are being counted.
|
113
|
+
#
|
114
|
+
# @return [Integer, nil]
|
115
|
+
def cached_query_count
|
116
|
+
counter = current_counter
|
117
|
+
counter.cached_query_count if counter.is_a?(Counter)
|
118
|
+
end
|
119
|
+
|
110
120
|
# Return the number of rows that have been counted within the current block.
|
111
121
|
# Returns nil if not inside a block where queries are being counted.
|
112
122
|
#
|
@@ -182,6 +192,8 @@ module ActiveRecordQueryCounter
|
|
182
192
|
query_count: counter.query_count,
|
183
193
|
row_count: counter.row_count,
|
184
194
|
query_time: counter.query_time,
|
195
|
+
cached_query_count: counter.cached_query_count,
|
196
|
+
cache_hit_rate: counter.cache_hit_rate,
|
185
197
|
transaction_count: counter.transaction_count,
|
186
198
|
transaction_time: counter.transaction_time
|
187
199
|
}
|
@@ -213,6 +225,13 @@ module ActiveRecordQueryCounter
|
|
213
225
|
unless ActiveRecord::ConnectionAdapters::TransactionManager.include?(TransactionManagerExtension)
|
214
226
|
ActiveRecord::ConnectionAdapters::TransactionManager.prepend(TransactionManagerExtension)
|
215
227
|
end
|
228
|
+
|
229
|
+
@cache_subscription ||= ActiveSupport::Notifications.subscribe("sql.active_record") do |_name, _start_time, _end_time, _id, payload|
|
230
|
+
if payload[:cached]
|
231
|
+
counter = current_counter
|
232
|
+
counter.cached_query_count += 1 if counter
|
233
|
+
end
|
234
|
+
end
|
216
235
|
end
|
217
236
|
|
218
237
|
private
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_record_query_counter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brian Durand
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-09-
|
11
|
+
date: 2023-09-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '5.
|
19
|
+
version: '5.1'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '5.
|
26
|
+
version: '5.1'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|