Created
November 23, 2011 20:10
-
-
Save ksarna/1389759 to your computer and use it in GitHub Desktop.
Profiling rails requests with ruby-prof
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# You can use this class in your console. For example | |
# p = ProfilingTools.new | |
# p.profiled_request(:controller => :welcome, :action => :index) | |
# this will profile whole application stack and save file in your tmp/profiler folder | |
# You can also use +request+ method just to see the output for example: | |
# | |
# p.request(:controller => :offers, :action => :index) | |
# | |
# p.response.body | |
# p.response.cookies # and so on | |
# | |
# Here's manual for reading profiler report: http://ruby-prof.rubyforge.org/graph.txt | |
class ProfilingTools | |
attr_accessor :response | |
def request(options = {}) | |
url=app.url_for(options) | |
app.get(url) | |
@response = app.response | |
puts app.html_document.root.to_s | |
end | |
# use this in production env or other env where you have: config.cache_classes = true | |
def profiled_request(options = {}) | |
require 'ruby-prof' | |
# Profile the code | |
url = app.url_for(options) | |
RubyProf.start | |
app.get(url) | |
results = RubyProf.stop | |
@response = app.response | |
save_profiling_results(results) | |
end | |
private | |
def save_profiling_results(results) | |
folder = 'tmp' | |
File.open "#{RAILS_ROOT}/#{folder}/profile-graph.html", 'w+' do |file| | |
RubyProf::GraphHtmlPrinter.new(results).print(file) | |
end | |
File.open "#{RAILS_ROOT}/#{folder}/profile-flat.txt", 'w+' do |file| | |
RubyProf::FlatPrinter.new(results).print(file) | |
end | |
File.open "#{RAILS_ROOT}/#{folder}/profile-tree.prof", 'w+' do |file| | |
RubyProf::CallTreePrinter.new(results).print(file) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment