[Awesome Ruby Gem] Use rails_mini_profiler gem to provide an easy-to-use performance profiler for your Rails applications

Rails Mini Profiler

Rails Mini Profiler is an easy-to-use performance profiler for your Rails applications. It is heavily inspired by Rack Mini Profiler GitHub - MiniProfiler/rack-mini-profiler: Profiler for your development and production Ruby rack apps. - https://github.com/MiniProfiler/rack-mini-profiler and other APM tools.

Rails Mini Profiler improves upon rack-mini-profiler in the latter regard. It is a developer tool, rather than a monitoring tool, and sets a big focus on developer experience. Simply put, it aims to be the best tool available to help you figure out why specific requests are slow.

As such, compared to rack-mini-profiler, it does not support non-Rails apps (e.g. Sinatra) or production sampling, but provides a much better user experience and better supports API-only applications.

Installation

You can install it as a gem:

1
$ gem install rails_mini_profiler

or add it into a Gemfile (Bundler):

1
2
3
4
5
6
7
8
9
10
# Gemfile

# Put gems used only for development or testing in the appropriate group in the Gemfile
group :development do

gem 'rails_mini_profiler', '0.2.1'

# For call-stack profiling flamegraphs
gem 'stackprof'
end

Usages

Run the installer:

1
$ rails rails_mini_profiler:install

Inspect the generated migration in db/migrate and run it:

1
$ rails db:migrate

Start your Rails application and perform some requests. You can either click the little hedgehog 🦔 on the top left or navigate to /rails_mini_profiler to view collected performance metrics.

Rails Mini Profiler provides detailed information about your requests to help you figure out why certain requests perform poorly.

Installing it will generate a new initializer config/initializers/rails_mini_profiler.rb and add a new route:

1
2
3
4
5
6
# routes.rb
Rails.application.routes.draw do
...

mount RailsMiniProfiler::Engine => '/rails_mini_profiler'
end

Once you perform requests against your applications you can inspect them using that route, or by clicking the badge on the top right that is injected into your pages.

Rails Mini Profiler Request Overview

Requests to your application will be profiled automatically. You can view all stored requests by navigating to yourapp/rails_mini_profiler/profiled_requests.

This view shows you how your requests spend their time. How much of it is spent in the DB, how much in rendering views? By clicking on individual traces you can find out detailed information.

Flamegraphs

Rails Mini Profiler automatically records Flamegraphs for profiled requests. To enable this feature, add Stackprof to your Gemfile:

1
gem 'stackprof'

For convenience, Flamegraphs are recorded for every request. This may incur a significant performance penalty. To change the default behavior see Configuration.

Flamegraphs are rendered using Speedscope. See Troubleshooting if Flamegraphs are not rendering correctly.

Configuration

Rails Mini Profiler provides a wide array of configuration options. You can find details below. For an example configuration check initializers/rails_mini_profiler.rb (or the template file).

Clean up

Rails Mini Profiler does not offer an automatic way to clean up old profiling information. It is recommended you add a sweeper job to clean up old profiled requests periodically (e.g. using clockwork. For example, with ActiveJob:

1
2
3
4
5
6
7
8
9
10
11
12
13
# Clockwork
every(1.month, 'purge rails mini profiler' do
ProfiledRequestCleanupJob.perform_later
end

# ActiveJob
class ProfiledRequestCleanupJob < ApplicationJob
queue_as :default

def perform
RailsMiniProfiler::ProfiledRequest.where('created_at < ?', 1.month.ago).destroy_all
end
end

Users

Profiling information is segregated by user ID. That means users cannot see each other’s profiled requests.

Per default, individual users are identified by their IP address. You may change this by setting a custom user provider:

1
config.user_provider = proc { |env| Rack::Request.new(env).ip }

You may also explicitly set the user from the application itself:

1
2
3
4
5
class ApplicationController < ActionController::Base
before_action do
RailsMiniProfiler::User.authorize(current_user.id)
end
end

Note that you must set the current user when running Rails Mini Profiler in production. No profiles will be saved otherwise.


Profiling in Production

Rails Mini Profiler is not intended for performance reporting. There are other tools for that ( Skylight, New Relic, DataDog…).

However, you can still use it in production to profile specific requests. Since profiling impacts performance, it is recommended that you limit which requests are being profiled:

1
2
3
RailsMiniProfiler.configure do |config|
config.enabled = proc { |env| env.headers['RMP_ENABLED'].present? }
end

Only requests by explicitly set users will be stored. To configure how individual users are identified see Users

References

[1] hschne/rails-mini-profiler: Performance profiling for Rails, made simple 🦔 - https://github.com/hschne/rails-mini-profiler

[2] rails_mini_profiler | RubyGems.org | your community gem host - https://rubygems.org/gems/rails_mini_profiler

[3] GitHub - MiniProfiler/rack-mini-profiler: Profiler for your development and production Ruby rack apps. - https://github.com/MiniProfiler/rack-mini-profiler

[4] tmm1/stackprof: a sampling call-stack profiler for ruby 2.2+ - https://github.com/tmm1/stackprof