为什么 PHP-FPM 性能优于 Octane?

pil*_*eup 5 laravel laravel-octane laravel-9

我设置了一个干净的 Laravel 9 项目。然后我使用 RoadRunner 设置 Octane。

我在 Windows 11 主机中的 VirtualBox VM 上运行它。

我的电脑:

  • CPU:锐龙5 3600

  • 内存:32GB - 2x16GB DDR4 3200Mhz CL16

  • 存储:三星 970 Evo(未加),500GB

虚拟机:

  • CPU:4核

  • 内存:4GB

  • 存储:固定10GB

我使用 nginx 测试并比较了 PHP-FPM 和 Octane 之间的性能wrk:https: //github.com/wg/wrk

在 Laravel 的默认主页上运行基准测试

这些是每个设置的 nginx 配置文件:

  1. 辛烷值设置:
map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}

server {
    listen 80;
    listen [::]:80;
    server_name myapp.dev;
    server_tokens off;
    root /var/www/html/myapp/public;

    index index.php;

    charset utf-8;

    location /index.php {
        try_files /not_exists @octane;
    }

    location / {
        try_files $uri $uri/ @octane;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    access_log off;
    error_log  /var/log/nginx/myapp.dev-error.log error;

    error_page 404 /index.php;

    location @octane {
        set $suffix "";

        if ($uri = /index.php) {
            set $suffix ?$query_string;
        }

        proxy_http_version 1.1;
        proxy_set_header Host $http_host;
        proxy_set_header Scheme $scheme;
        proxy_set_header SERVER_PORT $server_port;
        proxy_set_header REMOTE_ADDR $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;

        proxy_pass http://127.0.0.1:8000$suffix;
    }
}
Run Code Online (Sandbox Code Playgroud)
  1. PHP-FPM 设置:
server {
    listen 80;
    server_name myapp.dev;
    root /var/www/html/myapp/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.html index.htm index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}
Run Code Online (Sandbox Code Playgroud)

使用 PHP-FPM 时的平均请求/秒高于使用 Octane 时的平均请求/秒。这就是我跑的方式wrk

wrk -t4 -c400 -d30s http://127.0.0.1:8080/index.html
Run Code Online (Sandbox Code Playgroud)

我还与 4 名工人一起测试了辛烷值:

php artisan octane:start --workers=4
Run Code Online (Sandbox Code Playgroud)

PHP-FPM 的平均值约为 480 请求/秒,Octane 的平均值约为 400 请求/秒

基准示例:

  1. PHP-FPM:
user@server:~$ wrk -t4 -c400 -d30s http://127.0.0.1/
Running 30s test @ http://127.0.0.1/
  4 threads and 400 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   723.95ms   83.16ms   1.04s    81.75%
    Req/Sec   143.19     99.21   525.00     67.24%
  16381 requests in 30.10s, 293.92MB read
Requests/sec:    544.13
Transfer/sec:      9.76MB
Run Code Online (Sandbox Code Playgroud)
  1. 辛烷值:
user@server:~$ wrk -t4 -c400 -d30s http://127.0.0.1/
Running 30s test @ http://127.0.0.1/
  4 threads and 400 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   887.53ms  299.89ms   1.53s    61.66%
    Req/Sec   138.80    134.46   782.00     86.98%
  13340 requests in 30.04s, 238.36MB read
Requests/sec:    444.09
Transfer/sec:      7.94MB
Run Code Online (Sandbox Code Playgroud)
  1. 使用 Laravel 内置开发服务器进行测试运行:
user@server:~$ wrk -t4 -c400 -d30s http://127.0.0.1:8080/
Running 30s test @ http://127.0.0.1:8080/
  4 threads and 400 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   767.54ms  626.07ms   1.78s    79.41%
    Req/Sec    44.60     45.79   257.00     87.16%
  2075 requests in 30.04s, 37.04MB read
  Socket errors: connect 0, read 2075, write 0, timeout 1973
Requests/sec:     69.08
Transfer/sec:      1.23M
Run Code Online (Sandbox Code Playgroud)

为什么?

小智 0

确保您在 PHP 配置中为 cli 启用了 opcache

opcache.enable_cli=1

我猜 php-fpm 正在使用 opcache,但 Roadrunner 没有。