Rails専用のパフォーマンスツール
- クエリ、viewレンダリングにかかった時間をログに計測して記録
- クエリプロファイリング:1リクエストごとにクエリを昇順または降順に整理してログに出力、またsqlのクエリの実行箇所を記録
- Viewプロファイリング:1リクエストごとにviewレンダリングにかかったページをログに計測して記録
- 対応version:Rails4.0, 4.1, 4.2, 5.0
- gemとしてインストール
$ gem install rails_log_profiling
- gemfileに追加
gem 'rails_log_profiling', :group => "development"
Rails::Log::Profilingは初期の段階ではなにも行いません
動作させるにはconfig/environments/development.rbに下記の設定を追加してください
# クエリのプロファイリングを有効にします
Rails::Log::Profiling.query_profiling_enable = true
# viewのプロファイリングを有効にします
Rails::Log::Profiling.view_profiling_enable = true
設定後、Rails Serverを起動 log/以下に
- rails_log_query_profiling.log
- rails_log_view_profiling.log
2つのログファイルが作られ、記録されます
$ tail -f log/rails_log_query_profiling.log
# output
PostsController#index
total query count: 2, total query time: 26.6ms
1: Articles Load (21.7ms) SELECT `articles`.* FROM `articles` ORDER BY `articles`.`id` ASC LIMIT 1
Identify Query Location:
/Users/fukumone/private_repo/rails_test/app/controllers/posts_controller.rb:9:in `index'
2: Post Load (4.9ms) SELECT `posts`.* FROM `posts`
Identify Query Location:
/Users/fukumone/private_repo/rails_test/app/views/posts/index.html.erb:16:in `_app_views_posts_index_html_erb__3476269730194822991_70352062040540'
$ tail -f log/rails_log_view_profiling.log
# output
Parent: 65.5ms
/Users/fukumone/private_repo/rails_test/app/views/posts/index.html.erb
Children: total time: 6.9ms, partial page count: 3, total rendering page count: 12
5.4ms: /Users/fukumone/private_repo/rails_test/app/views/articles/_show.html.erb
rendering page count: 10
0.8ms: /Users/fukumone/private_repo/rails_test/app/views/articles/_show_3.html.erb
0.7ms: /Users/fukumone/private_repo/rails_test/app/views/articles/_show_2.html.erb
Rails::Log::Profilingはオプションが用意されています
Rails::Log::Profiling.query_profiling_enable
: true => クエリのプロファイリングを有効にするRails::Log::Profiling.view_profiling_enable
: true => viewのプロファイリングを有効にするRails::Log::Profiling.sort_order
: rails_log_query_profiling.logのクエリの並び順を指定する、降順はdesc、昇順はascと設定してください。初期設定はdescになっていますRails::Log::Profiling.continue_to_query_caller
: true => rails_log_query_profiling.logにてクエリメソッドの呼び出し状況を最後までトレースし記録します、探知する量が多くなってしまう場合があるため、初期設定はfalseになっています
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
MIT