hhxsv5 / laravel-s

LaravelS is an out-of-the-box adapter between Laravel/Lumen and Swoole.
MIT License
3.83k stars 473 forks source link

压力测试与fpm相差无几 #267

Closed zhong1002 closed 4 years ago

zhong1002 commented 4 years ago
  1. Your software version (Screenshot of your startup)

+---------------------------+---------------------------------------+ | Component | Version | +---------------------------+---------------------------------------+ | PHP | 7.3.16-1+ubuntu16.04.1+deb.sury.org+1 | | Swoole | 4.4.17 | | LaravelS | 3.7.3 | | Laravel Framework [local] | 5.5.49 | +---------------------------+---------------------------------------+

  1. Detail description about this issue(error/log) 本来只是对比一下几个框架的性能,发现结果好像有点问题. 都是空框架,只返回"hello world" 用的jmeter测试.因为发现在web路由中测试结果异常,于是在api路由也测试了一下,发现api路由中结果是符合预期的.
测试都是运行1w次,我测试的服务器是8核16G的   100用户 500用户 1000用户 平均字节
php 6476 5379 4557 213
tp6 2396 2010 1867 213
Laravel(api) 1424 1242 1003 254
Laravel+swoole(api) 6464 5299 4629 216
Laravel(web) 298     934
Laravel+swoole(web) 300     859

image

image

api路由是 /api/test web路由是 /

  1. Some reproducible code blocks and steps

    测试代码已上传到 https://github.com/slarkes/abtest

hhxsv5 commented 4 years ago

请确认请求是否走了LaravelS(Swoole Http Server)?输出php_sapi_name()看看? 可以将JMeter请求地址与Nginx配置贴出来看看。

zhong1002 commented 4 years ago

走的,因为api路由与web路由也在同一个项目. image

nginx配置是

upstream swoole_test {

通过 IP:Port 连接

server workspace:5200 weight=5 max_fails=3 fail_timeout=30s;
# 通过 UnixSocket Stream 连接,小诀窍:将socket文件放在/dev/shm目录下,可获得更好的性能
#server unix:/xxxpath/laravel-s-test/storage/laravels.sock weight=5 max_fails=3 fail_timeout=30s;
#server 192.168.1.1:5200 weight=3 max_fails=3 fail_timeout=30s;
#server 192.168.1.2:5200 backup;
keepalive 16;

} server { listen 8083; listen [::]:8083;

# For https
#ssl on;
#listen 443 ssl;
#listen [::]:443 ssl;
#ssl_certificate /var/www/ssl_cert/default.crt;
#ssl_certificate_key /var/www/ssl_cert/default.key;

server_name 172.17.0.12;
root /var/www/abtest/laravel5-swoole/public;
index index.php index.html index.htm;

location / {
    try_files $uri @laravels;
}

location ~* \.php$ {
    return 404;
}

location ~ /\.ht {
        deny all;
}

location /.well-known/acme-challenge/ {
    root /var/www/letsencrypt/;
    log_not_found off;
}

error_log /var/log/nginx/laravel_error.log;
access_log /var/log/nginx/laravel_access.log;

location @laravels {
    # proxy_connect_timeout 60s;
    # proxy_send_timeout 60s;
    # proxy_read_timeout 120s;
    proxy_http_version 1.1;
    proxy_set_header Connection "";
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Real-PORT $remote_port;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header Scheme $scheme;
    proxy_set_header Server-Protocol $server_protocol;
    proxy_set_header Server-Name $server_name;
    proxy_set_header Server-Addr $server_addr;
    proxy_set_header Server-Port $server_port;
    proxy_pass http://swoole_test;
}

}

s51983 commented 4 years ago

1、session和cache换成redis驱动 2、关闭session(包括Csrf中间件)

高并发下会因为这两点疯狂写文件,即使session换用redis也会因为疯狂读写redis导致cpu飙升

hhxsv5 commented 4 years ago

压测注意事项:

  1. 先阅读:https://wiki.swoole.com/wiki/page/62.html
  2. 注意各个中间件,默认的Session中间件基于文件,IO性能非常差
  3. 压空接口,目的在于对比框架的消耗,对比FPM,快慢都是相对的
  4. 如果代码中有类似sleep的同步阻塞操作,那QPS自然不会高
  5. 压测时最好在内网测试,避免网络带宽因素影响结果。