Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Bench Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Install the package in your Laravel project:
    composer require --dev testo/testo testo/bench
    
  2. Create a benchmark test class by extending 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());
        }
    }
    
  3. Run benchmarks via Testo CLI:
    vendor/bin/testo --filter="*PerformanceTest"
    

First Use Case: Laravel API Endpoint

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"

Implementation Patterns

Workflow: CI/CD Integration

  1. Add benchmark stage to .github/workflows/tests.yml:
    - name: Run Performance Benchmarks
      run: vendor/bin/testo --filter="*Benchmark" --min-iterations=10
    
  2. Store results in GitHub Actions artifacts or a dedicated benchmarks/ directory:
    - name: Upload Benchmark Results
      uses: actions/upload-artifact@v3
      with:
        name: benchmark-results
        path: storage/benchmarks/
    

Pattern: Database Query Benchmarking

use Illuminate\Support\Facades\DB;

public function test_query_execution()
{
    $query = DB::connection()->getPdo()->query("SELECT * FROM large_table");
    $this->assertLessThan(200, $query->fetchAll());
}

Pattern: Laravel Queue Worker

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
}

Integration Tip: Hybrid Testo/PHPUnit

Use Testo for benchmarks and PHPUnit for correctness:

# Run all PHPUnit tests
phpunit

# Run only benchmarks
vendor/bin/testo --filter="*Benchmark"

Gotchas and Tips

Pitfalls

  1. High Variance in CI:

    • Symptom: Benchmarks fail intermittently in CI due to shared resources.
    • Fix: Increase --min-iterations (e.g., 10) or use --warmup-iterations=5 to stabilize the environment.
  2. Outlier Sensitivity:

    • Symptom: False failures due to one slow iteration.
    • Fix: Configure Testo to ignore outliers:
      protected function benchConfig(): array
      {
          return [
              'ignore_outliers' => true,
              'outlier_threshold' => 2.0, // 2 standard deviations
          ];
      }
      
  3. Testo Dependency Risk:

    • Symptom: Testo’s niche status may limit long-term support.
    • Fix: Document fallback to PHPBench if Testo is deprecated:
      composer require --dev phpbench/phpbench
      

Debugging

  • View raw timings:
    vendor/bin/testo --verbose
    
  • Inspect environment impact:
    public function test_environment_stability()
    {
        $this->bench(fn() => sleep(0.1))->assertVarianceLessThan(0.05);
    }
    

Configuration Quirks

  1. Sample Size:

    • Default is 10 iterations. Increase for stable results:
      protected function benchConfig(): array
      {
          return ['iterations' => 50];
      }
      
  2. Confidence Intervals:

    • Adjust for stricter/looser thresholds:
      $this->assertTimeLessThan(1000, 0.95); // 95% confidence
      

Extension Points

  1. Custom Assertions:

    use Testo\Bench\Assertions;
    
    Assertions::assertVarianceLessThan($benchmark, 0.1);
    
  2. Pre-Benchmark Setup:

    protected function setUp(): void
    {
        $this->artisan('cache:clear'); // Ensure consistent state
    }
    
  3. Post-Benchmark Teardown:

    protected function tearDown(): void
    {
        $this->artisan('queue:flush'); // Clean up jobs
    }
    

Laravel-Specific Tips

  • 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']);
    }
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
croct/coding-standard
croct/plug-php
nqxcode/phpmorphy
boundwize/pyrameter
testo/facade
develia/commons
dmstr/symfony-system-resources-bundle
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
renatomarinho/laravel-page-speed
develia/geo-bundle
austinheap/laravel-database-encryption
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
imbo/imbo-coding-standard
visualbuilder/filament-lottie
servicioslineaonce/starter-kit
atomcoder/laravel-reorderable
irajul/filament-shadcn-theme