Skip to content

Instantly share code, notes, and snippets.

@blackcoper
Last active July 12, 2025 10:10
Show Gist options
  • Select an option

  • Save blackcoper/40b3ae1f98c016af72720be8fc0998bd to your computer and use it in GitHub Desktop.

Select an option

Save blackcoper/40b3ae1f98c016af72720be8fc0998bd to your computer and use it in GitHub Desktop.
Laravel Octane Benchmark

Laravel Octane Benchmark Tutorial

This guide helps you benchmark a Laravel application using three different runtime scenarios:

  1. PHP-FPM (no Octane, no pooling)
  2. Laravel Octane with Swoole (no persistent connections)
  3. Laravel Octane with Swoole and MySQL persistent connections

πŸ› οΈ Prerequisites

  • Laravel app installed
  • MySQL running locally
  • ApacheBench (ab) or wrk
  • PHP >= 8.1
  • Composer

πŸš€ Step 1: Install Laravel Octane

composer require laravel/octane
php artisan octane:install

Choose Swoole:

composer require open-swoole/swoole

Run Octane:

php artisan octane:start

πŸ”§ Step 2: Enable Persistent Connection (Scenario C)

Edit config/database.php under the mysql connection:

'options' => extension_loaded('pdo_mysql') ? [
    PDO::ATTR_PERSISTENT => true,
] : [],

Clear config:

php artisan config:clear

πŸ§ͺ Step 3: Create Benchmark Route

Edit routes/web.php:

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Route;

Route::get('/benchmark', function () {
    $start = microtime(true);

    for ($i = 0; $i < 100; $i++) {
        DB::table('users')->limit(1)->get();
    }

    return [
        'duration_ms' => (microtime(true) - $start) * 1000,
        'memory' => memory_get_usage(true),
    ];
});

πŸ§ͺ Step 4: Run Benchmarks

Use ApacheBench to run:

ab -n 1000 -c 100 http://127.0.0.1:8000/benchmark

Or use wrk:

wrk -t12 -c400 -d30s http://127.0.0.1:8000/benchmark

πŸ” Step 5: Automate Benchmark

Use this bash script benchmark.sh to automate the tests:

chmod +x benchmark.sh
./benchmark.sh

πŸ“Š Sample Result Table

Scenario Requests/sec Avg Duration (ms) Memory Usage Notes
A (FPM) ~200 ~500 High Baseline
B (Octane) ~900 ~80 Lower Octane runtime
C (Octane + Pooling) ~1200 ~50 Lowest Fastest

βœ… Tips

  • Use php artisan octane:reload after code/config changes
  • Avoid static/global state in Octane
  • Preload services using Octane::warm()

πŸ“ Files

  • benchmark.sh: Benchmark automation script
  • routes/web.php: Benchmark endpoint

πŸ™‹ Need Help?

Let me know if you want this exported as a GitHub repo or Gist!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment