Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Xhprof Laravel Package

laracraft-tech/laravel-xhprof

Integrate XHProf profiling into your Laravel app to measure request performance, capture CPU/memory metrics, and analyze call graphs. Includes middleware/collectors to enable profiling per route or environment and store runs for later review.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Laravel Ecosystem Alignment: The package is designed as a first-class Laravel citizen, leveraging service providers, middleware, and configuration publishing. It integrates seamlessly with Laravel’s request lifecycle (via middleware) and supports Laravel’s event system for extensibility (e.g., custom storage backends). This reduces architectural friction and ensures compatibility with Laravel’s evolving features (e.g., 13.x support).
  • Modular Profiling: Decouples profiling logic from storage and visualization, allowing TPMs to swap implementations (e.g., database vs. file storage) without modifying core functionality. The use of interfaces (e.g., Storage) enables future-proofing for custom integrations (e.g., S3, Elasticsearch).
  • Performance Trade-offs: XHProf introduces ~5–10% overhead, which is acceptable for development/staging but requires environment-based gating (e.g., disable in production). The package mitigates this with:
    • Configurable XHPROF_SAMPLE_RATE (e.g., sample every 100µs).
    • URL-based exclusion rules (skip_urls) to avoid profiling non-critical routes.
    • Middleware-based activation (e.g., only enable for local or admin users).

Integration Feasibility

  • Minimal Setup: Installation requires three commands (composer require, vendor:publish, migrate) and minimal configuration, reducing onboarding time. The package handles:
    • Extension validation (xhprof availability check).
    • Storage initialization (database migrations or file system setup).
    • UI routing (e.g., /xhprof for visualization).
  • Middleware-Driven: Profiles requests via Laravel’s middleware stack, enabling granular control over profiling scope. Example use cases:
    • Profile all requests in local environment.
    • Exclude /health or /api/webhooks via skip_urls.
    • Route-specific profiling (e.g., only /api/orders).
  • CLI and Background Task Support: Supports profiling of Artisan commands and queues via a facade (Xhprof::start()/stop()), but requires manual instrumentation. This is feasible for critical jobs (e.g., php artisan queue:work --profile) but may not scale for all background tasks.
  • Storage Flexibility: Defaults to database storage (with xhprof_runs table) but supports file storage. Later versions address blob size limitations (e.g., LONGTEXT for MySQL). Risk: Schema migrations may be needed if upgrading from older versions.

Technical Risk

  • Extension Dependency: The xhprof PHP extension must be installed and enabled in all target environments. Mitigation:
    • Document extension requirements in README/CONTRIBUTING.md.
    • Provide Dockerfile snippets or CI/CD templates for extension setup.
    • Offer a fallback (e.g., log a warning if xhprof is missing).
  • Storage Scalability: Database storage can bloat with high profiling volume. Mitigation:
    • Use file storage for high-volume apps (e.g., storage/xhprof).
    • Implement pruning (e.g., php artisan xhprof:prune --days=7).
    • Extend storage to S3 or other backends via the Storage interface.
  • Middleware Ordering: Profiling middleware must run early in the stack to capture full request time. Mitigation:
    • Document required middleware position in app/Http/Kernel.php.
    • Provide a helper method (e.g., XhprofMiddleware::shouldRunEarly()).
  • Case Sensitivity in Skip Rules: skip_urls uses literal substring matching, which may miss routes due to case sensitivity. Mitigation:
    • Use exact substrings or regex (if extended via custom middleware).
    • Document this limitation in the config example.
  • CLI Profiling Gaps: Manual instrumentation is required for CLI jobs/queues, which may be overlooked. Mitigation:
    • Add a Xhprof::profile() helper for common use cases (e.g., queues).
    • Provide examples in the docs for Artisan commands and queue workers.

Key Questions for TPM

  1. Environment Compatibility:
    • Is the xhprof extension available in all CI/CD environments (e.g., GitHub Actions, self-hosted runners)? If not, how will you enforce extension requirements?
    • For shared hosting or restricted environments, will you use a fallback (e.g., Tideways, Blackfire) or document limitations?
  2. Storage Strategy:
    • Will you use database or file storage? For teams with high profiling volume, file storage may be preferable but requires manual cleanup.
    • Do you need custom storage backends (e.g., S3, Elasticsearch)? If so, will you extend the package or use a different tool?
  3. Profiling Scope:
    • Should profiling be global, route-specific, or environment-gated? Example: Enable only in local or for admin users (?profile=1).
    • Do you need to profile CLI jobs, queues, or background tasks? If yes, how will you ensure consistent instrumentation (e.g., wrapper traits, decorators)?
  4. Performance Overhead:
    • What is the acceptable profiling overhead for your team? For example, 10% overhead may be tolerable in staging but not in CI.
    • Will you disable profiling in production or use it for selective monitoring (e.g., error paths)?
  5. Visualization Needs:
    • Does the default UI meet your needs, or will you integrate with third-party tools (e.g., QCacheGrind, custom dashboards)?
    • Do you need historical trend analysis or branch comparison features? If so, will you extend the package or use a different solution?
  6. Maintenance and Support:
    • Who will monitor storage growth and manage pruning? Will this be automated (e.g., cron job) or manual?
    • How will you handle schema migrations if upgrading from older versions? Will you test this in a staging environment?
  7. Security:
    • Should the profiling endpoint (/xhprof) be authenticated or rate-limited? The package does not include this by default.
    • Are there sensitive routes that should never be profiled? If so, how will you enforce this (e.g., middleware, config)?

Integration Approach

Stack Fit

  • Laravel 10–13.x: The package is actively maintained for Laravel 10–13.x and follows Laravel’s conventions (e.g., service providers, middleware, migrations). It leverages Laravel’s:
    • Service Container: For dependency injection (e.g., storage backends).
    • Middleware: For request profiling.
    • Configuration: For runtime settings (e.g., skip_urls, sample rate).
    • Migrations: For database storage.
  • PHP Extensions: Requires xhprof (and optionally xhguards for function-level profiling). Compatibility:
    • PHP 8.0+: Tested with PHP 8.0–8.3 (Laravel 10–13.x).
    • XHProf 0.9.5+: Later versions support LONGTEXT for MySQL.
  • Storage Backends: Supports:
    • Database: MySQL, PostgreSQL, SQLite (via xhprof_runs table).
    • File: Local filesystem (configurable path).
    • Custom: Extendable via Storage interface (e.g., S3, Elasticsearch).

Migration Path

  1. Pre-Integration Checklist:
    • Verify xhprof extension is available (php -m | grep xhprof).
    • Ensure PHP version meets Laravel requirements (e.g., 8.0+ for Laravel 10+).
    • Review storage strategy (database vs. file) and allocate resources (e.g., DB space, filesystem quotas).
  2. Installation:
    composer require laracraft-tech/laravel-xhprof
    php artisan vendor:publish --tag=xhprof-config
    php artisan migrate
    
  3. Configuration:
    • Update .env (e.g., XHPROF_STORAGE=file).
    • Configure config/xhprof.php:
      'skip_urls' => [
          'health', // Exclude /health
          'webhooks', // Exclude /api/webhooks
      ],
      'sample_rate' => 100000, // 100µs sample rate
      'storage' => [
          'driver' => 'file',
          'path' => storage_path('xhprof'),
      ],
      
  4. Middleware Registration:
    • Add to app/Http/Kernel.php (ensure it runs early):
      protected $middleware = [
          \LaracraftTech\Xhprof\Middleware\ProfileRequests::class,
          // ...
      ];
      
  5. CLI/Queue Instrumentation (Optional):
    • Wrap critical jobs in `Xhprof::start
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport