Weave 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

Profiling Laravel Package

draw/profiling

Laravel profiling tools for measuring performance and tracing requests. Capture timings, memory usage, and key execution steps to help identify slow endpoints and bottlenecks during development and debugging.

View on GitHub
Deep Wiki
Context7

Test Profiling with Annotations

Use @profile annotations to mark tests for profiling:

/**
 * @profile
 */
public function test_slow_endpoint()
{
    $response = $this->get('/slow-endpoint');
    $response->assertOk();
}

CI/CD Integration

Add a custom PHPUnit listener to fail builds on slow tests:

<!-- phpunit.xml -->
<listeners>
    <listener class="Draw\Profiling\Listeners\PerformanceListener" file="vendor/draw/profiling/src/Listeners/PerformanceListener.php"/>
</listeners>

Configure thresholds in phpunit.xml:

<env name="PROFILING_THRESHOLD" value="1000"/> <!-- 1000ms -->

Integration with draw/tester

Leverage draw/tester for assertions:

use Draw\Tester\Assertions;

public function test_query_performance()
{
    Assertions::assertQueryRanIn('SELECT * FROM users', '<50ms');
}

Profiling External Calls

Wrap HTTP clients or APIs:

Profiling::start('External API');
$response = Http::get('https://api.example.com/data');
Profiling::stop('External API');
Profiling::dump(); // Log latency

Batch Processing

Profile cron jobs or queue workers:

public function test_batch_job()
{
    Profiling::start('Batch Job');
    $this->artisan('queue:work', ['--once' => true]);
    Profiling::stop('Batch Job');
    Profiling::assertUnder('1000ms'); // Fail if >1s
}

Gotchas and Tips

Pitfalls

  1. Double Profiling

    • Avoid nesting Profiling::start()/stop() without unique names:
      // ❌ Overwrites previous data
      Profiling::start('section');
      Profiling::start('section'); // Duplicate!
      
  2. PHPUnit Conflicts

    • If tests fail silently, check for:
      • Custom PHPUnit extensions overriding hooks.
      • @before/@after annotations interfering with profiling.
  3. Memory Leaks

    • Profiling adds overhead. For long-running tests:
      Profiling::disable(); // Temporarily pause
      // ... heavy operation ...
      Profiling::enable();
      
  4. CI/CD False Positives

    • Flaky CI environments may skew timings. Use:
      phpunit --repeat 3 --stop-on-failure
      

Debugging

  • Missing Data? Verify the ProfilingServiceProvider is registered and no exceptions are caught:

    try {
        Profiling::start('test');
        // ...
    } catch (\Throwable $e) {
        Profiling::dump(); // Force output
        throw $e;
    }
    
  • Slow Queries Use draw/tester assertions:

    Assertions::assertQueryCount('SELECT * FROM users', 1, '<10ms');
    
  • CLI Output Issues Redirect output to a file:

    phpunit --profile > profiling.log
    

Extension Points

  1. Custom Listeners Extend PerformanceListener to add Slack alerts or database logging:

    class SlackListener extends PerformanceListener
    {
        protected function onThresholdExceeded(string $test, float $duration)
        {
            $this->notifySlack("Slow test: {$test} ({$duration}ms)");
        }
    }
    
  2. Database Backend Store results in a table:

    Profiling::storeInDatabase(); // Hypothetical method
    
  3. Visualization Export to JSON for Grafana:

    Profiling::dump('profiling.json');
    

Configuration Quirks

  • Auto-Start Enable global profiling in config/profiling.php:

    'auto_start' => true, // Starts profiling for all tests
    'threshold' => 500,   // Fail tests >500ms
    
  • Excluded Tests Skip profiling for specific tests:

    /**
     * @profile-exclude
     */
    public function test_fast_operation() { ... }
    

Pro Tips

  1. Profile in Staging Run profiling in staging before production:

    phpunit --profile --env=staging
    
  2. Compare Baselines Use Git to track profiling data:

    git add profiling.log
    git commit -m "Baseline: Optimized user queries"
    
  3. Pair with telescope Correlate slow queries with Telescope entries:

    Profiling::start('Query');
    User::where('active', true)->get();
    Profiling::stop('Query');
    // Check Telescope for the query hash
    
  4. Parallel Testing Use --parallel with caution—profiling may skew results:

    phpunit --parallel --profile
    
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.
coleus/frame
thtg88/journalism
filagora/filagora-ai-forums
rlacerda83/query-parser
socialdept/atp-orm
cboxdk/laravel-assistant-client
socialdept/atp-resolver
socialdept/tenant-domains
tautvydasr/blade-class-names
socialdept/color-scales
maherelgamil/controller
coleus/widgets
coleus/table
coleus/users
coleus/music
coleus/health
coleus/calendar
coleus/notes
coleus/support
coleus/settings