Profiling

Improving performance is three questions: what is slow, why is it slow, and did the fix work? Monitoring tools answer the first in production. Rails Mini Profiler answers the second and third at your desk: a waterfall of every query, template, component render, and cache call in a request, plus a flamegraph, one click from the page you are looking at. This guide covers installing our fork, reading a profile, configuring it, and safely turning it on for a staging or production request when the slowness only happens there.

1. Why Rails Mini Profiler

Two tools share the name and the hedgehog. rack-mini-profiler is the veteran: framework-agnostic, capable of light production sampling, and shown as a floating badge with a timing table. Rails Mini Profiler trades the generality for depth. It is Rails-only, it stores every profiled request in your database so you can compare a request before and after a change, and it puts the detail — a real waterfall, per-trace popovers, a Speedscope flamegraph — in a dashboard built for reading rather than a tooltip.

It is a developer tool, not monitoring. For "which endpoints are slow for real users this week" you want an APM. For "this page takes two seconds on my machine and I don't know where", this is the fastest route to the answer.

2. Installation

We install our fork from Git — a plain gem "rails_mini_profiler" would pull the archived original (see section 6). Put it in the main group, not :development, if you ever want to profile a staging or production request; it is inert until enabled. Add stackprof alongside it for flamegraphs:

# Gemfile
gem "rails_mini_profiler", git: "https://github.com/profoundry-us/rails-mini-profiler", branch: "main"

# Flamegraphs for rails_mini_profiler
gem "stackprof"

The installer writes an initializer, a migration for the three tables that hold profiled requests, traces, and flamegraphs, and mounts the dashboard. Run it, then migrate:

bundle install
bin/rails rails_mini_profiler:install
bin/rails db:migrate
# config/routes.rb (added by the installer)
mount RailsMiniProfiler::Engine => "/rails_mini_profiler"

One more step that the installer cannot do for you. ViewComponent does not instrument renders by default, so the component tracer sees nothing until the host app turns instrumentation on. Do it in the initializer rather than application.rb so it can be gated later (see section 5):

# config/initializers/rails_mini_profiler.rb
Rails.application.config.view_component.instrumentation_enabled = true
Rails.application.config.view_component.use_deprecated_instrumentation_name = false

Start the app and load any page. A small hedgehog badge appears in the corner; click it — or visit /rails_mini_profiler — for the dashboard.

3. Reading a Profile

The dashboard lists every profiled request with its total time and a breakdown. Open one and you get the waterfall: a bar per trace, positioned in time and nested by what called what, with the time a trace spent itself (as opposed to in its children) shown explicitly. Each bar is a tracer's output:

TracerWhat you see
controller

The action itself — the outer bar every other trace nests inside.

sequel

Every SQL statement, with its duration, row count, and (in the fork) an N+1 flag when the same shape repeats.

instantiation

Turning rows into ActiveRecord objects — the cost that hides behind a fast query.

view

Templates, partials, collections, and layouts.

view_component

ViewComponent renders, with the arguments each component was built with, so you can tell one IconComponent from another. Fork addition.

cache

Every Rails.cache read, write, fetch hit, generate, and delete. Fork addition.

job

Jobs enqueued during the request (their execution happens elsewhere). Fork addition.

Three habits get you to the answer quickly:

  • Sort by self time, not total. The controller bar is always the longest; the question is which leaf under it is.
  • Collapse the tree and look for grouped repeats. Fifty identical SELECT … WHERE id = ? rows grouped into one line with a count is an N+1, and the popover names the query so you know which includes to add.
  • Open the flamegraph when the waterfall is quiet. If the request is slow but no single trace is, the time is in Ruby — serialisation, a helper, a gem — and the flamegraph shows the stack that ate it.
Flamegraphs and Concurrency

StackProf cannot sample concurrent requests, so a page that fires several requests at once (Turbo frames, assets) may record a flamegraph for only one of them. Re-send just the request you care about, or append ?rmp_flamegraph=true to jump straight to its flamegraph.

4. Configuration

The generated initializer is well commented; three settings are worth changing on day one.

Turn off per-trace backtraces. Every trace captures a backtrace for the "source" section of its detail view. On a component-heavy page that capture measured around 30% of the profiled request — the profiler was the slowest thing in the profile. Leave it off for honest numbers and flip it on only when you need to know where a trace originates.

Skip the noise. Health checks, asset requests, and the profiler's own dashboard don't need profiling. skip_paths takes strings or regexes.

Sweep old requests. Profiled requests are never deleted for you. On a shared staging database the tables grow forever; a small job on a schedule keeps them bounded.

# config/initializers/rails_mini_profiler.rb
RailsMiniProfiler.configure do |config|
  config.enabled = proc { |env| Rails.env.development? }

  # Backtrace capture measured ~30% of a profiled request; the trace
  # detail view just omits its "source" section with this off.
  config.backtraces_enabled = false

  config.flamegraph_enabled = true
  config.skip_paths = ["/up", %r{\A/assets/}]

  # Default set — drop `view_component` if the app doesn't use it.
  config.tracers = %i[controller instantiation sequel view view_component cache job rmp]
end
# app/jobs/profiled_request_cleanup_job.rb
class ProfiledRequestCleanupJob < ApplicationJob
  def perform
    RailsMiniProfiler::ProfiledRequest.where("created_at < ?", 2.weeks.ago).destroy_all
  end
end

5. Profiling Beyond Development

Some slowness only exists with real data. Rails Mini Profiler supports this — nothing is profiled and the dashboard is unreachable until you say otherwise — but the stock suggestion (a request header) is awkward from a browser, and every profiled request costs the server. Our pattern is a key-approved session:

  1. Visit any URL once with ?rmp_key=<secret> (the secret lives in an RMP_API_KEY environment variable).
  2. A small middleware compares it in constant time, stamps the session as approved, and authorises the profiler "user" on every later request in that session.
  3. config.enabled and the dashboard's route constraint both check the session flag, so approved sessions are profiled and can read the dashboard; everyone else sees no badge, no traces, and a 404 for /rails_mini_profiler.

Rotating the key is a config change. The full middleware is about forty lines; the shape is:

# config/initializers/rails_mini_profiler.rb
class RmpApiKeyGate
  SESSION_FLAG = "rmp_authorized"
  ENV_FLAG = "app.rmp_authorized"

  def initialize(app) = @app = app

  def call(env)
    session = env["rack.session"]
    session[SESSION_FLAG] = true if session && key_valid?(env)

    if session && session[SESSION_FLAG]
      env[ENV_FLAG] = true
      RailsMiniProfiler::User.authorize("rmp-api-key")
    end

    @app.call(env)
  end

  private

  def key_valid?(env)
    key = ENV["RMP_API_KEY"].to_s
    given = Rack::Utils.parse_query(env["QUERY_STRING"].to_s)["rmp_key"].to_s
    !key.empty? && !given.empty? && ActiveSupport::SecurityUtils.secure_compare(given, key)
  end
end

Rails.application.config.middleware.insert_after(ActionDispatch::Session::CookieStore, RmpApiKeyGate)
Rails.application.config.filter_parameters += [:rmp_key]

RailsMiniProfiler.configure do |config|
  config.enabled = proc { |env| Rails.env.development? || env[RmpApiKeyGate::ENV_FLAG] }
end
# config/routes.rb
mount RailsMiniProfiler::Engine => "/rails_mini_profiler",
      constraints: ->(req) { Rails.env.development? || req.session[RmpApiKeyGate::SESSION_FLAG] == true }
Instrumentation Is Not Free

ViewComponent instrumentation fires a notification on every component render whether or not the request is being profiled. Enable it outside development only when the deployment can actually profile — if Rails.env.development? || ENV["RMP_API_KEY"].present? — so a production app without a key pays nothing. And treat the key as rotatable, not secret: it travels in a query string, which your host's router may log even though Rails filters it.

6. Why a Fork

The original project's last release was v0.7.3 in October 2022, and it profiles the Rails of that era: controllers, SQL, and templates. A LocoMotion app renders through components, leans on the cache, and enqueues jobs from requests — none of which appeared in a profile. We maintain profoundry-us/rails-mini-profiler to fill those gaps, and it is what the guide above installs.

The fork addsBecause

ViewComponent, cache, job, collection, and layout tracers

Upstream traces controllers, SQL, and templates; a component-based Hotwire app spends its time in exactly the places that left blank.

Waterfall-aligned bars with explicit self time, a collapsible trace tree, grouped repeats

A page rendering 300 components produced 300 flat rows; the tree groups them and shows what each level costs itself.

Richer popovers: group stats, N+1 flags, row counts, params, component props

The questions you ask of a slow trace, answered in place instead of in the console.

backtraces_enabled config

Backtrace capture measured about 30% of a profiled request on a component-heavy page — the profiler was distorting what it measured.

Sprockets / importmap fixes, a raw-SQL bind crash fix, a Dockerised dev environment

Upstream's last release was v0.7.3 in October 2022; these are the fixes that accumulated since.

The fork tracks main; there is no separate gem release, which is why the Gemfile points at Git. Upgrading means bundle update rails_mini_profiler and, if the schema changed, re-running the install migrations — the project is pre-1.0 and does not ship upgrade migrations.

Success!

You can see where every request spends its time, on your machine and — when you need to — on a real deployment. Before you move on:

Made with by Profoundry .
Copyright © 2023-2026 Profoundry LLC.
All rights reserved.