laracraft-tech/laravel-xhprof
Laravel package integrating XHProf profiling into your app. Capture and review performance data for requests, analyze bottlenecks, and compare runs to optimize code, queries, and overall response time with minimal setup and overhead.
composer require laracraft-tech/laravel-xhprof
php artisan vendor:publish --tag=xhprof-config
php artisan vendor:publish --tag=xhprof-migrations
php artisan migrate
extension=xhprof.so in your php.ini.XHPROF_SAMPLE_RATE (e.g., XHPROF_SAMPLE_RATE=100 for full profiling or 10 for sampling).?profile=1 to any Laravel route (e.g., http://your-app.test/api/orders?profile=1)./xhprof (default).php artisan xhprof:view
Wrap command logic with the facade:
use LaracraftTech\Xhprof\Facades\Xhprof;
public function handle()
{
Xhprof::start('cli-job');
// Your logic here...
$profile = Xhprof::stop();
// Save or log the profile
}
/api/orders).?profile=1 to the URL.app/Http/Kernel.php:
protected $middlewareGroups = [
'web' => [
\LaracraftTech\Xhprof\Http\Middleware\Profile::class,
// Other middleware...
],
];
/xhprof for the latest profile.use LaracraftTech\Xhprof\Facades\Xhprof;
use Tests\TestCase;
class PerformanceTest extends TestCase
{
public function test_api_response_time()
{
Xhprof::start('api-performance-test');
$response = $this->get('/api/orders');
$profile = Xhprof::stop();
$this->assertLessThan(500, $profile->getWallTime(), "API response time exceeds 500ms");
}
}
php artisan test --filter PerformanceTest
use LaracraftTech\Xhprof\Facades\Xhprof;
class ProcessOrders implements ShouldQueue
{
public function handle()
{
Xhprof::start('process-orders-job');
// Job logic...
$profile = Xhprof::stop();
file_put_contents(storage_path('logs/xhprof-job.log'), print_r($profile, true));
}
}
php artisan queue:work
storage/logs/xhprof-job.log or the database.\LaracraftTech\Xhprof\Http\Middleware\Profile early in the middleware stack.local/staging in config/xhprof.php:
'enabled' => env('APP_ENV') !== 'production',
skip_urls:
'skip_urls' => [
'health-check',
'admin/*',
],
'storage' => 'file',
Storage interface for S3 or other backends.XHProf Extension Missing:
xhprof_enable() errors.XHPROF_SAMPLE_RATE in php.ini.php -m | grep xhprof.Database Storage Bloat:
xhprof_runs table.php artisan xhprof:prune --days=30
Or switch to file storage.Middleware Order Issues:
\LaracraftTech\Xhprof\Http\Middleware\Profile to the top of the middleware stack.Case Sensitivity in Skip Rules:
/Health are not skipped.Blob Data Truncation (MySQL):
xhprof_runs uses LONGTEXT for the data column (handled in v1.0.10+ migrations).CLI Profiling Overhead:
XHPROF_SAMPLE_RATE=10) or profile selectively.php artisan xhprof:view to inspect the latest profile.$profile = Xhprof::stop();
file_put_contents(storage_path('logs/xhprof-' . now()->timestamp . '.log'), print_r($profile, true));
git bisect with profiling to identify regressions.Custom Storage Backend:
LaracraftTech\Xhprof\Contracts\Storage interface and bind it in a service provider.Custom Middleware:
\LaracraftTech\Xhprof\Http\Middleware\Profile to add logic (e.g., conditional profiling).Visualization:
Automated Alerts:
xhprof.saved event to trigger alerts for performance thresholds:
\LaracraftTech\Xhprof\Events\ProfileSaved::class => function ($event) {
if ($event->profile->getWallTime() > 1000) {
// Send alert (e.g., Slack, PagerDuty)
}
}
How can I help you explore Laravel packages today?