phperf/phpunit
phperf/phpunit is a lightweight PHPUnit extension aimed at profiling and performance measurement during test runs. Add simple hooks to capture timing and resource usage so you can spot slow tests and track performance regressions over time.
composer require --dev phperf/phpunitphpunit.xml, ensure <php> contains a PERF_ENABLED environment variable (e.g., <env name="PERF_ENABLED" value="1" force="false"/>) to activate performance assertions (disabled by default in CI for consistency).use Perf\PHPUnit\Assertions;
public function testProcessBatchDoesNotExceedTimeLimit()
{
$this->assertTimeLimit(0.1, function () {
$this->service->processBatch(1000);
});
}
This checks that the closure runs within 100ms—failures surface in PHPUnit’s output like any other assertion.PERF_ENABLED=1 in a nightly job) to avoid slowing down routine PR checks.assertTimeLimit($seconds, callable $test, string $message = '')assertExecutionTime($callable, callable $callback) to capture and inspect raw timing data (e.g., store for trend analysis).@performance and run separately via --group=performance.assertTimeLimit() + assertLessThan() to allow minor variance:
$time = $this->measureExecutionTime(fn() => $this->cache->warm());
$this->assertLessThan(0.25, $time, 'Cache warmup must stay under 250ms');
assertTimeLimit() only if test passes ≥5 times in a row with low variance; otherwise use assertExecutionTime() and validate ranges later.PERF_ENABLED=1. This avoids false CI failures on shared runners—remember to explicitly enable in your pipeline config.Perf\PHPUnit\TestCase to add project-specific assertion helpers (e.g., assertDbQueryCount($max)).PERF_VERBOSE=1 to log timing details to stderr when assertions fail—helps identify hotspots without modifying tests.How can I help you explore Laravel packages today?