Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I profile a request in Ruby on Rails?

How can I profile a controller action?

One of my views is taking quite some time to render, and I'd like to break it down. I see script/performance/profiler, but that seems to only have access to the global scope.

like image 764
ryeguy Avatar asked Apr 26 '10 19:04

ryeguy


People also ask

What is rack mini profiler?

Middleware that displays speed badge for every html page. Designed to work both in production and in development.

What is Stackprof?

stackprof is a fast sampling profiler for ruby code, with cpu, wallclock and object allocation samplers.

How do I disable mini profiler?

To turn the profiler off, use disable. So, http://localhost:3000/articles?pp=disable would disable the profiler until it was re-enabled. To turn it back on, use enable.


2 Answers

ruby-prof is the way to go. Here's a howto, How to profile your Rails and Ruby applications with ruby-prof.

If you use it in combination with a visualisation tool such as kcachegrind, you can easily separate the code that is your application code from the framework code.

I gave a talk on these tools at my local Ruby-users group a while back: Making your rails app kick ass with ruby-prof and kcachegrind.

like image 70
Taryn East Avatar answered Oct 03 '22 12:10

Taryn East


The Ruby on Rails console should show you which partials are taking a while to render. If that's not enough, you could install the new relic gem (https://github.com/newrelic/rpm) and use that in developer mode which will give you an overview of what's going on.

If you're deploying on Heroku you can add the free version of rpm which will give you the same statistics on your running application.

Ruby has a profiler (http://ruby-prof.rubyforge.org/) which will show you memory usage, call times, etc., but it might be hard to disseminate your code from the framework code.

like image 36
jonnii Avatar answered Oct 03 '22 12:10

jonnii