aferrandini/xhprof-bundle
Laravel-friendly integration of XHProf profiling: collect and view performance data for your app, track request timings and call graphs, and store runs for comparison. Useful for spotting bottlenecks in production-like environments.
Install the Bundle
Add to composer.json:
"require": {
"aferrandini/xhprof-bundle": "dev-master"
}
Run:
composer update aferrandini/xhprof-bundle
Enable the Bundle
Register in config/bundles.php:
return [
// ...
Aferrandini\XhprofBundle\XhprofBundle::class => ['all' => true],
];
Configure XHProf Publish the default config:
php bin/console aferrandini:xhprof:install
Edit app/config/xhprof.php to set:
enabled: true (for production/staging).output_dir: Path to store .xhprof files (e.g., var/log/xhprof/).First Use Case Trigger profiling via Symfony’s profiler:
php bin/console debug:profiler
Navigate to /_profiler/ in your browser. Click the "XHProf" tab to view raw data.
Automatic Profiling
xhprof.php:
'enabled' => true,
'always_on' => true, // Profiles all requests
Manual Profiling
$profiler = $this->get('xhprof.profiler');
$profiler->start('my_custom_section');
// ... code to profile ...
$profiler->stop();
Conditional Profiling
if ($this->get('kernel')->getEnvironment() === 'staging') {
$profiler->start('staging_request');
}
Integration with Symfony Events
# config/services.yaml
services:
App\EventListener\XhprofListener:
tags:
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
// src/EventListener/XhprofListener.php
public function onKernelRequest(GetResponseEvent $event) {
$profiler = $this->container->get('xhprof.profiler');
$profiler->start('request_' . uniqid());
}
Output Formats
.xhprof files (default) stored in output_dir.xhprof_html xhprof.out.12345 > profile.html
Cleanup
php bin/console aferrandini:xhprof:cleanup --days=7
Performance Overhead
'enabled' => false,
always_on: false to avoid unintended profiling.File Permissions
output_dir is writable:
chmod -R 775 var/log/xhprof/
php bin/console debug:config aferrandini_xhprof
Memory Limits
memory_limit. Increase if needed:
'memory_limit' => '256M', // in xhprof.php
Symfony 4+ Compatibility
config/bundles.php.xhprof.profiler service isn’t found.Missing Profiler Tab
/_profiler/ XHProf tab doesn’t appear:
php bin/console cache:clear
xhprof extension is loaded in PHP:
php -m | grep xhprof
$profiler = $this->get('xhprof.profiler');
var_dump($profiler->isEnabled()); // bool
xhprof.php:
'debug' => true,
Check var/log/dev.log for issues.Custom Data Collection
$profiler->addCustomData('user_id', $user->id);
Post-Processing Hooks
XhprofBundle\Service\Profiler service to modify output:
# config/services.yaml
services:
App\Service\CustomProfiler:
decorates: 'xhprof.profiler'
arguments: ['@App\Service\CustomProfiler.inner']
Alternative Output
// src/DependencyInjection/XhprofExtension.php
$container->set('xhprof.writer', new CustomWriter());
How can I help you explore Laravel packages today?