Five Tools for Profiling Rails Apps
Five Tools for Profiling Rails Apps
What is a Rails profiler, and why should I use one?
A Rails profiler is a tool used to analyze the performance of your Ruby on Rails application. It helps identify bottlenecks, memory leaks, and other performance issues, allowing you to optimize your code and improve overall web application speed. Profilers are essential in ensuring your web application runs smoothly and delivers a better user experience.
Choosing the right Rails profiler can greatly impact the performance and efficiency of your application. Different profilers offer varying features and cater to specific use cases. Here, I'll explore some popular Rails profilers and help you decide which one suits your needs best.
What are the best Rails profilers available?
There are several popular Rails profilers available, including rack-mini-profiler, DevTrace, and Rails Panel. Finding a Rails profiler that reliably works, however, can be a bigger challenge than expected. Below I’ll summarize the Rails profiler ecosystem and my experience with the most popular options, including each profiler's niche, strengths, and weaknesses.
rack-mini-profiler
rack-mini-profiler is a lightweight profiler that provides in-depth analysis of your application's SQL queries, view rendering times, and memory allocations. It instruments database calls, profiles the callstack with flamegraphs, and provides a number of memory-related metrics. Finally, it's easy to set up and provides real-time insights while you're developing your application.
rack-mini-profiler pros
-
It provides solid traces with breakdowns of partial rendering times and database query times
-
It hooks into other tools like memory profiling and stackprof
-
It’s possible to run in production
-
No noticeable performance overhead for timing instrumentation
rack-mini-profiler cons
-
It’s not possible to run multiple types of profilers at once (e.g. timing, memory, stackprof)
-
It requires that some profiling modes (e.g. memory and stackprof) be explicitly run vs. only accessed if you see an anomaly
-
Occasional issues where AJAX requests aren't properly captured
DevTrace
DevTrace is Scout's free Rails profiler and is included with the scout_apm gem. DevTrace provides the same level of traces Scout provides for app monitoring: instrumenting view rendering, database calls, and memory allocations. When run with ScoutProf – Scout's stack profiler – it also profiles your custom code.
DevTrace pros
-
It just works
-
It makes traces easily readable and consistent with those collected during monitoring
-
It profiles timing, memory, and the stack on every request
-
It’s easy to share traces with your team
-
No noticeable performance overhead
DevTrace cons
-
It only supports the development environment
-
Framework support is limited to Rails
Rails Panel
One part Ruby gem and one part Chrome extension, Rails Panel exposes details on your app's requests in the Chrome Developer Panel. Rails Panel instruments ActiveRecord and rendering calls.
Rails Panel pros
-
It’s unobtrusive, allowing you to expose and hide the developer panel to reveal details as needed
-
It integrates with popular editors to provide direct links from backtraces
-
No configuration required outside of the gem and Chrome extension install
Rails Panel cons
-
The Chrome Developer Panel must be open to capture request details - if it's hidden while an issue occurs, the request isn't captured
-
Framework support is limited to Rails
-
Tracing isn't as advanced as rack-mini-profiler or DevTrace
-
Tracing is limited to ActiveRecord and rendering calls
Which Rails profilers are difficult to work with?
Rack::Insight – I didn't have much luck getting this going against a Rails 4 app. Only my initial request appeared in the trace panel, app response times were noticeably slower, and the metrics I did see were not at the level of detail of rack-mini-profiler or Scout DevTrace.
Peek – I was able to get this partially working against a Rails 4 app but several of the panels didn't work, including the performance bar (a key panel). The initial configuration is more involved than all of the other tools as well. Peek also didn't run, or caused "stack level too deep errors" when app monitoring services like New Relic or Scout ran alongside it. All of the other profilers worked seamlessly with APM services.
Which Rails profiler should I use?
While rack-mini-profiler, DevTrace, and Rails Panel can run simultaneously without error, using all three at once is sure to cause information overload. You should choose a single Rails profiler based on how the strengths of each aligns with your specific needs. These are the sweet spots for each of the above profilers:
rack-mini-profiler
rack-mini-profiler is a great fit for developers that enjoy detailed statistics on performance. This is a tool that's not afraid to dump a significant amount of data into a developer’s lap. It’s also the only option for non-Rails apps (e.g. Sinatra) and running in production.
DevTrace
Do you value polish and filtering on performance metrics over a raw dump of data? DevTrace works to distill information into a clear and compressed view, providing the same timing, memory, and sampling data as rack-mini-profiler but in a more digestible manner.