This guide helps you benchmark a Laravel application using three different runtime scenarios:
- PHP-FPM (no Octane, no pooling)
- Laravel Octane with Swoole (no persistent connections)
- Laravel Octane with Swoole and MySQL persistent connections
- Laravel app installed
- MySQL running locally
- ApacheBench (
ab) orwrk - PHP >= 8.1
- Composer
composer require laravel/octane
php artisan octane:installChoose Swoole:
composer require open-swoole/swooleRun Octane:
php artisan octane:startEdit config/database.php under the mysql connection:
'options' => extension_loaded('pdo_mysql') ? [
PDO::ATTR_PERSISTENT => true,
] : [],Clear config:
php artisan config:clearEdit 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),
];
});Use ApacheBench to run:
ab -n 1000 -c 100 http://127.0.0.1:8000/benchmarkOr use wrk:
wrk -t12 -c400 -d30s http://127.0.0.1:8000/benchmarkUse this bash script benchmark.sh to automate the tests:
chmod +x benchmark.sh
./benchmark.sh| 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 |
- Use
php artisan octane:reloadafter code/config changes - Avoid static/global state in Octane
- Preload services using
Octane::warm()
benchmark.sh: Benchmark automation scriptroutes/web.php: Benchmark endpoint
Let me know if you want this exported as a GitHub repo or Gist!