rack-mini-profiler gem is a middleware that displays speed badge for every html page. Designed to work both in production and in development.
Database profiling - Currently supports Mysql2, Postgres, Oracle (oracle_enhanced ~> 1.5.0) and Mongoid3 (with fallback support to ActiveRecord)
Call-stack profiling - Flame graphs showing time spent by gem
Memory profiling - Per-request memory usage, GC stats, and global allocation metrics
You can install it as a gem:
gem install rack-mini-profiler
or add it into a Gemfile (Bundler):
All you have to do is to include the Gem and you’re good to go in development. See notes below for use in production.
Rails and manual initialization
In case you need to make sure
rack_mini_profiler is initialized after all other gems, or you want to execute some code before
gem 'rack-mini-profiler', require: false
Note the require: false part - if omitted, it will cause the Railtie for the mini-profiler to be loaded outright, and an attempt to re-initialize it manually will raise an exception.
Then run the generator which will set up
rack-mini-profiler in development:
bundle exec rails g rack_profiler:install
A typical web application spends a lot of time querying the database. rack_mini_profiler will detect the ORM that is available and apply patches to properly collect query statistics.
To make this work, declare the orm’s gem before declaring rack-mini-profiler in the Gemfile:
If you wish to override this behavior, the environment variable
RACK_MINI_PROFILER_PATCH is available.
To generate flamegraphs:
stackprof gem to your Gemfile
visit a page in your app with
Memory allocations can be measured (using the memory_profiler gem) which will show allocations broken down by gem, file location, and class and will also highlight String allocations.
?pp=profile-memory to the URL of any request while
Rack::MiniProfiler is enabled to generate the report.
Additional query parameters can be used to filter the results.
memory_profiler_allow_files - filename pattern to include (default is all files)
There are two additional
pp options that can be used to analyze memory which do not require the
?pp=profile-gc to report on Garbage Collection statistics
?pp=analyze-memory to report on ObjectSpace statistics
Access control in non-development environments
rack-mini-profiler is designed with production profiling in mind. To enable that run Rack::MiniProfiler.authorize_request once you know a request is allowed to profile.
# inside your ApplicationController
If your production application is running on more than one server (or more than one dyno) you will need to configure rack mini profiler’s storage to use Redis or Memcache. See storage for information on changing the storage backend.
Out-of-the-box we will initialize the authorization_mode to :allow_authorized in production. However, in some cases we may not be able to do it:
If you are running in development or test we will not enable the explicit authorization mode
If you use require: false on rack_mini_profiler we are unlikely to be able to run the railtie
If you are running outside of rails we will not run the railtie
In those cases use:
Rack::MiniProfiler.config.authorization_mode = :allow_authorized
When deciding to fully profile a page mini profiler consults with the authorization_mode
By default in production we attempt to set the authorization mode to
:allow_authorized meaning that end user will only be able to see requests where somewhere
Rack::MiniProfiler.authorize_request is invoked.
In development we run in the
:allow_all authorization mode meaning every request is profiled and displayed to the end user.
In a complex web application, it’s possible for a request to trigger rare conditions that result in poor performance. Mini Profiler ships with a feature to help detect those rare conditions and fix them. It works by enabling invisible profiling on one request every N requests, and saving the performance metrics that are collected during the request (a.k.a snapshot of the request) so that they can be viewed later. To turn this feature on, set the snapshot_every_n_requests config to a value larger than 0. The larger the value is, the less frequently requests are profiled.
Mini Profiler will exclude requests that are made to skipped paths (see skip_paths config below) from being sampled. Additionally, if profiling is enabled for a request that later finishes with a non-2xx status code, Mini Profiler will discard the snapshot and not save it (this behavior may change in the future).
Various aspects of rack-mini-profiler’s behavior can be configured when your app boots. For example in a Rails app, this should be done in an initializer: