ActionCost

ActionCost is a Rails 3 engine implemented as a gem. It hooks into ActiveRecord (and RecordCache if used) and counts the number of SQL queries per controller action and per table.

Setup

Simply include action_cost into your Rails 3 application Gemfile.

gem 'action_cost', '0.0.1'

In some applications it’s also necessary to add routes in #Rails.root/config/routes.rb for the ActionCost dashboard pages:

namespace :action_cost do
  get '/' => 'dashboards#index'
  match '/ca/:ca' => 'dashboards#ca', :as => 'ca', :constraints => { :ca => /.*/ }
end

Where is the data stored?

In the Rails process memory. The data goes away when the process dies.

Where is the data visible?

  1. Go to yourapp/action_cost, it will display a page with a list of controller action and the average number of queries made in each one of them. Click on a controller action to find which tables were queried.

  2. After each controller action completes, an action_cost summary is #Rails.root/log/action_cost.log