16 Sept 2026 · 2 min read
Profiling Magento 2: finding the real bottleneck
How to profile a Magento store with Blackfire or New Relic and read flamegraphs to target the actual slow code
Part 1 of the Magento 2 Performance/DevOps series — measure before you optimize.
Guesswork is how you waste a sprint
The slow route has a cause, and it is almost never where the team assumes. "It's the database" or "it's the theme" are hypotheses, not diagnoses. Profiling replaces the hypothesis with a flamegraph: a measured call stack showing exactly which function consumed the time. Optimize the widest frame and you move the number; optimize a guess and you ship nothing.
The tools
- Blackfire — function-level PHP profiling with call graphs; ideal for pinpointing a hot method in a request.
- New Relic — transaction and tier-level tracing across web, database, and external calls; ideal for production trends.
- XHProf / Tideways — lighter-weight alternatives for local capture.
The choice depends on where you measure: Blackfire for a controlled local capture of one slow action, New Relic for "which transactions are slow in production right now."
Capturing a useful trace
Profile the specific slow action, not the homepage by accident:
# Blackfire shape (locally against a Govard env)
blackfire run --slot=https curl https://store.example.test/category/slow-category
Then open the generated graph and look for the widest frame — the function with the most self-time or the deepest repeated subtree. A category page that loads the same EAV attribute in a loop shows up as one attribute-read method dominating the graph.
Common hotspots
- EAV lazy loads in a loop. Reading product attributes one at a time inside a collection iteration. Fix by eager-loading via a join or a single bulk fetch.
- Heavy plugins/observers on
loadorsaveevents firing far more often than needed. The graph reveals the observer at the top of a hot subtree. - Layout block overhead — blocks re-rendering on every request because cache keys are too narrow. Visible as repeated render calls in the trace.
Pitfalls
- Profiling overhead skews the numbers. A profiler adds cost; treat relative widths as truth, absolute times as approximate.
- Optimizing the wrong frame. Fixing a leaf that is cheap to call but rarely hot wastes effort. Target the widest frame.
- No baseline. Capture a "before" trace and keep it; without one you cannot prove the fix helped.
What's next
Profiling tells you what to fix; the pipeline should stop new slow code from landing. The next post builds CI quality gates that block regressions before merge.
Next in this series: CI quality gates for Magento 2 —
/blog/magento2-devops-ci-quality-gates