active_record_query_counter 2.0.0 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: eb19f1f7e8a7678f3cdf3079b05e9aa26353b33b809c240943e0899f584dac6a
4
- data.tar.gz: 7bcbce0b2d06c4f871074898651071f1400cd87c7ee8ee514837ee80caac6774
3
+ metadata.gz: 9d653eb9665511008bc78023c7e39ae8eb189e051847d83e16244424b5cbe595
4
+ data.tar.gz: 6e9643c840a635228ea0a04cb02277b91c34b1ea5e69bd5b93753066be8ab4fb
5
5
  SHA512:
6
- metadata.gz: 80c468ec96c069bb4043a0e210d4fb4b1d82714d2aa0b69bd07ac9fd23bb04255ffcf4b175fcfa66b7aa0afd8434c211645e70f4fd8d05ad9c10faa4ee95c679
7
- data.tar.gz: fb972d526b98c97f98230a29eb9412726746a58c3d4a05406ab9ccc046d6eb0ec5007fa8d52d26a40c9a966ad2af4c50c045c1d9257cd15ce90dd21c5dedbcfa
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.0.0
1
+ 2.1.0
@@ -28,7 +28,7 @@ Gem::Specification.new do |spec|
28
28
 
29
29
  spec.require_paths = ["lib"]
30
30
 
31
- spec.add_dependency "activerecord", ">= 5.0"
31
+ spec.add_dependency "activerecord", ">= 5.1"
32
32
 
33
33
  spec.add_development_dependency "bundler"
34
34
 
@@ -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[CACHE SCHEMA EXPLAIN].freeze
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.0.0
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-09 00:00:00.000000000 Z
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.0'
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.0'
26
+ version: '5.1'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement