thelowlypeon / too_active

Maybe the way you're using ActiveSupport in your application is... too active?

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Too Active

A tool for understanding if you're using Active Record too actively.

Profiling

TooActive is quite simple: you pass a block to TooActive.profile and then inspect the events that occurred as the block is executed.

TooActive.profile do
  MyModel.all.each do |model|
    puts model.relationships.map(&:id)
  end
end

TooActive.profile do
  MyModel.includes(:relationships).each do |model|
    puts model.relationships.map(&:id)
  end
end

It does this by subscribing to event using ActiveSupport::Notifications.

Currently, TooActive only supports sql.active_record events, which are fired when a SQL query is executed.

Installation

Install the gem in your Gemfile using bundler, or by building it locally.

# Gemfile

gem 'too_active', git: 'git@github.com:thelowlypeon/too_active'

If you add it to your Gemfile, you'll probably want to do so only in your test or development environments:

# Gemfile

group :development, :test do
  gem 'too_active', git: 'git@github.com:thelowlypeon/too_active'
end

Usage

Pass any block into profile:

events = TooActive.profile(analyze: false) { my_potentially_expensive_block }
puts events.count # => lots!

TooActive.profile { my_potentially_expensive_block } # => prints analysis

Note: This currently only analyzes active record queries and their duration. More to come.

About

Maybe the way you're using ActiveSupport in your application is... too active?


Languages

Language:Ruby 100.0%