LoginSignup
4
5

More than 5 years have passed since last update.

railsでプロファイラ(rack-mini-profiler)を追加する方法

Posted at

パフォーマンスチューニングをするために、プロファイリングツールを導入する。

今回使うのは、rack-mini-profiler。

導入

1.Gemfileに gem 'rack-mini-profiler' を追加する。
2.bundle install
3.rails s

以上3ステップで導入完了。
ブラウザにアクセスすると右上に以下のように表示される。

キャプチャ1.png

クリックするとメニューが開く
キャプチャ2.png

query列のリンクをクリックすると発行されたsqlが確認できる。

本番環境で利用する方法

開発環境であれば上記だけで利用可能だが、本番で利用したい場合は以下のステップも踏む必要がある。

application_controller.rb
class ApplicationController < ActionController::Base
  protect_from_forgery
#以下を追加  
  before_filter :miniprofiler

  private
  def miniprofiler
    Rack::MiniProfiler.authorize_request
  end
#以上を追加
end

on/off切り替え

以下のtrue/falseでプロファイラ表示の切り替え可能

/config/environments/production.rb
# Disable Rails's static asset server (Apache or nginx will already do this)
config.serve_static_assets = true

以上です!!

4
5
0

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
4
5