LoginSignup
4
3

More than 5 years have passed since last update.

Rails で Controller の特定の Action にプロファイラを仕掛ける

Posted at

Rails で特定のエンドポイントにプロファイラを仕掛けたいときに、どのようにコードを埋め込めば良いか、書いておきます。

stackprof

class FooController < ApplicationController
  around_action :stackprofile

  def index
  end

  :

  private

  def stackprofile(&action)
    profile_dump = Rails.root.join('tmp', "stackprof-cpu-#{$$}.#{Time.current.to_i}.dump")
    StackProf.run(mode: :cpu, out: profile_dump, &action)
  end
end

参考:

memprof2

class FooController < ApplicationController
  require 'memprof2' # Do not require by default
  around_action :memprof2

  def index
  end

  :

  private

  def memprof2
    Memprof2.run do
      begin
        yield
      ensure
        Memprof2.report(out: Rails.root.join('tmp', "memprof-report.#{$$}.#{Time.current.to_i}.txt")
      end
    end
  end
end

参考:

参考

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