I was wondering how I could do this as well. I ended up creating a custom 
logger that tracks the # of logs generated, and then attaching it to the 
connection.

```
require 'logger'

# A custom logger that tracks the count of logs generated
class CountLogger < Logger
  def initialize(...)
    @log_counter = Hash.new(0)
    start_counting
    super
  end

  # Returns the # of logs generated by log type: log_count(Logger::WARN)
  # If no severity is passed in, return the total # of logs generated
  def log_count(severity = nil)
    severity.nil? ? @log_counter.values.sum : @log_counter[severity]
  end

  def start_counting
    @counter_flag = true
  end

  def stop_counting
    @counter_flag = false
  end

  def clear_counts
    @log_counter = Hash.new(0)
  end

  def counting?
    @counter_flag ||= false
  end

  def add(severity, message = nil, progname = nil)
    @log_counter[severity] += 1 if counting?
    super
  end
end

@logger = CountLogger.new(nil)
DB.logger = @logger

# example of how you could make this work.
def count_queries
  @logger.clear_counts
  yield
  puts "# of DB queries executed: #{@logger.log_count}"
end
```

One thing I've been playing around with is hooking this into Roda as a 
plugin so that I can track the # of queries run per request. I can share 
the code for it if there's interest in it.

Hope this helps!
On Friday, August 12, 2022 at 12:35:46 PM UTC-7 [email protected] wrote:

> 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/5acc1f59-3185-4d10-a4cc-e05115602dc8n%40googlegroups.com.

Reply via email to