Skip to content

Commit 511a745

Browse files
committed
Add min_n_queries & ignore_pauses config options
1 parent 39bcf82 commit 511a745

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ Or install it yourself as:
115115

116116
The preferred type of notifications can be configured with:
117117

118+
* `Prosopite.min_n_queries`: Minimum number of N queries to report per N+1 case. Defaults to 2.
118119
* `Prosopite.raise = true`: Raise warnings as exceptions
119120
* `Prosopite.rails_logger = true`: Send warnings to the Rails log
120121
* `Prosopite.prosopite_logger = true`: Send warnings to `log/prosopite.log`
@@ -269,6 +270,8 @@ end
269270
Prosopite.finish
270271
```
271272

273+
Pauses can be ignored with `Prosopite.ignore_pauses = true` in case you want to remember their N+1 queries.
274+
272275
An example of when you might use this is if you are [testing Active Jobs inline](https://guides.rubyonrails.org/testing.html#testing-jobs),
273276
and don't want to run Prosopite on background job code, just foreground app code. In that case you could write an [Active Job callback](https://edgeguides.rubyonrails.org/active_job_basics.html#callbacks) that pauses the scan while the job is running.
274277

lib/prosopite.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ class << self
1010
:prosopite_logger,
1111
:custom_logger,
1212
:allow_stack_paths,
13-
:ignore_queries
13+
:ignore_queries,
14+
:ignore_pauses,
15+
:min_n_queries
1416

1517
def allow_list=(value)
1618
puts "Prosopite.allow_list= is deprecated. Use Prosopite.allow_stack_paths= instead."
@@ -29,6 +31,8 @@ def scan
2931
tc[:prosopite_query_caller] = {}
3032

3133
@allow_stack_paths ||= []
34+
@ignore_pauses ||= false
35+
@min_n_queries ||= 2
3236

3337
tc[:prosopite_scan] = true
3438

@@ -48,6 +52,10 @@ def tc
4852
end
4953

5054
def pause
55+
if @ignore_pauses
56+
return block_given? ? yield : nil
57+
end
58+
5159
if block_given?
5260
begin
5361
previous = tc[:prosopite_scan]
@@ -83,7 +91,7 @@ def create_notifications
8391
tc[:prosopite_notifications] = {}
8492

8593
tc[:prosopite_query_counter].each do |location_key, count|
86-
if count > 1
94+
if count >= @min_n_queries
8795
fingerprints = tc[:prosopite_query_holder][location_key].map do |q|
8896
begin
8997
fingerprint(q)

test/test_queries.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,25 @@ def test_pause_with_no_error_after_resume
124124
assert_no_n_plus_ones
125125
end
126126

127+
def test_pause_with_ignore_pauses
128+
# 20 chairs, 4 legs each
129+
chairs = create_list(:chair, 20)
130+
chairs.each { |c| create_list(:leg, 4, chair: c) }
131+
132+
Prosopite.ignore_pauses = true
133+
Prosopite.scan
134+
135+
Prosopite.pause
136+
Chair.last(20).each do |c|
137+
c.legs.last
138+
end
139+
140+
Prosopite.resume
141+
Prosopite.ignore_pauses = false
142+
143+
assert_n_plus_one
144+
end
145+
127146
def test_pause_with_error_after_resume
128147
# 20 chairs, 4 legs each
129148
chairs = create_list(:chair, 20)
@@ -348,6 +367,20 @@ def test_ignore_queries_with_incorrect_query_match
348367
assert_n_plus_one
349368
end
350369

370+
def test_min_n_queries
371+
chairs = create_list(:chair, 4)
372+
chairs.each { |c| create_list(:leg, 4, chair: c) }
373+
374+
Prosopite.min_n_queries = 5
375+
376+
Prosopite.scan
377+
Chair.last(4).each do |c|
378+
c.legs.last
379+
end
380+
381+
assert_no_n_plus_ones
382+
end
383+
351384
private
352385
def assert_n_plus_one
353386
assert_raises(Prosopite::NPlusOneQueriesError) do

0 commit comments

Comments
 (0)