RubyProfを使って、Railsアプリのプロファイリングをする方法を紹介します。

まずは、ruby-profをGemでインストールします。

   1  % sudo gem install ruby-prof

インストールが完了したら、ruby-profプラグインをRailsアプリにインストールします。 ruby-profプラグインは、Gemがインストールされたディレクトリの下にあります。 環境によって場所は変わりますが、例えば/usr/local/lib/ruby/gems/1.8/gems/ruby-prof-0.6.0/rails_plugin/ruby-profなどの場所にあります。

これをvendor/plugins/ruby-profにコピーすれば設定は完了です。

あとはproduction環境でRailsアプリにアクセスすると、以下のようなログが出力されます。

   1  Thread ID: 3076980460
   2  Total: 2.030000
   3  
   4   %self   total   self   wait  child  calls  name
   5   20.20    0.80   0.41   0.00   0.39   1649  Array#each_index
   6    8.87    0.25   0.18   0.00   0.07 106376  Kernel#===
   7    3.94    0.11   0.08   0.00   0.03   3044  <Module::Marshal>#load
   8    3.94    0.08   0.08   0.00   0.00  26468  PGresult#type
   9    3.45    0.21   0.07   0.00   0.14   3044  <Class::Thread>#exclusive
  10    3.45    0.07   0.07   0.00   0.00 106761  Fixnum#==
  11  (** snip **)

See Also

posted by Png genki on Sat 28 Jun 2008 at 18:11

caches_pageとの違い

caches_actionはcaches_pageと似ていますが、依然としてすべてのリクエストがAction Packを通って処理されるようになっています。これは認証や他の制限をつけたいときに便利です。

"/lists"と"/lists.xml"は別々にキャッシュされる

また、http://blog.s21g.com/listsとhttp://blog.s21g.com/lists.xmlは異なるリクエストとして処理され、別々にキャッシュされます。つまり、:action => "lists"のキャッシュが無効になったからといって、:actioin => "list", :format => ":xml"も無効になるわけではありません。

キャッシュの無効化

expire_actionでキャッシュを無効化できます。Sweepwerを使って処理、タイミングを書く。caches.rbのサンプルを抜き出します。

   1  class ListSweeper < ActionController::Caching::Sweeper
   2      observe List, Item
   3  
   4      def after_save(record)
   5         list = record.is_a?(List) ? record : record.list
   6         expire_page(:controller => "lists", :action => %w( show public feed ), :id => list.id)
   7         expire_action(:controller => "lists", :action => "all")
   8          list.shares.each { |share| expire_page(:controller => "lists", :action => "show", :id => share.url_key) }
   9        end
  10     end
  11   
  12  class ListsController < ApplicationController
  13      caches_action :index, :show, :public, :feed
  14      cache_sweeper :list_sweeper, :only => [ :edit, :destroy, :share ]
  15  end

Refs

vendor/rails/actionpack/lib/action_controller/caching.rb
http://scottstuff.net/presentations/rails-caching/ かなり詳しい
http://www.ibm.com/developerworks/web/library/wa-rails1/
http://www.hostingrails.com/forums/rails_coding_thread/386

posted by Png satoko on Sat 28 Jun 2008 at 01:19
Contents
RubyProfでRailsアプリのプロファイリングをする方法
caches_actionの動作についてメモ:action_controller/caching.rbを読む
Comments
瀧内元気: MacOS版は以下にあります * [genki/ViMouse](https://githu... '23-1
KingofSmack: Here also good reads for this mobile applicatio... '14-5
Spencer: You don't have to re-compile it, this version w... '14-4
staiano: Any chance we can get a recompile for 10.9? '14-1
dsjf: https://gist.github.com/6bf1bf2c3cbb5eb6e7a7 これ... '13-1
Services from s21g
twpro(ツイプロ)
Twitterプロフィールを快適検索
地価2009
土地の値段を調べてみよう
MyRestaurant
自分だけのレストラン手帳
Formula
ブログに数式を埋め込める数式コミュニティ