active_record_query_counter 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a39b68fd4dd57ebee4f0f338f7a4a3dab45a044fd62739f79113a6caa4d5080a
4
+ data.tar.gz: 6911749f1967b8b0a25e8d6dc3ef557bb81ce45526194a5ecaad71bfb4c75f5f
5
+ SHA512:
6
+ metadata.gz: 056e36015d9a3e825ae0f24d05a25e2bba69fdb52a19ff5bf54508d743998cf4a25ab0fe74eb68637a10b86da69298c284eaa61fd0ae565060d66e7c7306ddc9
7
+ data.tar.gz: f1c790acb2a831b86ec9bc5a5a65d73675177262c0fe75b2f0a6141cc1df48ec843742f2a41e9802a6f0cb41d405f091aa9a31aa86e476022b31c1ce69442bf1
@@ -0,0 +1,3 @@
1
+ # 1.0.0
2
+
3
+ * Initial release
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2020 Brian Durand
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,53 @@
1
+ # ActiveRecordQueryCounter
2
+
3
+ [![Build Status](https://travis-ci.org/bdurand/active_record_query_counter.svg?branch=master)](https://travis-ci.org/bdurand/active_record_query_counter)
4
+ [![Maintainability](https://api.codeclimate.com/v1/badges/21094ecec0c151983bb1/maintainability)](https://codeclimate.com/github/bdurand/active_record_query_counter/maintainability)
5
+
6
+ This gem injects itself into ActiveRecord to count the number of queries, the number of rows returned, and the amount of time spent on queries within a block.
7
+
8
+ The intended use is to gather instrumentation stats for finding hot spots in your code.
9
+
10
+ ## Usage
11
+
12
+ The behavior must be enabled on your database connection adapter from within an initializer.
13
+
14
+ Postgres:
15
+
16
+ ```ruby
17
+ ActiveRecordQueryCounter.enable!(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter)
18
+ ```
19
+
20
+ MySQL:
21
+
22
+ ```ruby
23
+ ActiveRecordQueryCounter.enable!(ActiveRecord::ConnectionAdapters::Mysql2Adapter)
24
+ ```
25
+
26
+ Next you must specify the blocks where you want to count queries.
27
+
28
+ ```ruby
29
+ ActiveRecordQueryCounter.count_queries do
30
+ do_something
31
+ puts "Queries: #{ActiveRecordQueryCounter.query_count}"
32
+ puts "Rows: #{ActiveRecordQueryCounter.row_count}"
33
+ puts "Time: #{ActiveRecordQueryCounter.query_time}"
34
+ end
35
+ ```
36
+
37
+ This gem includes middleware for both Rack and Sidekiq that will enable query counting.
38
+
39
+ If you are using Rails with Sidekiq, you can enable both with an initializer.
40
+
41
+ ```ruby
42
+ ActiveSupport.on_load(:active_record) do
43
+ ActiveRecordQueryCounter.enable!(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter)
44
+ end
45
+
46
+ Rails.application.config.middleware.use(ActiveRecordQueryCounter::RackMiddleware)
47
+
48
+ Sidekiq.configure_server do |config|
49
+ config.server_middleware do |chain|
50
+ chain.add ActiveRecordQueryCounter::SidekiqMiddleware
51
+ end
52
+ end
53
+ ```
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
@@ -0,0 +1,35 @@
1
+ Gem::Specification.new do |spec|
2
+ spec.name = 'active_record_query_counter'
3
+ spec.version = File.read(File.expand_path("../VERSION", __FILE__)).strip
4
+ spec.authors = ['Brian Durand']
5
+ spec.email = ['bbdurand@gmail.com']
6
+
7
+ spec.summary = "Count total number of ActiveRecord queries and row counts inside a block"
8
+ spec.homepage = "https://github.com/bdurand/lumberjack"
9
+ spec.license = "MIT"
10
+
11
+ # Specify which files should be added to the gem when it is released.
12
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
13
+ ignore_files = %w(
14
+ .gitignore
15
+ .travis.yml
16
+ Appraisals
17
+ Gemfile
18
+ Gemfile.lock
19
+ Rakefile
20
+ gemfiles/
21
+ spec/
22
+ )
23
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
24
+ `git ls-files -z`.split("\x0").reject{ |f| ignore_files.any?{ |path| f.start_with?(path) } }
25
+ end
26
+
27
+ spec.require_paths = ['lib']
28
+
29
+ spec.add_dependency "activerecord", ">= 4.2"
30
+
31
+ spec.add_development_dependency "sqlite3"
32
+ spec.add_development_dependency("rspec", ["~> 3.0"])
33
+ spec.add_development_dependency "rake"
34
+ spec.add_development_dependency "appraisal"
35
+ end
@@ -0,0 +1,112 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Everything you need to count ActiveRecord queries and row counts within a block.
4
+ #
5
+ # Usage:
6
+ #
7
+ # ActiveRecordQueryCounter.count_queries do
8
+ # yield
9
+ # puts ActiveRecordQueryCounter.query_count
10
+ # puts ActiveRecordQueryCounter.row_count
11
+ # end
12
+ module ActiveRecordQueryCounter
13
+
14
+ class << self
15
+
16
+ # Enable query counting within a block.
17
+ def count_queries
18
+ current = Thread.current[:database_query_counter]
19
+ begin
20
+ Thread.current[:database_query_counter] = [0, 0, 0.0]
21
+ yield
22
+ ensure
23
+ Thread.current[:database_query_counter] = current
24
+ end
25
+ end
26
+
27
+ # Increment the query counters
28
+ def increment(row_count, elapsed_time)
29
+ current = Thread.current[:database_query_counter]
30
+ if current.is_a?(Array)
31
+ current[0] = current[0].to_i + 1
32
+ current[1] = current[1].to_i + row_count
33
+ current[2] = current[2].to_f + elapsed_time
34
+ end
35
+ end
36
+
37
+ def query_count
38
+ current = Thread.current[:database_query_counter]
39
+ current[0].to_i if current.is_a?(Array)
40
+ end
41
+
42
+ def row_count
43
+ current = Thread.current[:database_query_counter]
44
+ current[1].to_i if current.is_a?(Array)
45
+ end
46
+
47
+ def query_time
48
+ current = Thread.current[:database_query_counter]
49
+ current[2].to_f if current.is_a?(Array)
50
+ end
51
+
52
+ # Return the query info as a hash with keys :query_count, :row_count, :query_time.
53
+ # or nil if not inside a block where queries are being counted.
54
+ def info
55
+ current = Thread.current[:database_query_counter]
56
+ if current
57
+ {
58
+ :query_count => current[0],
59
+ :row_count => current[1],
60
+ :query_time => current[2]
61
+ }
62
+ else
63
+ nil
64
+ end
65
+ end
66
+
67
+ # Enable the query counting behavior on a connection adapter class.
68
+ def enable!(connection_class)
69
+ unless connection_class.include?(ConnectionAdapterExtension)
70
+ connection_class.prepend(ConnectionAdapterExtension)
71
+ end
72
+ end
73
+
74
+ end
75
+
76
+ # Module to prepend to the connection adapter to inject the counting behavior.
77
+ module ConnectionAdapterExtension
78
+
79
+ def exec_query(*args)
80
+ start_time = Time.now
81
+ result = super
82
+ if result.is_a?(ActiveRecord::Result)
83
+ ActiveRecordQueryCounter.increment(result.length, Time.now - start_time)
84
+ end
85
+ result
86
+ end
87
+
88
+ end
89
+
90
+ # Rack middleware to count queries on a request.
91
+ class RackMiddleware
92
+
93
+ def initialize(app)
94
+ @app = app
95
+ end
96
+
97
+ def call(env)
98
+ ActiveRecordQueryCounter.count_queries { @app.call(env) }
99
+ end
100
+
101
+ end
102
+
103
+ # Sidekiq middleware to count queries on a job.
104
+ class SidekiqMiddleware
105
+
106
+ def call(worker, job, queue, &block)
107
+ ActiveRecordQueryCounter.count_queries(&block)
108
+ end
109
+
110
+ end
111
+
112
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_record_query_counter
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Brian Durand
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-01-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: appraisal
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description:
84
+ email:
85
+ - bbdurand@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - CHANGE_LOG.md
91
+ - MIT_LICENSE
92
+ - README.md
93
+ - VERSION
94
+ - active_record_query_counter.gemspec
95
+ - lib/active_record_query_counter.rb
96
+ homepage: https://github.com/bdurand/lumberjack
97
+ licenses:
98
+ - MIT
99
+ metadata: {}
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ requirements: []
115
+ rubygems_version: 3.0.3
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: Count total number of ActiveRecord queries and row counts inside a block
119
+ test_files: []