1. Go to this page and download the library: Download mateffy/laraperf library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
mateffy / laraperf example snippets
use function Mateffy\Laraperf\Testing\{measure, capture, is_capturing, timeline_mark};
// Measure a single operation
$result = measure(fn () => User::with('posts')->get());
// Manual start/stop with timeline marks
$cap = capture(); // starts capture
timeline_mark('before-query');
User::all();
timeline_mark('after-query');
$result = $cap->stop(); // stops and returns PerformanceResult
// Check if a capture session is active
if (is_capturing()) { ... }
// Declarative constraints via test() chain
test('dashboard does not trigger N+1 queries')
->maxQueryCount(10)
->noN1Patterns()
->maxDuration(500) // ms
->maxMemory('10M');
// Access results with perf()
test('user list is fast', function () {
$result = perf(); // PerformanceResult for this test
expect($result->queryCount())->toBeLessThan(20);
});
// Fluent expectation API
test('user query performance', function () {
$result = measure(fn () => User::with('posts')->paginate());
expect($result)
->performance()->duration()->toBeLessThan(100)
->performance()->queries()->count()->toBeLessThan(10)
->performance()->queries()->whereTable('users')->count()->toBe(1)
->performance()->n1()->toBe(0)
->performance()->toHaveNoN1()
->performance()->toHaveNoSlowQueries(50);
});
// Manual capture in tests
test('specific operation', function () {
$this->startPerformanceCapture();
// ... code under test ...
$result = $this->stopPerformanceCapture();
expect($result->n1Count())->toBe(0);
});