dragon-code/benchmark
Lightweight PHP benchmarking helper to compare execution speed of multiple callbacks. Run named tests, repeat iterations, trim outliers for cleaner averages, and print results (min/max/avg/total + memory) to the console. Includes bench() helper and Benchmark class.
--dev), avoiding runtime overhead.phpunit, telescope, or custom CLI tools).Benchmark as a singleton for global access) or artisan commands (e.g., php artisan benchmark:compare).toAssert() for regression testing in CI pipelines.bench()->compare(Query::where(...), DB::select(...))).warmup(3) may need adjustment for Laravel’s heavy autoloading.afterEach..benchmarks/ snapshots must be version-controlled to avoid flaky CI failures. Risk: merge conflicts if multiple devs modify benchmarks.toBeRegressionTime)?max: 10% in regressions) are acceptable for your team?.benchmarks/) in version control?pcte\parallel-laravel) to reduce CI time?php artisan benchmark:run) to centralize usage.// app/Console/Commands/BenchmarkCommand.php
use DragonCode\Benchmark\Benchmark;
class BenchmarkCommand extends Command {
public function handle() {
$results = Benchmark::make()
->iterations(100)
->compare(
'Eloquent': fn () => User::all()->count(),
'Query Builder': fn () => DB::table('users')->count(),
)
->toData();
$this->line(json_encode($results));
}
}
toAssert() in PHPUnit tests for performance gates:
public function test_user_count_performance() {
Benchmark::make()
->compare(
'Eloquent': fn () => User::all()->count(),
)
->toAssert()
->toBeAvgTime(from: 10, till: 50); // 10–50ms
}
- name: Run benchmarks
run: php artisan benchmark:run | tee benchmark-results.json
- name: Check regressions
run: php artisan benchmark:assert --max-time=15
bench()->compare(...)->toConsole()) in local dev.toAssert() to existing PHPUnit tests for high-risk components.toBeRegressionTime(max: 10))..benchmarks/ in .gitignore except for snapshots).composer require --dev dragon-code/benchmark).warmup() or iterations().afterEach.disableProgressBar() for cleaner logs.toData() output for auditing.bench() for quick comparisons.Benchmark::make() for reusable configurations.--dev).pcte/parallel-laravel) to reduce CI time.Parallel::run([
fn () => Benchmark::make()->compare('Path A')->toData(),
fn () => Benchmark::make()->compare('Path B')->toData(),
]);
| Failure Mode | Impact | Mitigation |
|---|---|---|
| False Positives | CI fails due to environment noise. | Use warmup(), exclude outliers, document baseline env. |
| Snapshot Drift | Snapshots become outdated. | Update snapshots via PR process; version-control them. |
| Stateful Callbacks | Memory |
How can I help you explore Laravel packages today?