LoginSignup
159
161

More than 5 years have passed since last update.

Railsアプリのパフォーマンスチューニング用ツール紹介

Last updated at Posted at 2012-12-26

Railsアプリのパフォーマンスチューニング用ツール紹介
forkwell.com Y U SO SLOW // Speaker Deck松田さんが紹介していたツールを使ってRailsアプリのボトルネックを調べたので,そのツールの説明.

MiniProfiler/Ruby

Gemfile
# developmentでもproductionでも入るようにしておく
gem 'rack-mini-profiler'

設定は以下

application_controller.rb
class ApplicationController < ActionController::Base
  before_filter :profile_for_admins
  # ...
  def authorize
    if Rails.env.development? || current_user.try(:staff?)
      Rack::MiniProfiler.authorize_request
    end
  end
end 

左上に各リクエスト(XHRなど)でかかった時間が表示される.発行されたSQLごとの処理時間も見られる.

参考リンク: #368 MiniProfiler - RailsCasts

SQLをざっと見てみて,明らかに無駄なSQLを排除していく.
かかった時間ごとにSQLをソートしてくれたらいいのに…

flyerhzm/bullet

N+1クエリ問題や不要なeager loading, counter cacheを使うべきポイントを検出してくれるツール.
flyerhzm/bullet configurationにある通りに設定すると以下のようなlogを出してくれる.stacktraceも見られるので問題箇所がわかりやすい.

log/bullet.log
N+1 Query detected
  Item => [:user]
  Add to your finder: :include => [:user]
N+1 Query method call stack
  N+1 Query method call stack
  N+1 Query method call stack
  N+1 Query method call stack
  /foo/bar/baz.rb:123:in `some_method'
  /foo/bar/baz.rb:123:in `other_method'

適切にFoo.all->Foo.includes(:bar).allなどとしてN+1クエリを潰せばよい.

SQL auto explain(v3.2)

http://guides.rubyonrails.org/v3.2.13/active_record_querying.html#automatic-explain のように設定すると,実行に時間がかかったSQLをログ出力してくれる.
しかしRails 4.0では削除済.(https://github.com/rails/rails/commit/d3688e02ca52c0b72d3092e8498da51e06b7fc58)

159
161
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
159
161