Code Profiling

Snippet to make a useful encapsulation in Rails apps.

config/initializers/rubyprof.rb:

1
2
3
4
5
6
7
8
9
10
11
12
13
class Prof
  def self.callstack(filename='callstack.html')
    result = nil
    profile = RubyProf.profile do
      result = yield
    end
    report = RubyProf::CallStackPrinter.new(profile)
    File.open(filename, 'w') do |file|
      report.print(file)
    end
    result
  end
end