Profiling Laravel with Blackfire and Xdebug | Mohamed Said       [Skip to content](#main)  [ ![](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MH.png) Mohamed SaidLaravel Backend Engineer ](https://www.msaied.com/public) - [Home](https://www.msaied.com/public)
- [Projects](https://www.msaied.com/public/projects)
- [Articles](https://www.msaied.com/public/articles)
- [Certificates](https://www.msaied.com/public/certificates)
- [About](https://www.msaied.com/public#about)

           [  Contact](https://www.msaied.com/public#contact) Menu 

Menu
----

Close 

 - [HomeStart here](https://www.msaied.com/public)
- [ProjectsCase studies](https://www.msaied.com/public/projects)
- [ArticlesEngineering notes](https://www.msaied.com/public/articles)
- [CertificatesCredentials](https://www.msaied.com/public/certificates)
- [AboutHow I work](https://www.msaied.com/public#about)
- [ContactGet in touch](https://www.msaied.com/public#contact)

  [Start a conversation](https://www.msaied.com/public#contact) [WhatsApp](https://wa.me/201094619204) [Email](mailto:hello@msaied.com) 

 1. [Home](https://www.msaied.com/public)
2. /
3. [Articles](https://www.msaied.com/public/articles)
4. /
5. Blackfire &amp; Xdebug Profiling in Laravel: Finding Real Bottlenecks

 Blackfire &amp; Xdebug Profiling in Laravel: Finding Real Bottlenecks
======================================================================

 Stop guessing where your Laravel app is slow. Learn how to use Blackfire and Xdebug profiling together to pinpoint real bottlenecks in routes, jobs, and Eloquent queries with actionable precision.

 ![](https://cdn.msaied.com/01M22N44A70A5MC2S599JP0MPH.webp) [Mohamed Said](https://www.msaied.com/public#person) Published 30 Aug 2026 · Updated 30 Aug 2026 · 3 min read

ShareCopy linkCopied

 ![Blackfire & Xdebug Profiling in Laravel: Finding Real Bottlenecks](https://cdn.msaied.com/608/26a2b1fe183034ea35445954544f68f1.png) 

  On this page +1. [Why Guessing Is Expensive](#why-guessing-is-expensive)
2. [Xdebug: Callgrind Profiles Locally](#xdebug-callgrind-profiles-locally)
3. [Reading the Call Graph](#reading-the-call-graph)
4. [Blackfire: Continuous Performance Assertions](#blackfire-continuous-performance-assertions)
5. [Profiling a Specific Artisan Command](#profiling-a-specific-artisan-command)
6. [Instrumenting Custom Code](#instrumenting-custom-code)
7. [Combining Both Tools Effectively](#combining-both-tools-effectively)
8. [Takeaways](#takeaways)

 Why Guessing Is Expensive
-------------------------

Most performance work starts with a hunch: "It must be the N+1 query" or "The cache is probably cold." Hunches waste hours. Profilers give you a call graph with wall-clock time, CPU time, memory delta, and I/O — so you fix the right thing first.

This article covers two complementary tools:

- **Xdebug** — free, always available, great for local deep-dives with a GUI like PHPStorm or KCachegrind.
- **Blackfire** — commercial, CI-friendly, built for continuous performance testing with assertions.

---

Xdebug: Callgrind Profiles Locally
----------------------------------

Install Xdebug 3 and add to `php.ini`:

```ini
[xdebug]
xdebug.mode=profile
xdebug.output_dir=/tmp/xdebug
xdebug.profiler_output_name=cachegrind.out.%p.%r

```

Trigger a profile for a single request by appending `?XDEBUG_PROFILE=1` or setting the cookie. For a CLI job:

```bash
XDEBUG_MODE=profile php artisan queue:work --once

```

Open the resulting `cachegrind.out.*` file in **KCachegrind** (Linux) or **QCachegrind** (macOS). Sort by *Self Cost* to find functions that consume time without delegating — these are your actual hot paths, not just callers.

### Reading the Call Graph

A common surprise: `PDOStatement::execute` shows up with 40 % self cost because a Blade partial triggers 60 lazy-loaded relations. The call graph makes the chain obvious:

```php
View::render
  └─ BlogPost::author()      ← repeated 50×
       └─ PDOStatement::execute

```

Fix it with `with('author')` on the controller query, re-profile, confirm the cost drops.

---

Blackfire: Continuous Performance Assertions
--------------------------------------------

Blackfire's killer feature is not the flame graph — it's **assertions in CI**. You write a `.blackfire.yaml` at the project root:

```yaml
tests:
  "Homepage loads fast":
    path: /
    assertions:
      - "main.wall_time < 200ms"
      - "metrics.sql.queries.count < 10"
      - "metrics.http.requests.count == 1"

```

Push to a branch, run `blackfire run php artisan blackfire:test` (or the GitHub Action), and the build fails if a regression sneaks in. This is performance-as-code.

### Profiling a Specific Artisan Command

```bash
blackfire run php artisan import:products --limit=100

```

Blackfire uploads the trace to its dashboard. Filter by **exclusive time** to find the single most expensive function call. A real example: `json_decode` inside a loop consuming 18 % of wall time because a 2 MB payload was decoded once per row instead of once per batch.

### Instrumenting Custom Code

For long jobs, add manual probes so Blackfire can segment the timeline:

```php
use Blackfire\Client;
use Blackfire\Profile\Configuration;

$blackfire = new Client();
$probe = $blackfire->createProbe((new Configuration())->setTitle('Batch import'));

foreach ($chunks as $chunk) {
    $this->processChunk($chunk);
}

$blackfire->endProbe($probe);

```

This gives you a named segment in the timeline rather than one undifferentiated blob.

---

Combining Both Tools Effectively
--------------------------------

| Scenario | Tool | |---|---| | Local deep-dive, no account needed | Xdebug + KCachegrind | | CI regression gate | Blackfire assertions | | Profiling a queue job in staging | Blackfire `run` | | Memory leak hunt | Xdebug memory snapshots | | Comparing two implementations | Blackfire comparison view |

A practical workflow: use Xdebug locally to understand *what* is slow, fix it, then write a Blackfire assertion so it never regresses.

---

Takeaways
---------

- **Never optimise without a profiler** — wall-clock logs lie about where time is actually spent.
- Xdebug callgrind + KCachegrind is zero-cost and reveals the full PHP call graph.
- Blackfire assertions in CI make performance a first-class build constraint.
- Sort by *exclusive/self* time, not inclusive, to find the real culprit.
- Instrument long-running jobs with Blackfire probes to segment the timeline.
- Fix one bottleneck at a time and re-profile; compound fixes mask each other's impact.

- [laravel](https://www.msaied.com/public/articles?search=laravel)
- [performance](https://www.msaied.com/public/articles?search=performance)
- [profiling](https://www.msaied.com/public/articles?search=profiling)
- [blackfire](https://www.msaied.com/public/articles?search=blackfire)
- [xdebug](https://www.msaied.com/public/articles?search=xdebug)

 Frequently asked questions 
---------------------------

  Can I use Xdebug profiling in production?Avoid it. Xdebug profiling adds significant overhead (often 2–5×) and writes large files to disk. Use it locally or in a dedicated staging environment. For production profiling, Blackfire's agent has much lower overhead and is designed for live traffic sampling.

   How do Blackfire assertions differ from just checking response time in a test?Blackfire assertions inspect the internal call graph — SQL query count, HTTP sub-requests, memory allocations, and specific function call counts — not just the final response time. This means you can catch a regression like an extra 20 queries even if the total wall time stays under your threshold due to a fast database in CI.

   Does Xdebug profiling work with Laravel Octane?Partially. Because Octane reuses worker processes, Xdebug's per-request profiler output can mix traces across requests. The safest approach is to profile with Octane disabled locally, or use Blackfire which has explicit Octane support via its agent.

   ![Mohamed Said](https://cdn.msaied.com/01M22N44A70A5MC2S599JP0MPH.webp)About the author
----------------

[Mohamed Said](https://www.msaied.com/public#person)Senior Backend Engineer specializing in Laravel, scalable SaaS platforms, APIs, and cloud infrastructure. I build secure, high-performance web applications that help businesses grow.

[About](https://www.msaied.com/public#about) [GitHub ↗](https://github.com/EG-Mohamed) [LinkedIn ↗](https://www.linkedin.com/in/msaiedm/) [WhatsApp ↗](https://wa.me/201094619204) [Email Address ↗](mailto:hello@msaied.com) [My CV ↗](https://drive.google.com/file/u/0/d/1MF20IPRJyzfy32mhEutjL5EpSls0w2Q8/view)  

   [Previous articleRecursive CTEs and Hierarchical Data in Laravel with PostgreSQL](https://www.msaied.com/public/articles/recursive-ctes-and-hierarchical-data-in-laravel-with-postgresql) [Next articleLaravel AI SDK: Tool-Calling Agents and Conversation Persistence](https://www.msaied.com/public/articles/laravel-ai-sdk-tool-calling-agents-and-conversation-persistence-3)  

   On this page
-------------

1. [Why Guessing Is Expensive](#why-guessing-is-expensive)
2. [Xdebug: Callgrind Profiles Locally](#xdebug-callgrind-profiles-locally)
3. [Reading the Call Graph](#reading-the-call-graph)
4. [Blackfire: Continuous Performance Assertions](#blackfire-continuous-performance-assertions)
5. [Profiling a Specific Artisan Command](#profiling-a-specific-artisan-command)
6. [Instrumenting Custom Code](#instrumenting-custom-code)
7. [Combining Both Tools Effectively](#combining-both-tools-effectively)
8. [Takeaways](#takeaways)

 ###  Have a technical challenge?

 Tell me what you’re building. I reply within two working days.

[Start a conversation](https://www.msaied.com/public#contact) 

   Related articles
-----------------

 [ ![](https://cdn.msaied.com/740/cce86edc21eddcbdd2f2454fadaf9c70.png)  · 3 min read### The Pipeline Pattern in Laravel: Custom Pipelines Beyond Middleware

5 Oct 2026 ](https://www.msaied.com/public/articles/the-pipeline-pattern-in-laravel-custom-pipelines-beyond-middleware-1) [ ![](https://cdn.msaied.com/739/2d6897fdcdcf090613f96f72a64b8a78.png)  · 4 min read### MySQL Full-Text Search in Laravel: Indexes, Relevance Scoring, and Boolean Mode

4 Oct 2026 ](https://www.msaied.com/public/articles/mysql-full-text-search-in-laravel-indexes-relevance-scoring-and-boolean-mode) [ ![](https://cdn.msaied.com/738/073696a3fefe18bec825beec5ac658f5.png)  · 4 min read### Laravel Queue Rate-Limited Middleware: Throttling Jobs Without Losing Work

4 Oct 2026 ](https://www.msaied.com/public/articles/laravel-queue-rate-limited-middleware-throttling-jobs-without-losing-work) 

  Have a technical challenge?
----------------------------

Tell me what you’re building. I reply within two working days.

 [Discuss your project ↗](https://www.msaied.com/public#contact) 

  © 2026 Mohamed Said · Built with Laravel, meant to last.Senior Backend Engineer specializing in Laravel, scalable SaaS platforms, APIs, and cloud infrastructure. I build secure, high-performance web applications that help businesses grow.

 - [Home](https://www.msaied.com/public)
- [Articles](https://www.msaied.com/public/articles)
- [Certificates](https://www.msaied.com/public/certificates)
- [GitHub](https://github.com/EG-Mohamed)
- [LinkedIn](https://www.linkedin.com/in/msaiedm/)
- [WhatsApp](https://wa.me/201094619204)
- [Email Address](mailto:hello@msaied.com)
- [My CV](https://drive.google.com/file/u/0/d/1MF20IPRJyzfy32mhEutjL5EpSls0w2Q8/view)
- [Sitemap](https://www.msaied.com/public/sitemap.xml)
