Hey Mark,
I'm no Jeremy, but I recently did something similar to detect N+1 issues in
tests.
I wrote a module that I prepended to `Sequel::Postgres::Dataset` and kept a
`Thread.current[:queries]` in the block
This only counts selects, you can override the other methods if necessary.
module DatasetInstrumentation
def fetch_rows(*)
if Thread.current.key?(:queries)
Thread.current[:queries] += 1
end
super
end
end
Sequel::Postgres::Dataset.prepend(DatasetInstrumentation)
...
def count_queries
Thread.current[:queries] ||= 0
yield
count = Thread.current[:queries]
Thread.current[:queries] = nil
count
end
...
count_queries { Model.first }
Hope this helps!
On Friday, June 24, 2022 at 3:35:42 PM UTC-5 Jeremy Evans wrote:
> On Fri, Jun 24, 2022 at 1:25 PM Mark Allen <[email protected]> wrote:
>
>> Specifically, I'm trying to do this exact thing, but with Sequel instead
>> of ActiveRecord
>>
>>
>> https://github.com/rmosolgo/graphql-ruby/blob/master/guides/dataloader/testing.md#testing-dataloader-sources
>>
>> On Friday, June 24, 2022 at 4:22:29 PM UTC-4 Mark Allen wrote:
>>
>>> Hi, I want to count the number of DB operations within a block. Can't
>>> find any documentation how to do this. Found DB.extend_datasets which looks
>>> promising but can't find a way to UN-extend_dataset after. Thoughts?
>>>
>>
> The easiest way is probably using a Database logger, and seeing how many
> queries are logged during the block. You can remove the logger when the
> block exits.
>
> Thanks,
> Jeremy
>
--
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/sequel-talk/b82763a5-9fdb-4fe8-8f49-09b90675f50bn%40googlegroups.com.