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
- Visit our community
- Watch the RailsCast
- Read about Flame graphs in rack-mini-profiler
- Read the announcement posts from 2012
We have decided to restructure our repository so there is a central UI repo and the various language implementations have their own.
WE NEED HELP.
- Help triage issues
If you feel like taking on any of this start an issue and update us on your progress.
Install/add to Gemfile in Ruby 2.4+
gem 'rack-mini-profiler'NOTE: Be sure to require rack_mini_profiler below the pg and mysql gems in your Gemfile. rack_mini_profiler will identify these gems if they are loaded to insert instrumentation. If included too early no SQL will show up.
You can also include optional libraries to enable additional features.
# For memory profiling
gem 'memory_profiler'
# For call-stack profiling flamegraphs
gem 'flamegraph'
gem 'stackprof'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.
Prior to version 2.0.0, Mini Profiler patched various Rails methods to get the information it needed such as template rendering time. Starting from version 2.0.0, Mini Profiler doesn't patch any Rails methods by default and relies on ActiveSupport::Notifications to get the information it needs from Rails. If you want Mini Profiler to keep using its patches in version 2.0.0 and later, change the gem line in your Gemfile to the following:
If you want to manually require Mini Profiler:
gem 'rack-mini-profiler', require: ['enable_rails_patches']If you don't want to manually require Mini Profiler:
gem 'rack-mini-profiler', require: ['enable_rails_patches', 'rack-mini-profiler']If you start seeing SystemStackError: stack level too deep errors from Net::HTTP after installing Mini Profiler, this means there is another patch for Net::HTTP#request that conflicts with Mini Profiler's patch in your application. To fix this, change rack-mini-profiler gem line in your Gemfile to the following:
gem 'rack-mini-profiler', require: ['prepend_net_http_patch', 'rack-mini-profiler']If you currently have require: false, remove the 'rack-mini-profiler' string from the require array above so the gem line becomes like this:
gem 'rack-mini-profiler', require: ['prepend_net_http_patch']This conflict happens when a ruby method is patched twice, once using module prepend, and once using method aliasing. See this ruby issue for details. The fix is to apply all patches the same way. Mini Profiler by default will apply its patch using method aliasing, but you can change that to module prepend by adding require: ['prepend_net_http_patch'] to the gem line as shown above.
In case you need to make sure rack_mini_profiler is initialized after all other gems, or you want to execute some code before rack_mini_profiler required:
gem 'rack-mini-profiler', require: falseNote 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:installrequire 'rack-mini-profiler'
home = lambda { |env|
[200, {'Content-Type' => 'text/html'}, ["<html><body>hello!</body></html>"]]
}
builder = Rack::Builder.new do
use Rack::MiniProfiler
map('/') { run home }
end
run builderrequire 'rack-mini-profiler'
class MyApp < Sinatra::Base
use Rack::MiniProfiler
endFor working with hanami, you need to use rack integration. Also, you need to add Hanami::View::Rendering::Partial#render method for profile:
# config.ru
require 'rack-mini-profiler'
Rack::MiniProfiler.profile_method(Hanami::View::Rendering::Partial, :render) { "Render partial #{@options[:partial]}" }
use Rack::MiniProfilerA 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:
gem 'pg'
gem 'mongoid'
gem 'rack-mini-profiler'If you wish to override this behavior, the environment variable RACK_MINI_PROFILER_PATCH is available.
export RACK_MINI_PROFILER_PATCH="pg,mongoid"
# or
export RACK_MINI_PROFILER_PATCH="false"
# initializers/rack_profiler.rb: SqlPatches.patch %w(mongo)To generate flamegraphs:
- add the flamegraph gem to your Gemfile
- visit a page in your app with
?pp=flamegraph
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.
Add ?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)memory_profiler_ignore_files- filename pattern to exclude (default is no exclusions)memory_profiler_top- number of results per section (defaults to 50)
The allow/ignore patterns will be treated as regular expressions.
Example: ?pp=profile-memory&memory_profiler_allow_files=active_record|app
There are two additional pp options that can be used to analyze memory which do not require the memory_profiler gem
- Use
?pp=profile-gcto report on Garbage Collection statistics - Use
?pp=analyze-memoryto report on ObjectSpace statistics
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 skippd 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).
After enabling snapshots sampling, you can see the snapshots that have been collected at /mini-profiler-resources/snapshots (or if you changed the base_url_path config, substitute mini-profiler-resources with your value of the config). You'll see on that page a table where each row represents a group of snapshots with the duration of the worst snapshot in that group. The worst snapshot in a group is defined as the snapshot whose request took longer than all of the snapshots in the same group. Snapshots grouped by HTTP method and path of the request, and if your application is a Rails app, Mini Profiler will try to convert the path to controller#action and group by that instead of request path. Clicking on a group will display the snapshots of that group sorted from worst to best. From there, you can click on a snapshot's ID to see the snapshot with all the performance metrics that were collected.
Access to the snapshots page is restricted to only those who can see the speed badge on their own requests, see the section below this one about access control.
Mini Profiler will keep a maximum of 1000 snapshots by default, and you can change that via the snapshots_limit config. When snapshots reach the configured limit, Mini Profiler will save a new snapshot only if it's worse than at least one of the existing snapshots and delete the best one (i.e. the snapshot whose request took the least time compared to other snapshots).
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
before_action do
if current_user && current_user.is_admin?
Rack::MiniProfiler.authorize_request
end
endIf 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.
Note:
Out-of-the-box we will initialize the authorization_mode to :whitelist 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 whitelist mode
- If you use
require: falseon 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 = :whitelistWhen 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 :whitelist 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.
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: config/initializers/mini_profiler.rb
To fix some nasty bugs with rack-mini-profiler showing the wrong data, the middleware
will remove headers relating to caching (Date & Etag on responses, If-Modified-Since & If-None-Match on requests).
This probably won't ever break your application, but it can cause some unexpected behavior. For
example, in a Rails app, calls to stale? will always return true.
To disable this behavior, use the following config setting:
# Do not let rack-mini-profiler disable caching
Rack::MiniProfiler.config.disable_caching = false # defaults to truerack-mini-profiler stores its results so they can be shared later and aren't lost at the end of the request.
There are 4 storage options: MemoryStore, RedisStore, MemcacheStore, and FileStore.
FileStore is the default in Rails environments and will write files to tmp/miniprofiler/*. MemoryStore is the default otherwise.
# set MemoryStore
Rack::MiniProfiler.config.storage = Rack::MiniProfiler::MemoryStore
# set RedisStore
if Rails.env.production?
Rack::MiniProfiler.config.storage_options = { url: ENV["REDIS_SERVER_URL"] }
Rack::MiniProfiler.config.storage = Rack::MiniProfiler::RedisStore
endMemoryStore stores results in a processes heap - something that does not work well in a multi process environment.
FileStore stores results in the file system - something that may not work well in a multi machine environment.
RedisStore/MemcacheStore work in multi process and multi machine environments (RedisStore only saves results for up to 24 hours so it won't continue to fill up Redis). You will need to add gem redis/gem dalli respectively to your Gemfile to use these stores.
Additionally you may implement an AbstractStore for your own provider.
MiniProfiler will attempt to keep all user results isolated, out-of-the-box the user provider uses the ip address:
Rack::MiniProfiler.config.user_provider = Proc.new{|env| Rack::Request.new(env).ip}You can override (something that is very important in a multi-machine production setup):
Rack::MiniProfiler.config.user_provider = Proc.new{ |env| CurrentUser.get(env) }The string this function returns should be unique for each user on the system (for anonymous you may need to fall back to ip address)
You can increase the granularity of profiling by measuring the performance of specific methods. Add methods of interest to an initializer.
Rails.application.config.to_prepare do
::Rack::MiniProfiler.profile_singleton_method(User, :non_admins) { |a| "executing all_non_admins" }
::Rack::MiniProfiler.profile_method(User, :favorite_post) { |a| "executing favorite_post" }
endIt is also possible to profile any arbitrary block of code by passing a block to Rack::MiniProfiler.step(name, opts=nil).
Rack::MiniProfiler.step('Adding two elements') do
result = 1 + 2
endSingle page applications built using Ember, Angular or other frameworks need some special care, as routes often change without a full page load.
On route transition always call:
window.MiniProfiler.pageTransition();
This method will remove profiling information that was related to previous page and clear aggregate statistics.
You need to inject the following in your SPA to load MiniProfiler's speed badge (extra details surrounding this script):
<script async type="text/javascript" id="mini-profiler" src="/mini-profiler-resources/includes.js?v=12b4b45a3c42e6e15503d7a03810ff33" data-version="12b4b45a3c42e6e15503d7a03810ff33" data-path="/mini-profiler-resources/" data-current-id="redo66j4g1077kto8uh3" data-ids="redo66j4g1077kto8uh3" data-horizontal-position="left" data-vertical-position="top" data-trivial="false" data-children="false" data-max-traces="10" data-controls="false" data-authorized="true" data-toggle-shortcut="Alt+P" data-start-hidden="false" data-collapse-results="true"></script>Note: The GUID (data-version and the ?v= parameter on the src) will change with each release of rack_mini_profiler. The MiniProfiler's speed badge will continue to work, although you will have to change the GUID to expire the script to fetch the most recent version.
MiniProfiler also ships with a /rack-mini-profiler/requests route that displays the speed badge on a blank HTML page. This can be useful when profiling an application that does not render HTML.
MiniProfiler can be configured so it registers its assets in the assets pipeline. To do that, you'll need to provide a lambda (or proc) to the assets_url config (see the below section). The callback will receive 3 arguments which are: name represents asset name (currently it's either rack-mini-profiling.js or rack-mini-profiling.css), assets_version is a 32 characters long hash of MiniProfiler's assets, and env which is the env object of the request. MiniProfiler expects the assets_url callback to return a URL from which the asset can be loaded (the return value will be used as a href/src attribute in the DOM). If the assets_url callback is not set (the default) or it returns a non-truthy value, MiniProfiler will fallback to loading assets from its own middleware (/mini-profiler-resources/*). The following callback should work for most applications:
Rack::MiniProfiler.config.assets_url = ->(name, version, env) {
ActionController::Base.helpers.asset_path(name)
}You can set configuration options using the configuration accessor on Rack::MiniProfiler.
For example:
Rack::MiniProfiler.config.position = 'bottom-right'
Rack::MiniProfiler.config.start_hidden = trueThe available configuration options are:
| Option | Default | Description |
|---|---|---|
| pre_authorize_cb | Rails: dev only Rack: always on |
A lambda callback that returns true to make mini_profiler visible on a given request. |
| position | 'top-left' |
Display mini_profiler on 'top-right', 'top-left', 'bottom-right' or 'bottom-left'. |
| skip_paths | [] |
An array of paths that skip profiling. Both String and Regexp are acceptable in the array. |
| skip_schema_queries | Rails dev: trueOthwerwise: false |
true to skip schema queries. |
| auto_inject | true |
true to inject the miniprofiler script in the page. |
| backtrace_ignores | [] |
Regexes of lines to be removed from backtraces. |
| backtrace_includes | Rails: [/^\/?(app|config|lib|test)/]Rack: [] |
Regexes of lines to keep in backtraces. |
| backtrace_remove | rails: Rails.rootRack: nil |
A string or regex to remove part of each line in the backtrace. |
| toggle_shortcut | Alt+P | Keyboard shortcut to toggle the mini_profiler's visibility. See jquery.hotkeys. |
| start_hidden | false |
false to make mini_profiler visible on page load. |
| backtrace_threshold_ms | 0 |
Minimum SQL query elapsed time before a backtrace is recorded. |
| flamegraph_sample_rate | 0.5 |
How often to capture stack traces for flamegraphs in milliseconds. |
| base_url_path | '/mini-profiler-resources/' |
Path for assets; added as a prefix when naming assets and sought when responding to requests. |
| collapse_results | true |
If multiple timing results exist in a single page, collapse them till clicked. |
| max_traces_to_show | 20 | Maximum number of mini profiler timing blocks to show on one page |
| html_container | body |
The HTML container (as a jQuery selector) to inject the mini_profiler UI into |
| show_total_sql_count | false |
Displays the total number of SQL executions. |
| enable_advanced_debugging_tools | false |
Enables sensitive debugging tools that can be used via the UI. In production we recommend keeping this disabled as memory and environment debugging tools can expose contents of memory that may contain passwords. |
| assets_url | nil |
See the "Register MiniProfiler's assets in the Rails assets pipeline" section above. |
| snapshot_every_n_requests | -1 |
Determines how frequently snapshots are taken. See the "Snapshots Sampling" above for more details. |
| snapshots_limit | 1000 |
Determines how many snapshots Mini Profiler is allowed to keep. |
If you are using Rack::Deflate with Rails and rack-mini-profiler in its default configuration,
Rack::MiniProfiler will be injected (as always) at position 0 in the middleware stack,
which means it will run after Rack::Deflate on response processing. To prevent attempting to inject
HTML in already compressed response body MiniProfiler will suppress compression by setting
identity encoding in Accept-Encoding request header.
If you include the query string pp=help at the end of your request you will see the various options available. You can use these options to extend or contract the amount of diagnostics rack-mini-profiler gathers.
To get MiniProfiler working with Rails 2.3.X you need to do the initialization manually as well as monkey patch away an incompatibility between activesupport and json_pure.
Add the following code to your environment.rb (or just in a specific environment such as development.rb) for initialization and configuration of MiniProfiler.
# configure and initialize MiniProfiler
require 'rack-mini-profiler'
c = ::Rack::MiniProfiler.config
c.pre_authorize_cb = lambda { |env|
Rails.env.development? || Rails.env.production?
}
tmp = Rails.root.to_s + "/tmp/miniprofiler"
FileUtils.mkdir_p(tmp) unless File.exist?(tmp)
c.storage_options = {:path => tmp}
c.storage = ::Rack::MiniProfiler::FileStore
config.middleware.use(::Rack::MiniProfiler)
::Rack::MiniProfiler.profile_method(ActionController::Base, :process) {|action| "Executing action: #{action}"}
::Rack::MiniProfiler.profile_method(ActionView::Template, :render) {|x,y| "Rendering: #{path_without_format_and_extension}"}
# monkey patch away an activesupport and json_pure incompatability
# http://pivotallabs.com/users/alex/blog/articles/1332-monkey-patch-of-the-day-activesupport-vs-json-pure-vs-ruby-1-8
if JSON.const_defined?(:Pure)
class JSON::Pure::Generator::State
include ActiveSupport::CoreExtensions::Hash::Except
end
endIf you want to contribute to this project, that's great, thank you! You can run the following rake task:
$ bundle exec rake client_dev
which will start a local Sinatra server at http://localhost:9292 where you'll be able to preview your changes. Refreshing the page should be enough to see any changes you make to files in the lib/html directory.
$ rake build
$ rake spec
Additionally you can also run autotest if you like.
The MIT License (MIT)
Copyright (c) 2013 Sam Saffron
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.