n_one 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/workflows/ci.yml +16 -0
- data/.github/workflows/release.yml +34 -0
- data/.gitignore +9 -0
- data/.rubocop.yml +13 -0
- data/Gemfile +15 -0
- data/Gemfile.lock +80 -0
- data/LICENSE.txt +203 -0
- data/README.md +88 -0
- data/Rakefile +15 -0
- data/lib/n_one.rb +40 -0
- data/lib/n_one/query.rb +20 -0
- data/lib/n_one/runner.rb +77 -0
- data/lib/n_one/version.rb +5 -0
- data/n_one.gemspec +28 -0
- metadata +73 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b2b2ba1b5d986f35ed074dac942c0448f1ae64217403a5946398da54ad93e54f
|
4
|
+
data.tar.gz: 14cbea5e0fd6b84932f5d8b74957f8d99e97b0572eb062af824c9cf41745be0d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ea4606afb1b0ef8cc5417745f76b7260b628e77c645896e6ba919096340692cf6602a5d7645133fe5dbf79021d0664a738b6e870bbbea72728b6cb514bac638e
|
7
|
+
data.tar.gz: '08567625417d3881bc257bde3da51db8e500eb06a2ffdc2dd32d2800026d8d00df6a9ffb81cc6638d66bb96bde769f015c231a31a0444e7e8deb9b84c4efda04'
|
@@ -0,0 +1,16 @@
|
|
1
|
+
name: CI
|
2
|
+
on: [push, pull_request]
|
3
|
+
jobs:
|
4
|
+
test:
|
5
|
+
strategy:
|
6
|
+
matrix:
|
7
|
+
ruby: [2.5, '3.0', head]
|
8
|
+
runs-on: ubuntu-latest
|
9
|
+
steps:
|
10
|
+
- uses: actions/checkout@v2
|
11
|
+
- uses: ruby/setup-ruby@v1
|
12
|
+
with:
|
13
|
+
bundler-cache: true
|
14
|
+
ruby-version: ${{ matrix.ruby }}
|
15
|
+
- run: bundle exec rake test
|
16
|
+
- run: bundle exec rake rubocop
|
@@ -0,0 +1,34 @@
|
|
1
|
+
name: Release N-One to RubyGems
|
2
|
+
|
3
|
+
on:
|
4
|
+
push:
|
5
|
+
tags:
|
6
|
+
- 'v[0-9].[0-9]+.[0-9]+'
|
7
|
+
jobs:
|
8
|
+
build-and-release:
|
9
|
+
name: Release
|
10
|
+
runs-on: ubuntu-latest
|
11
|
+
permissions:
|
12
|
+
packages: write
|
13
|
+
contents: read
|
14
|
+
|
15
|
+
steps:
|
16
|
+
- name: Checkout
|
17
|
+
uses: actions/checkout@v2
|
18
|
+
|
19
|
+
- name: Set up Ruby 2.6
|
20
|
+
uses: ruby/setup-ruby@477b21f02be01bcb8030d50f37cfec92bfa615b6
|
21
|
+
with:
|
22
|
+
ruby-version: 2.6
|
23
|
+
- run: bundle install
|
24
|
+
|
25
|
+
- name: Publish to RubyGems
|
26
|
+
run: |
|
27
|
+
mkdir -p $HOME/.gem
|
28
|
+
touch $HOME/.gem/credentials
|
29
|
+
chmod 0600 $HOME/.gem/credentials
|
30
|
+
printf -- "---\n:rubygems_api_key: ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials
|
31
|
+
gem build *.gemspec
|
32
|
+
gem push *.gem
|
33
|
+
env:
|
34
|
+
GEM_HOST_API_KEY: "${{secrets.RUBYGEMS_API_KEY}}"
|
data/.gitignore
ADDED
data/.rubocop.yml
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source 'https://rubygems.org'
|
4
|
+
|
5
|
+
gemspec
|
6
|
+
|
7
|
+
gem 'activerecord'
|
8
|
+
gem 'factory_bot'
|
9
|
+
gem 'minitest'
|
10
|
+
gem 'pry'
|
11
|
+
gem 'rake', '~> 13.0'
|
12
|
+
gem 'rubocop'
|
13
|
+
gem 'rubocop-minitest'
|
14
|
+
gem 'rubocop-rake'
|
15
|
+
gem 'sqlite3'
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
n_one (1.0.0)
|
5
|
+
pg_query
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activemodel (6.1.3)
|
11
|
+
activesupport (= 6.1.3)
|
12
|
+
activerecord (6.1.3)
|
13
|
+
activemodel (= 6.1.3)
|
14
|
+
activesupport (= 6.1.3)
|
15
|
+
activesupport (6.1.3)
|
16
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
17
|
+
i18n (>= 1.6, < 2)
|
18
|
+
minitest (>= 5.1)
|
19
|
+
tzinfo (~> 2.0)
|
20
|
+
zeitwerk (~> 2.3)
|
21
|
+
ast (2.4.2)
|
22
|
+
coderay (1.1.3)
|
23
|
+
concurrent-ruby (1.1.8)
|
24
|
+
factory_bot (6.1.0)
|
25
|
+
activesupport (>= 5.0.0)
|
26
|
+
i18n (1.8.9)
|
27
|
+
concurrent-ruby (~> 1.0)
|
28
|
+
method_source (1.0.0)
|
29
|
+
minitest (5.14.3)
|
30
|
+
parallel (1.20.1)
|
31
|
+
parser (3.0.1.0)
|
32
|
+
ast (~> 2.4.1)
|
33
|
+
pg_query (1.3.0)
|
34
|
+
pry (0.14.0)
|
35
|
+
coderay (~> 1.1)
|
36
|
+
method_source (~> 1.0)
|
37
|
+
rainbow (3.0.0)
|
38
|
+
rake (13.0.3)
|
39
|
+
regexp_parser (2.1.1)
|
40
|
+
rexml (3.2.5)
|
41
|
+
rubocop (1.13.0)
|
42
|
+
parallel (~> 1.10)
|
43
|
+
parser (>= 3.0.0.0)
|
44
|
+
rainbow (>= 2.2.2, < 4.0)
|
45
|
+
regexp_parser (>= 1.8, < 3.0)
|
46
|
+
rexml
|
47
|
+
rubocop-ast (>= 1.2.0, < 2.0)
|
48
|
+
ruby-progressbar (~> 1.7)
|
49
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
50
|
+
rubocop-ast (1.4.1)
|
51
|
+
parser (>= 2.7.1.5)
|
52
|
+
rubocop-minitest (0.12.1)
|
53
|
+
rubocop (>= 0.90, < 2.0)
|
54
|
+
rubocop-rake (0.5.1)
|
55
|
+
rubocop
|
56
|
+
ruby-progressbar (1.11.0)
|
57
|
+
sqlite3 (1.4.2)
|
58
|
+
tzinfo (2.0.4)
|
59
|
+
concurrent-ruby (~> 1.0)
|
60
|
+
unicode-display_width (2.0.0)
|
61
|
+
zeitwerk (2.4.2)
|
62
|
+
|
63
|
+
PLATFORMS
|
64
|
+
ruby
|
65
|
+
x86_64-linux
|
66
|
+
|
67
|
+
DEPENDENCIES
|
68
|
+
activerecord
|
69
|
+
factory_bot
|
70
|
+
minitest
|
71
|
+
n_one!
|
72
|
+
pry
|
73
|
+
rake (~> 13.0)
|
74
|
+
rubocop
|
75
|
+
rubocop-minitest
|
76
|
+
rubocop-rake
|
77
|
+
sqlite3
|
78
|
+
|
79
|
+
BUNDLED WITH
|
80
|
+
2.2.10
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,203 @@
|
|
1
|
+
|
2
|
+
Apache License
|
3
|
+
Version 2.0, January 2004
|
4
|
+
http://www.apache.org/licenses/
|
5
|
+
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
7
|
+
|
8
|
+
1. Definitions.
|
9
|
+
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
12
|
+
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
14
|
+
the copyright owner that is granting the License.
|
15
|
+
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
17
|
+
other entities that control, are controlled by, or are under common
|
18
|
+
control with that entity. For the purposes of this definition,
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
20
|
+
direction or management of such entity, whether by contract or
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
23
|
+
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
25
|
+
exercising permissions granted by this License.
|
26
|
+
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
28
|
+
including but not limited to software source code, documentation
|
29
|
+
source, and configuration files.
|
30
|
+
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
32
|
+
transformation or translation of a Source form, including but
|
33
|
+
not limited to compiled object code, generated documentation,
|
34
|
+
and conversions to other media types.
|
35
|
+
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
37
|
+
Object form, made available under the License, as indicated by a
|
38
|
+
copyright notice that is included in or attached to the work
|
39
|
+
(an example is provided in the Appendix below).
|
40
|
+
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
47
|
+
the Work and Derivative Works thereof.
|
48
|
+
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
50
|
+
the original version of the Work and any modifications or additions
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
62
|
+
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
65
|
+
subsequently incorporated within the Work.
|
66
|
+
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
73
|
+
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
79
|
+
where such license applies only to those patent claims licensable
|
80
|
+
by such Contributor that are necessarily infringed by their
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
83
|
+
institute patent litigation against any entity (including a
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
86
|
+
or contributory patent infringement, then any patent licenses
|
87
|
+
granted to You under this License for that Work shall terminate
|
88
|
+
as of the date such litigation is filed.
|
89
|
+
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
92
|
+
modifications, and in Source or Object form, provided that You
|
93
|
+
meet the following conditions:
|
94
|
+
|
95
|
+
(a) You must give any other recipients of the Work or
|
96
|
+
Derivative Works a copy of this License; and
|
97
|
+
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
99
|
+
stating that You changed the files; and
|
100
|
+
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
103
|
+
attribution notices from the Source form of the Work,
|
104
|
+
excluding those notices that do not pertain to any part of
|
105
|
+
the Derivative Works; and
|
106
|
+
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
109
|
+
include a readable copy of the attribution notices contained
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
112
|
+
of the following places: within a NOTICE text file distributed
|
113
|
+
as part of the Derivative Works; within the Source form or
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
115
|
+
within a display generated by the Derivative Works, if and
|
116
|
+
wherever such third-party notices normally appear. The contents
|
117
|
+
of the NOTICE file are for informational purposes only and
|
118
|
+
do not modify the License. You may add Your own attribution
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
121
|
+
that such additional attribution notices cannot be construed
|
122
|
+
as modifying the License.
|
123
|
+
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
125
|
+
may provide additional or different license terms and conditions
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
129
|
+
the conditions stated in this License.
|
130
|
+
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
134
|
+
this License, without any additional terms or conditions.
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
136
|
+
the terms of any separate license agreement you may have executed
|
137
|
+
with Licensor regarding such Contributions.
|
138
|
+
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
141
|
+
except as required for reasonable and customary use in describing the
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
143
|
+
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
153
|
+
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
159
|
+
incidental, or consequential damages of any character arising as a
|
160
|
+
result of this License or out of the use or inability to use the
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
163
|
+
other commercial damages or losses), even if such Contributor
|
164
|
+
has been advised of the possibility of such damages.
|
165
|
+
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
169
|
+
or other liability obligations and/or rights consistent with this
|
170
|
+
License. However, in accepting such obligations, You may act only
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
175
|
+
of your accepting any such warranty or additional liability.
|
176
|
+
|
177
|
+
END OF TERMS AND CONDITIONS
|
178
|
+
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
180
|
+
|
181
|
+
To apply the Apache License to your work, attach the following
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
183
|
+
replaced with your own identifying information. (Don't include
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
185
|
+
comment syntax for the file format. We also recommend that a
|
186
|
+
file or class name and description of purpose be included on the
|
187
|
+
same "printed page" as the copyright notice for easier
|
188
|
+
identification within third-party archives.
|
189
|
+
|
190
|
+
Copyright 2021 Mpampis Kostas
|
191
|
+
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
193
|
+
you may not use this file except in compliance with the License.
|
194
|
+
You may obtain a copy of the License at
|
195
|
+
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
197
|
+
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
201
|
+
See the License for the specific language governing permissions and
|
202
|
+
limitations under the License.
|
203
|
+
|
data/README.md
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
# NOne ![CI](https://github.com/prikha/n_one/actions/workflows/ci.yml/badge.svg)
|
2
|
+
|
3
|
+
NOne is able to auto-detect N+1 queries with confidence
|
4
|
+
|
5
|
+
**Note!** It is a rewrite of similar library https://github.com/charkost/prosopite. All credits go there.
|
6
|
+
|
7
|
+
## How it works
|
8
|
+
|
9
|
+
NOne monitors all SQL queries using the Active Support instrumentation
|
10
|
+
and looks for the following pattern which is present in all N+1 query cases:
|
11
|
+
|
12
|
+
More than one queries have the same call stack and the same query fingerprint.
|
13
|
+
|
14
|
+
## Installation
|
15
|
+
|
16
|
+
Add this line to your application's Gemfile:
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
gem 'pg_query'
|
20
|
+
gem 'n_one'
|
21
|
+
```
|
22
|
+
|
23
|
+
And then execute:
|
24
|
+
|
25
|
+
$ bundle install
|
26
|
+
|
27
|
+
Or install it yourself as:
|
28
|
+
|
29
|
+
$ gem install n_one
|
30
|
+
|
31
|
+
## Development Environment Usage
|
32
|
+
|
33
|
+
NOne auto-detection can be enabled on all controllers:
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
class ApplicationController < ActionController::Base
|
37
|
+
unless Rails.env.production?
|
38
|
+
around_action do
|
39
|
+
NOne.scan! do
|
40
|
+
yield
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
```
|
46
|
+
|
47
|
+
## Test Environment Usage
|
48
|
+
And each test can be scanned with:
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
# spec/spec_helper.rb
|
52
|
+
config.around(:each) do |example|
|
53
|
+
NOne.scan! do
|
54
|
+
example.run
|
55
|
+
end
|
56
|
+
end
|
57
|
+
```
|
58
|
+
|
59
|
+
or with custom code using scan report
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
# spec/your_spec.rb
|
63
|
+
it 'has no N+1 queries' do
|
64
|
+
n_ones = NOne.scan do
|
65
|
+
MyAction.perform(arguments)
|
66
|
+
end
|
67
|
+
|
68
|
+
expect(n_ones.size).to eq(0)
|
69
|
+
end
|
70
|
+
```
|
71
|
+
|
72
|
+
## Whitelisting
|
73
|
+
|
74
|
+
Ignore notifications for call stacks containing one or more substrings:
|
75
|
+
|
76
|
+
```ruby
|
77
|
+
NOne.scan!(whitelist: 'myapp/lib/known_n_plus_ones/') do
|
78
|
+
example.run
|
79
|
+
end
|
80
|
+
```
|
81
|
+
|
82
|
+
## Contributing
|
83
|
+
|
84
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/prikha/n_one.
|
85
|
+
|
86
|
+
## License
|
87
|
+
|
88
|
+
NOne is licensed under the Apache License, Version 2.0. See LICENSE.txt for the full license text.
|
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rake/testtask'
|
4
|
+
|
5
|
+
Rake::TestTask.new(:test) do |t|
|
6
|
+
t.libs << 'test'
|
7
|
+
t.libs << 'lib'
|
8
|
+
t.test_files = FileList['test/**/test_*.rb']
|
9
|
+
end
|
10
|
+
|
11
|
+
require 'rubocop/rake_task'
|
12
|
+
|
13
|
+
RuboCop::RakeTask.new(:rubocop)
|
14
|
+
|
15
|
+
task default: %i[test rubocop]
|
data/lib/n_one.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'n_one/version'
|
4
|
+
require_relative 'n_one/query'
|
5
|
+
require_relative 'n_one/runner'
|
6
|
+
|
7
|
+
# Reliable N+1 detection based on sql fingerprinting
|
8
|
+
module NOne
|
9
|
+
class NPlusOneDetected < StandardError # :nodoc:
|
10
|
+
def initialize(report)
|
11
|
+
@report = report
|
12
|
+
super
|
13
|
+
end
|
14
|
+
|
15
|
+
def message
|
16
|
+
"N+1 queries detected(count: #{@report.size}) \n" +
|
17
|
+
@report.map do |detected_case|
|
18
|
+
<<~MESSAGE
|
19
|
+
SQL query called #{detected_case[:count]} times
|
20
|
+
---
|
21
|
+
#{detected_case[:sql].join("\n")}
|
22
|
+
|
23
|
+
Backtrace:
|
24
|
+
---
|
25
|
+
#{detected_case[:caller].join("\n")}
|
26
|
+
MESSAGE
|
27
|
+
end.join("\n\n")
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
module_function
|
32
|
+
|
33
|
+
def scan(whitelist: [], &block)
|
34
|
+
Runner.new(whitelist: whitelist).scan(&block)
|
35
|
+
end
|
36
|
+
|
37
|
+
def scan!(whitelist: [], &block)
|
38
|
+
Runner.new(whitelist: whitelist).scan!(&block)
|
39
|
+
end
|
40
|
+
end
|
data/lib/n_one/query.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'pg_query'
|
4
|
+
|
5
|
+
module NOne
|
6
|
+
# Abstract SQL fingerprinting
|
7
|
+
module Query
|
8
|
+
module_function
|
9
|
+
|
10
|
+
def fingerprint(query)
|
11
|
+
raise 'MySQL is not supported' if ActiveRecord::Base.connection.adapter_name.downcase.include?('mysql')
|
12
|
+
|
13
|
+
begin
|
14
|
+
PgQuery.fingerprint(query)
|
15
|
+
rescue PgQuery::ParseError
|
16
|
+
nil
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/n_one/runner.rb
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module NOne
|
4
|
+
class Runner # :nodoc:
|
5
|
+
def initialize(whitelist: [])
|
6
|
+
@whitelist = ['active_record/validations/uniqueness'] + whitelist
|
7
|
+
end
|
8
|
+
|
9
|
+
def scan(&block)
|
10
|
+
init_store
|
11
|
+
record_sql(&block)
|
12
|
+
detect_n_plus_ones
|
13
|
+
end
|
14
|
+
|
15
|
+
def scan!(&block)
|
16
|
+
report = scan(&block)
|
17
|
+
|
18
|
+
raise NPlusOneDetected, report unless report.empty?
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
attr_reader :store, :whitelist
|
24
|
+
|
25
|
+
def init_store
|
26
|
+
@store = {}
|
27
|
+
end
|
28
|
+
|
29
|
+
def detect_n_plus_ones # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
|
30
|
+
store.values.map do |statement|
|
31
|
+
next if statement[:count] <= 1
|
32
|
+
next if statement[:caller].any? do |backtrace_line|
|
33
|
+
whitelist.any? do |whitelisted|
|
34
|
+
backtrace_line.include?(whitelisted)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
compact_caller = statement[:caller].reject { |line| line.include?(::Bundler.bundle_path.to_s) }
|
39
|
+
|
40
|
+
{
|
41
|
+
sql: statement[:sql],
|
42
|
+
count: statement[:count],
|
43
|
+
caller: compact_caller
|
44
|
+
}
|
45
|
+
end.compact
|
46
|
+
end
|
47
|
+
|
48
|
+
def record_sql(&block) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
|
49
|
+
subscriber = ActiveSupport::Notifications.subscribe 'sql.active_record' do |_, _, _, _, data|
|
50
|
+
sql = data[:sql]
|
51
|
+
cached = data[:cached]
|
52
|
+
|
53
|
+
next if !sql.include?('SELECT') || cached
|
54
|
+
|
55
|
+
sql_fingerprint = Query.fingerprint(sql)
|
56
|
+
next unless sql_fingerprint
|
57
|
+
|
58
|
+
location_key = Digest::SHA1.hexdigest(caller.join)
|
59
|
+
|
60
|
+
store["#{sql_fingerprint}_#{location_key}"] ||= {
|
61
|
+
count: 0,
|
62
|
+
sql: [],
|
63
|
+
caller: nil
|
64
|
+
}
|
65
|
+
|
66
|
+
store["#{sql_fingerprint}_#{location_key}"][:count] += 1
|
67
|
+
store["#{sql_fingerprint}_#{location_key}"][:sql] << sql
|
68
|
+
store["#{sql_fingerprint}_#{location_key}"][:sql].uniq!
|
69
|
+
store["#{sql_fingerprint}_#{location_key}"][:caller] ||= caller.dup
|
70
|
+
end
|
71
|
+
|
72
|
+
block.call
|
73
|
+
ensure
|
74
|
+
ActiveSupport::Notifications.unsubscribe(subscriber)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
data/n_one.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'lib/n_one/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'n_one'
|
7
|
+
spec.version = NOne::VERSION
|
8
|
+
spec.authors = ['Sergey Prikhodko']
|
9
|
+
spec.email = ['prikha@gmail.com']
|
10
|
+
|
11
|
+
spec.summary = 'N+1 auto-detection for ActiveRecord and PostgreSQL'
|
12
|
+
spec.description = 'N+1 auto-detection for ActiveRecord and PostgreSQL'
|
13
|
+
spec.homepage = 'https://github.com/prikha/n_one'
|
14
|
+
spec.license = 'Apache-2.0'
|
15
|
+
spec.required_ruby_version = Gem::Requirement.new('>= 2.5.0')
|
16
|
+
|
17
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
18
|
+
spec.metadata['source_code_uri'] = 'https://github.com/prikha/n_one'
|
19
|
+
|
20
|
+
# Specify which files should be added to the gem when it is released.
|
21
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
22
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
23
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
|
24
|
+
end
|
25
|
+
spec.require_paths = ['lib']
|
26
|
+
|
27
|
+
spec.add_runtime_dependency 'pg_query'
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: n_one
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sergey Prikhodko
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-08-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: pg_query
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: N+1 auto-detection for ActiveRecord and PostgreSQL
|
28
|
+
email:
|
29
|
+
- prikha@gmail.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- ".github/workflows/ci.yml"
|
35
|
+
- ".github/workflows/release.yml"
|
36
|
+
- ".gitignore"
|
37
|
+
- ".rubocop.yml"
|
38
|
+
- Gemfile
|
39
|
+
- Gemfile.lock
|
40
|
+
- LICENSE.txt
|
41
|
+
- README.md
|
42
|
+
- Rakefile
|
43
|
+
- lib/n_one.rb
|
44
|
+
- lib/n_one/query.rb
|
45
|
+
- lib/n_one/runner.rb
|
46
|
+
- lib/n_one/version.rb
|
47
|
+
- n_one.gemspec
|
48
|
+
homepage: https://github.com/prikha/n_one
|
49
|
+
licenses:
|
50
|
+
- Apache-2.0
|
51
|
+
metadata:
|
52
|
+
homepage_uri: https://github.com/prikha/n_one
|
53
|
+
source_code_uri: https://github.com/prikha/n_one
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options: []
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: 2.5.0
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
requirements: []
|
69
|
+
rubygems_version: 3.1.2
|
70
|
+
signing_key:
|
71
|
+
specification_version: 4
|
72
|
+
summary: N+1 auto-detection for ActiveRecord and PostgreSQL
|
73
|
+
test_files: []
|