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

Opcache Profiler Bundle Laravel Package

codepoet/opcache-profiler-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Install via Composer:

    composer require codepoet/opcache-profiler-bundle:^1.0
    

    (Note: For Symfony 2.0.x, use the gist workaround.)

  2. Enable the Bundle: Add to config/bundles.php (Symfony 4+) or AppKernel.php (Symfony 2/3):

    new Codepoet\OpcacheProfilerBundle\CodepoetOpcacheProfilerBundle(),
    
  3. First Use Case: Access any Symfony profiler page (e.g., /_profiler/). The Opcache tab will appear in the toolbar, showing:

    • Cache status (enabled/disabled)
    • Memory usage
    • Hit/miss ratios
    • Cached script count

Implementation Patterns

Core Workflows

  1. Debugging Performance Bottlenecks:

    • Use the hit/miss ratio to identify uncached scripts (high misses = Opcache inefficiency).
    • Correlate with Symfony profiler timings to spot slow scripts that should be cached.
  2. Monitoring During Deployment:

    • After deploying, check the filelist (if enabled) to verify critical classes are cached.
    • Watch for memory spikes post-deploy (may indicate cache invalidation issues).
  3. Environment-Specific Config:

    # config/packages/dev/codepoet_opcache_profiler.yaml
    codepoet_opcache_profiler:
        data_collector:
            show_filelist: true  # Only enable in dev/staging
            cache_stats: true    # Show detailed cache stats
    
  4. Integration with CI/CD:

    • Add a pre-deploy check (e.g., via PHPUnit) to fail if Opcache hit ratio < 90% in staging.

Advanced Patterns

  • Custom Metrics: Extend the OpcacheDataCollector to log cache events to a monitoring system (e.g., Datadog):

    // src/EventListener/OpcacheListener.php
    use Codepoet\OpcacheProfilerBundle\DataCollector\OpcacheDataCollector;
    
    public function onKernelRequest(GetResponseEvent $event) {
        $opcache = $event->getKernel()->getContainer()
            ->get('codepoet_opcache_profiler.data_collector');
        if ($opcache->getMissCount() > 10) {
            // Trigger alert
        }
    }
    
  • Dynamic Filelist Filtering: Override the getFileList() method to exclude vendor/ or cache/ directories:

    // config/services.yaml
    Codepoet\OpcacheProfilerBundle\DataCollector\OpcacheDataCollector:
        arguments:
            $showFilelist: true
            $filelistFilter: '~^(?!.*(vendor|cache))~'
    

Gotchas and Tips

Pitfalls

  1. Memory Overhead:

    • show_filelist: true can consume hundreds of MB for large apps. Enable only in dev/staging.
    • Fix: Use opcache.get_status() directly in CLI scripts for lightweight checks.
  2. Symfony 4+ Compatibility:

    • The bundle targets Symfony 2/3. For Symfony 4+, ensure config/bundles.php is used instead of AppKernel.
    • Fix: Check the Symfony 4 migration guide for bundle loading.
  3. Opcache Disabled:

    • The toolbar may show no data if Opcache is off. Verify with:
      php -i | grep opcache.enable
      
    • Fix: Enable Opcache in php.ini (opcache.enable=1) and restart PHP-FPM.
  4. Filelist Inconsistencies:

    • The filelist may lag behind real-time changes (e.g., after opcache_reset()).
    • Tip: Refresh the profiler tab or run opcache_reset() in CLI to sync.

Debugging Tips

  • CLI Debugging: Use opcache_get_status() in a script to bypass the toolbar:

    $status = opcache_get_status();
    var_dump($status['opcache_statistics']);
    
  • Log Cache Events: Add this to config/packages/monolog.yaml to log Opcache misses:

    handlers:
        opcache:
            type: stream
            path: "%kernel.logs_dir%/opcache.log"
            level: debug
            channels: ["opcache"]
    

    Then log misses in a subscriber:

    public function onKernelRequest(RequestEvent $event) {
        $misses = opcache_get_status()['opcache']['miss_count'];
        if ($misses > 0) {
            $this->logger->debug('Opcache misses', ['misses' => $misses]);
        }
    }
    

Extension Points

  1. Custom Data Collection: Extend OpcacheDataCollector to add metrics like:

    • Script execution time (from Symfony profiler).
    • File modification timestamps (to detect stale cache).
  2. Toolbar Integration: Override the Twig template (templates/CodepoetOpcacheProfilerBundle:Toolbar:opcache.html.twig) to:

    • Add visual warnings for high miss rates.
    • Include a "Clear Opcache" button (calls opcache_reset()).
  3. Asynchronous Monitoring: Use a cron job to log Opcache stats to a database:

    */5 * * * * php /path/to/script.php opcache:stats >> /var/log/opcache.log
    

    Script example:

    // script/opcache:stats.php
    $stats = opcache_get_status();
    file_put_contents(
        '/var/log/opcache.json',
        json_encode($stats['opcache_statistics'])
    );
    
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.
datacore/hub-sdk
alengo/sulu-http-cache-bundle
croct/coding-standard
croct/plug-php
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
imbo/imbo-coding-standard
visualbuilder/filament-lottie
servicioslineaonce/starter-kit
atomcoder/laravel-reorderable
irajul/filament-shadcn-theme
agtp/agtp-php
agtp/mod-php
centraldesktop/protobuf-php
trappistes/laravel-custom-fields