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.
Use @profile annotations to mark tests for profiling:
/**
* @profile
*/
public function test_slow_endpoint()
{
$response = $this->get('/slow-endpoint');
$response->assertOk();
}
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 -->
draw/testerLeverage draw/tester for assertions:
use Draw\Tester\Assertions;
public function test_query_performance()
{
Assertions::assertQueryRanIn('SELECT * FROM users', '<50ms');
}
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
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
}
Double Profiling
Profiling::start()/stop() without unique names:
// ❌ Overwrites previous data
Profiling::start('section');
Profiling::start('section'); // Duplicate!
PHPUnit Conflicts
@before/@after annotations interfering with profiling.Memory Leaks
Profiling::disable(); // Temporarily pause
// ... heavy operation ...
Profiling::enable();
CI/CD False Positives
phpunit --repeat 3 --stop-on-failure
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
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)");
}
}
Database Backend Store results in a table:
Profiling::storeInDatabase(); // Hypothetical method
Visualization Export to JSON for Grafana:
Profiling::dump('profiling.json');
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() { ... }
Profile in Staging Run profiling in staging before production:
phpunit --profile --env=staging
Compare Baselines Use Git to track profiling data:
git add profiling.log
git commit -m "Baseline: Optimized user queries"
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
Parallel Testing
Use --parallel with caution—profiling may skew results:
phpunit --parallel --profile
How can I help you explore Laravel packages today?