testo/bench
Benchmark plugin for Testo. Runs benchmark methods repeatedly, records per-iteration timings, and reports descriptive statistics plus warnings for variance, outliers, and noisy environments—ideal for tracking performance alongside correctness tests.
composer require --dev testo/testo testo/bench
Testo\Bench\BenchmarkTestCase:
use Testo\Bench\BenchmarkTestCase;
use Illuminate\Support\Facades\Http;
class ApiPerformanceTest extends BenchmarkTestCase
{
public function test_api_response_time()
{
$response = Http::get('https://api.example.com/data');
$this->assertLessThan(500, $response->time());
}
}
vendor/bin/testo --filter="*PerformanceTest"
Benchmark a critical API endpoint to detect performance regressions:
public function test_checkout_endpoint()
{
$response = Http::post('/checkout', ['item' => 'test']);
$this->assertLessThan(1000, $response->time()); // Max 1s response time
}
Run with:
vendor/bin/testo --filter="test_checkout_endpoint"
.github/workflows/tests.yml:
- name: Run Performance Benchmarks
run: vendor/bin/testo --filter="*Benchmark" --min-iterations=10
benchmarks/ directory:
- name: Upload Benchmark Results
uses: actions/upload-artifact@v3
with:
name: benchmark-results
path: storage/benchmarks/
use Illuminate\Support\Facades\DB;
public function test_query_execution()
{
$query = DB::connection()->getPdo()->query("SELECT * FROM large_table");
$this->assertLessThan(200, $query->fetchAll());
}
use Illuminate\Support\Facades\Queue;
public function test_job_processing_time()
{
Queue::fake();
$job = new ProcessOrderJob($order);
$this->assertLessThan(5000, $job->handle()); // Max 5s processing
}
Use Testo for benchmarks and PHPUnit for correctness:
# Run all PHPUnit tests
phpunit
# Run only benchmarks
vendor/bin/testo --filter="*Benchmark"
High Variance in CI:
--min-iterations (e.g., 10) or use --warmup-iterations=5 to stabilize the environment.Outlier Sensitivity:
protected function benchConfig(): array
{
return [
'ignore_outliers' => true,
'outlier_threshold' => 2.0, // 2 standard deviations
];
}
Testo Dependency Risk:
composer require --dev phpbench/phpbench
vendor/bin/testo --verbose
public function test_environment_stability()
{
$this->bench(fn() => sleep(0.1))->assertVarianceLessThan(0.05);
}
Sample Size:
protected function benchConfig(): array
{
return ['iterations' => 50];
}
Confidence Intervals:
$this->assertTimeLessThan(1000, 0.95); // 95% confidence
Custom Assertions:
use Testo\Bench\Assertions;
Assertions::assertVarianceLessThan($benchmark, 0.1);
Pre-Benchmark Setup:
protected function setUp(): void
{
$this->artisan('cache:clear'); // Ensure consistent state
}
Post-Benchmark Teardown:
protected function tearDown(): void
{
$this->artisan('queue:flush'); // Clean up jobs
}
Mock External Services:
public function test_api_with_mock()
{
Http::fake([
'api.example.com' => Http::response('data', 200, ['X-Process-Time' => 100]),
]);
$this->assertTimeLessThan(200);
}
Benchmark Eloquent Queries:
public function test_eloquent_query()
{
$user = User::query()->where('id', 1)->first();
$this->assertLessThan(50, $user->getQueryLog()[0]['time']);
}
How can I help you explore Laravel packages today?