cookpad-performance 0.2.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 16ed8a79cc750fdc941bd559af36feda7bff7b54a6deaf36729fc277cf829a37
4
+ data.tar.gz: 4c004dc7a7965b55802b4011d66477aed68a9a0d5528e227a43684b9170f18f3
5
+ SHA512:
6
+ metadata.gz: f73325fca388869a266b077f27c7c111309346da0d7e09221e64e7385ad7290b31f9c55fd0d619a0a5c067868afa76c40e07fb9d8b14f1bb3d88cf91fa8c5cc6
7
+ data.tar.gz: 8292570b2d24a37cd489a0f06b718436ac61dbb0d811ca0e4686f3182c93db84ff68848cac6e2934cdda324e40f0a37875a1652e3cfcfac659230b6dc0f72411
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2022 Gavin Morrice
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.
data/README.md ADDED
@@ -0,0 +1,65 @@
1
+ # Cookpad::Performance
2
+
3
+ Adds a selection of tools to make performance troubleshooting and profiling easier in development.
4
+
5
+ ## Philosophy
6
+
7
+ This gem should provide a set of plug-and-play performance monitoring and troubleshooting tools for use in development and test environments.
8
+
9
+ Each tool should sit behind a feature toggle, and should be explicitly enabled through the use of environment variables.
10
+
11
+ The gem should not make any changes to the functioning and performance of the application unless a specific feature has explicitly been enabled. This helps us to provide helpful tools that will stay out of your way unless your require them.
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ ```ruby
18
+ group :development, :test do
19
+ gem "cookpad-performance"
20
+ end
21
+ ```
22
+
23
+ And then execute:
24
+ ```bash
25
+ $ bundle
26
+ ```
27
+
28
+ ## Included tools
29
+
30
+ ### ActiveRecord query counter
31
+
32
+ Log the total number of unique queries, cached queries, and async queries at the foot of each request in the Rails application log.
33
+
34
+ Activate by setting an environment variable called `LOG_DB_QUERY_COUNT=true`.
35
+
36
+ See [the initializer file](config/initializers/active_record_query_counter.rb) for more information.
37
+
38
+ ### Disable cached query logging
39
+
40
+ Silence cached ActiveRecord database queries from the Rails application log. Makes it easier to see which unique queries are being executed in the database.
41
+
42
+ Activate by setting an environment variable called `DISABLE_CACHED_QUERY_LOGGING=true`.
43
+
44
+ _Note: This should be used with discretion, to declutter very noisy logs. Cached queries are also expensive and should not be ignored_
45
+
46
+ See [the initializer file](config/initializers/disable_cached_query_logging.rb) for more information.
47
+
48
+ ### n+1 detection in Rails logs
49
+
50
+ Receive warnings in the Rails application logs when an n+1 query is detected.
51
+
52
+ Activate by setting an environment variable called `LOG_N_PLUS_ONE_QUERIES=true`, or `RAISE_N_PLUS_ONE_QUERIES=true` to raise exceptions when an instance is detected.
53
+
54
+ See [the initializer file](config/initializers/n_plus_one_detection.rb) for more information.
55
+
56
+ ### Profile mode
57
+
58
+ Run your Rails application in a production-like configuration of the development environment.
59
+
60
+ Activate by setting an environment variable called `PROFILE=true`.
61
+
62
+ See [the initializer file](config/initializers/profile_mode.rb) for more information.
63
+
64
+ ## License
65
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ require "bundler/setup"
2
+
3
+ begin
4
+ require "rspec/core/rake_task"
5
+ RSpec::Core::RakeTask.new(:spec)
6
+ rescue LoadError
7
+ end
8
+
9
+ task default: :spec
10
+
11
+ load "rails/tasks/statistics.rake"
12
+
13
+ require "bundler/gem_tasks"
@@ -0,0 +1,15 @@
1
+ if ENV["LOG_DB_QUERY_COUNT"] == "true"
2
+ require "active_record/query_counter"
3
+ ActiveSupport::Notifications.subscribe(/sql/, ActiveRecord::QueryCounter.current)
4
+ ActionController::Base.class_eval do
5
+ around_action :report_active_record_queries_count
6
+
7
+ private
8
+
9
+ def report_active_record_queries_count
10
+ ActiveRecord::QueryCounter.current.reset_counter
11
+ yield
12
+ logger.info(ActiveRecord::QueryCounter.current.report_counts)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,4 @@
1
+ if !Rails.env.production? && ENV["DISABLE_CACHED_QUERY_LOGGING"] == "true"
2
+ require "cookpad/performance/disable_cached_query_logging"
3
+ ActiveSupport::Logger.prepend Cookpad::Performance::DisableCachedQueryLogging
4
+ end
@@ -0,0 +1,8 @@
1
+ if (ENV["LOG_N_PLUS_ONE_QUERIES"] == "true" || ENV["RAISE_N_PLUS_ONE_QUERIES"] == "true") &&
2
+ (Rails.env.development? || Rails.env.test?)
3
+ require "prosopite"
4
+ Prosopite.rails_logger = ENV["LOG_N_PLUS_ONE_QUERIES"] == "true"
5
+ Prosopite.prosopite_logger = true
6
+ Prosopite.stderr_logger = false
7
+ Prosopite.raise = ENV["RAISE_N_PLUS_ONE_QUERIES"] == "true"
8
+ end
@@ -0,0 +1,34 @@
1
+ if ENV["PROFILE"] == "true" && Rails.env.development?
2
+ Rails.application.configure do
3
+ config.cache_classes = true
4
+ config.eager_load = true
5
+
6
+ logger = ActiveSupport::Logger.new($stdout)
7
+ logger.formatter = config.log_formatter
8
+ config.logger = ActiveSupport::TaggedLogging.new(logger)
9
+ config.log_level = :info
10
+
11
+ config.active_record.verbose_query_logs = false
12
+ config.active_record.migration_error = false
13
+
14
+ config.public_file_server.enabled = true
15
+ config.public_file_server.headers = {
16
+ "Cache-Control" => "max-age=315360000, public",
17
+ "Expires" => "Tue, 31 Dec 2030 23:55:55 GMT"
18
+ }
19
+ config.assets.js_compressor = Uglifier.new(harmony: true)
20
+ config.assets.compile = false
21
+ config.assets.digest = true
22
+ config.assets.debug = false
23
+
24
+ config.action_controller.perform_caching = true
25
+ config.action_controller.enable_fragment_cache_logging = true
26
+
27
+ config.action_mailer.perform_caching = true
28
+
29
+ config.action_view.cache_template_loading = true
30
+ config.action_view.annotate_rendered_view_with_filenames = false
31
+
32
+ config.log_tags = [:request_id]
33
+ end
34
+ end
@@ -0,0 +1,66 @@
1
+ module ActiveRecord
2
+ require "active_support/subscriber"
3
+ class QueryCounter < ActiveSupport::Subscriber
4
+ PAYLOAD_QUERY_TYPE_KEYS = %i(cached async).freeze
5
+ private_constant :PAYLOAD_QUERY_TYPE_KEYS
6
+
7
+ LOG_TEMPLATE = <<~STRING.chomp.freeze
8
+ DB query summary: \
9
+ Unique: %{unique} | \
10
+ Async: %{async} | \
11
+ Cached: %{cached} | \
12
+ Total: %{total}
13
+ STRING
14
+ private_constant :LOG_TEMPLATE
15
+
16
+ INGORED_QUERY_PREFIXES = [
17
+ "BEGIN",
18
+ "COMMIT",
19
+ "CREATE TABLE",
20
+ "DROP TABLE",
21
+ "PRAGMA",
22
+ "SELECT column_name",
23
+ "SELECT sqlite_version(*)",
24
+ "SELECT name FROM sqlite_master",
25
+ "SELECT sql FROM",
26
+ "SELECT table_name",
27
+ "SELECT `schema_migrations`.`version`",
28
+ "SET @@SESSION.sql_mode",
29
+ "SET NAME",
30
+ "SHOW FULL FIELDS"
31
+ ].freeze
32
+ private_constant :INGORED_QUERY_PREFIXES
33
+
34
+ def self.current
35
+ @_current ||= new
36
+ end
37
+
38
+ def sql(event)
39
+ return if INGORED_QUERY_PREFIXES.detect { |s| event.payload[:sql].start_with?(s) }
40
+
41
+ increment_counter(type: type_of_event(event))
42
+ end
43
+
44
+ def reset_counter
45
+ counter.transform_values! { 0 }
46
+ end
47
+
48
+ def report_counts
49
+ LOG_TEMPLATE % counter.merge({ total: counter.values.sum })
50
+ end
51
+
52
+ private
53
+
54
+ def counter
55
+ @_counter ||= Hash.new { |hash, key| hash[key] = 0 }
56
+ end
57
+
58
+ def increment_counter(type:)
59
+ counter[type] += 1
60
+ end
61
+
62
+ def type_of_event(event)
63
+ PAYLOAD_QUERY_TYPE_KEYS.detect { |key| event.payload[key] } || :unique
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,13 @@
1
+ module Cookpad
2
+ module Performance
3
+ module DisableCachedQueryLogging
4
+ CACHE_REGEXP = /\[36mCACHE\s[\w:]+\s(Load|Pluck)+/.freeze
5
+
6
+ def add(severity, message = nil, progname = nil, &block)
7
+ super if progname.nil? || !progname&.match?(CACHE_REGEXP)
8
+
9
+ # :noop:
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,15 @@
1
+ module Cookpad
2
+ module Performance
3
+ class Engine < ::Rails::Engine
4
+ isolate_namespace Cookpad::Performance
5
+
6
+ config.to_prepare do
7
+ if !Rails.env.production? &&
8
+ (ENV["LOG_N_PLUS_ONE_QUERIES"] == "true" ||
9
+ ENV["RAISE_N_PLUS_ONE_QUERIES"] == "true")
10
+ ActionController::Base.include(NPlusOneDetection)
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,5 @@
1
+ module Cookpad
2
+ module Performance
3
+ VERSION = "0.2.1".freeze
4
+ end
5
+ end
@@ -0,0 +1,8 @@
1
+ require "rails"
2
+ require "cookpad/performance/version"
3
+ require "cookpad/performance/engine"
4
+
5
+ module Cookpad
6
+ module Performance
7
+ end
8
+ end
@@ -0,0 +1 @@
1
+ require "cookpad/performance"
metadata ADDED
@@ -0,0 +1,256 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cookpad-performance
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.1
5
+ platform: ruby
6
+ authors:
7
+ - Bodacious
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-04-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: prosopite
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rails
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '7.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '7.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: byebug
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '11.1'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '11.1'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pronto
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.11'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.11'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pronto-rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.11'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.11'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.10'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.10'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec-rails
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubocop
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '1.26'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '1.26'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rubocop-performance
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '1.13'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '1.13'
139
+ - !ruby/object:Gem::Dependency
140
+ name: rubocop-rails
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '2.13'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '2.13'
153
+ - !ruby/object:Gem::Dependency
154
+ name: rubocop-rspec
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '2.9'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: '2.9'
167
+ - !ruby/object:Gem::Dependency
168
+ name: sprockets-rails
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ - !ruby/object:Gem::Dependency
182
+ name: sqlite3
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - ">="
186
+ - !ruby/object:Gem::Version
187
+ version: '0'
188
+ type: :development
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - ">="
193
+ - !ruby/object:Gem::Version
194
+ version: '0'
195
+ - !ruby/object:Gem::Dependency
196
+ name: uglifier
197
+ requirement: !ruby/object:Gem::Requirement
198
+ requirements:
199
+ - - "~>"
200
+ - !ruby/object:Gem::Version
201
+ version: '4.2'
202
+ type: :development
203
+ prerelease: false
204
+ version_requirements: !ruby/object:Gem::Requirement
205
+ requirements:
206
+ - - "~>"
207
+ - !ruby/object:Gem::Version
208
+ version: '4.2'
209
+ description: Provides a set of performance tools for our Rails apps
210
+ email:
211
+ - gavin@gavinmorrice.com
212
+ executables: []
213
+ extensions: []
214
+ extra_rdoc_files: []
215
+ files:
216
+ - MIT-LICENSE
217
+ - README.md
218
+ - Rakefile
219
+ - config/initializers/active_record_query_counter.rb
220
+ - config/initializers/disable_cached_query_logging.rb
221
+ - config/initializers/n_plus_one_detection.rb
222
+ - config/initializers/profile_mode.rb
223
+ - lib/active_record/query_counter.rb
224
+ - lib/cookpad-performance.rb
225
+ - lib/cookpad/performance.rb
226
+ - lib/cookpad/performance/disable_cached_query_logging.rb
227
+ - lib/cookpad/performance/engine.rb
228
+ - lib/cookpad/performance/version.rb
229
+ homepage: https://github.com/cookpad/cookpad-performance
230
+ licenses:
231
+ - MIT
232
+ metadata:
233
+ homepage_uri: https://github.com/cookpad/cookpad-performance
234
+ source_code_uri: https://github.com/cookpad/cookpad-performance
235
+ changelog_uri: https://github.com/cookpad/cookpad-performance/blob/main/CHANGELOG
236
+ rubygems_mfa_required: 'true'
237
+ post_install_message:
238
+ rdoc_options: []
239
+ require_paths:
240
+ - lib
241
+ required_ruby_version: !ruby/object:Gem::Requirement
242
+ requirements:
243
+ - - ">="
244
+ - !ruby/object:Gem::Version
245
+ version: 2.7.0
246
+ required_rubygems_version: !ruby/object:Gem::Requirement
247
+ requirements:
248
+ - - ">="
249
+ - !ruby/object:Gem::Version
250
+ version: '0'
251
+ requirements: []
252
+ rubygems_version: 3.3.8
253
+ signing_key:
254
+ specification_version: 4
255
+ summary: Rails app performance tools
256
+ test_files: []