Tips and Tools for Identifying Performance Bottlenecks
And why you should listen to me
Or maybe your app resides on one network and your DB on another, and you're doing
select * from a_table
with many records & a ton of
gem 'ruby-prof', groups: [:development, :profile] # in Gemfile
if Rails.env.profile? # in config.ru
use Rack::RubyProf, :path => '/temp/profile'
end
# profile db connection settings in config/database.yml
# (same as dev environment is fine)
# Create config/environments/profile.rb with at least these options:
config.cache_classes = true
config.cache_template_loading = true
# bundle install and restart with: rails s -e profile
result = RubyProf.profile do
...
[code to profile]
...
end
printer=RubyProf::GraphPrinter.new(result)
printer.print(STDOUT, {})
Benchmark.bm do |x|
x.report("find by id: ") {
@student = Student.find_by_id(params[:student_id])
}
x.report("find: ") {
@student = @district.students.find(params[:student_id])
}
end
user system total real
find by id: 0.090000 0.000000 0.090000 ( 1.352654)
find: 0.030000 0.000000 0.030000 ( 0.030710)
i.e., how a web server is supposed to work-Wes Gamble
# in config/environments/production.rb
config.threadsafe!
ActiveSupport::Notifications.instrument("render", :extra => :information) do
render :text => "Foo"
end
ActiveSupport::Notifications.subscribe("render") do |*args|
events << ActiveSupport::Notifications::Event.new(*args)
end