为什么PHP-FPM的性能比辛烷好



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

我在Windows 11主机的VirtualBox虚拟机上运行它。

我的电脑:

  • CPU:Ryzen 5 3600

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

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

虚拟机:

  • CPU:4核

  • RAM:4GB

  • 存储:10GB固定

我使用wrk测试并比较了PHP-FPM和辛烷与nginx之间的性能: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;
}
}
  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;
}
}

使用PHP-FPM时的平均req/sec高于使用辛烷时。这就是我运行wrk:的方式

wrk -t4 -c400 -d30s http://127.0.0.1:8080/index.html

我还和4名工人一起跑步:

php artisan octane:start --workers=4

PHP-FPM的平均值约为480 req/sec,辛烷的平均值为约400 req/sec

示例基准:

  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
  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
  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

为什么?

确保在PHP配置中为cli启用了操作缓存

opcache.enable_cli=1

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

最新更新