前言:最近想使用 swoole 試試 websocket, 正好安裝測試下 laravel 和 swoole 性能,不知道是不是 laravel9 性能優化的好,最后壓測結果和 laravels 的差不多~
laradock 安裝 swoole
動手操作后遇到問題:
因為原先的 PHP 版本是 7.4,需要修改.env 文件,將版本切換到 8.0 及以上 (laravels 安裝的時候要求 php 是 8.1,所以還是設置成 8.1 版本的):
再次執行 build 命令后成功。【推薦:laravel視頻教程】
驗證結果:
配置 laravels 的 http 服務器
1. 安裝 laravel 項目
教程很多,此處參考自:laravel視頻教程
基于 安裝了 docker 的環境
curl -s "https://laravel.build/laravel9" | bashCopy
2. 安裝 laravels
composer require hhxsv5/laravel-sCopy
3. 發布 laravels 配置
php artisan laravels publishCopy
4. 配置站點
說明: 站點對應的項目代碼都是 /var/www/laravel9/public
(1) 配置 laravels 的 http 服務器
upstream?laravels?{ ????#?Connect?IP:Port ????server?workspace:5200?weight=5?max_fails=3?fail_timeout=30s; ????keepalive?16; } server?{ ????listen?80; ????server_name?swoole.test; ????root?/var/www/laravel9/public; ????index?index.php?index.html?index.htm; ????#?Nginx?處理靜態資源,LaravelS?處理動態資源 ????location?/?{ ????????try_files?$uri?@laravels; ????} ????location?@laravels?{ ????????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://laravels; ????} ????error_log?/var/log/nginx/swoole_test_error.log; ????access_log?/var/log/nginx/swoole_test_access.log; }
注意:laravels 項目需要在 laravel9 項目下的.env 文件中增加以下配置:
LARAVELS_LISTEN_IP=workspace LARAVELS_DAEMONIZE=trueCopy
(2) 配置普通 laravel 項目站點
server?{ ????listen?80; ????listen?[::]:80; ????#?For?https ????#?listen?443?ssl; ????#?listen?[::]:443?ssl?ipv6only=on; ????#?ssl_certificate?/etc/nginx/ssl/default.crt; ????#?ssl_certificate_key?/etc/nginx/ssl/default.key; ????server_name?laravel.test; ????root?/var/www/laravel9/public; ????index?index.php?index.html?index.htm; ????location?/?{ ?????????try_files?$uri?$uri/?/index.php$is_args$args; ????} ????location?~?.php$?{ ????????try_files?$uri?/index.php?=404; ????????fastcgi_pass?php-upstream; ????????fastcgi_index?index.php; ????????fastcgi_buffers?16?16k; ????????fastcgi_buffer_size?32k; ????????fastcgi_param?SCRIPT_FILENAME?$document_root$fastcgi_script_name; ????????#fixes?timeouts ????????fastcgi_read_timeout?600; ????????include?fastcgi_params; ????} ????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; }
(3) 本地 host 配置
127.0.0.1 swoole.test127.0.0.1 laravel.testCopy
(4) 重新 build 容器
docker-compose stop docker-compose build workspace nginx docker-compose up -d redis mysql nginx workspaceCopy
(5) 進入 workspace 容器啟動 laravels
進入容器命令:
docker exec -it d4940755a928 /bin/bashCopy
AB 性能測試結果
- 核心關注的是每秒請求數(Requests per second)
- 都是基于 Laradock 環境
- 共享同一份項目代碼
1.總請求數是 100,并發數是 10(左側為 swoole,右側為 laravel9):
2.總請求數是 1000,并發數是 20(左側為 swoole,右側為 laravel9);
差距不明顯,甚至有時候 laravel9 的結果更好,這樣的結果算翻車嗎~
原文地址:https://learnku.com/articles/73575