发布于2021-03-14 06:07 阅读(757) 评论(0) 点赞(14) 收藏(4)
这次测试咱们采用Laravel框架。laravel作为最热门的php框架之一,广受认可与欢迎。同时由于集成度很高,每次运行都加载了大量文件,加之使用了大量的闭包、魔术方法,导致laravel框架很重,并发性能极差。OPcache和Swoole都是php的扩展,这次旨在比较这两个扩展分别开启后对Laravel应用的加速效果。
测试所用的主机为虚拟机,虚拟机配置在双核4GB的个人电脑中。虚拟机系统为linux,http服务器采用nginx,用lnmp脚本安装nginx、mysql、php。Laravel框架为7.X版本。
server{ listen 80; root "/vagrant/www/laravel7/public"; server_name test.laravel.com; index index.html index.php; location / { try_files $uri $uri/ /index.php?$args; } location ~ \.php$ { fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_index index.php; fastcgi_split_path_info ^(.+\.php)(.*)$; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
编辑/etc/hosts文件,在新行添加127.0.0.1 test.laravel.com
<?php
namespace App\Http\Controllers;
class TestController extends Controller
{
public function test()
{
return 123;
}
}
Route::get('test', 'TestController@test');
在app/Http/Kernel文件中,关掉频率限制中间件throttle。
修改php-fpm.conf文件,修改pm和pm.max_children 配置,pm设置为static,pm.max_children设置为50,以获得较好的并发性能。
[www] listen = /tmp/php-cgi.sock listen.backlog = -1 listen.allowed_clients = 127.0.0.1 listen.owner = www listen.group = www listen.mode = 0666 user = www group = www pm = static pm.max_children = 50 pm.start_servers = 10 pm.min_spare_servers = 10 pm.max_spare_servers = 20 request_terminate_timeout = 100 request_slowlog_timeout = 0 slowlog = var/log/slow.log
Server Software: nginx
Server Hostname: test.laravel.com
Server Port: 80
Document Path: /api/test
Document Length: 3 bytes
Concurrency Level: 100
Time taken for tests: 148.306 seconds
Complete requests: 1000
Failed requests: 0
Total transferred: 253000 bytes
HTML transferred: 3000 bytes
Requests per second: 6.74 [#/sec] (mean)
Time per request: 14830.553 [ms] (mean)
Time per request: 148.306 [ms] (mean, across all concurrent requests)
Transfer rate: 1.67 [Kbytes/sec] received
此时的并发大约为为 7 qps
在配置文件php.ini文件中开启opcache
zend_extension="opcache.so" opcache.enable=1 opcache.memory_consumption=128 opcache.max_accelerated_files=10000 opcache.revalidate_freq=60 opcache.fast_shutdown=1 opcache.enable_cli=1 opcache.interned_strings_buffer=8
Server Software: nginx
Server Hostname: test.laravel.com
Server Port: 80
Document Path: /api/test
Document Length: 4 bytes
Concurrency Level: 100
Time taken for tests: 11.006 seconds
Complete requests: 1000
Failed requests: 0
Total transferred: 254000 bytes
HTML transferred: 4000 bytes
Requests per second: 90.86 [#/sec] (mean)
Time per request: 1100.590 [ms] (mean)
Time per request: 11.006 [ms] (mean, across all concurrent requests)
Transfer rate: 22.54 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 1 4.3 0 16
Processing: 409 1069 152.0 1092 1414
Waiting: 408 1069 152.0 1092 1414
Total: 424 1070 149.6 1092 1414
Percentage of the requests served within a certain time (ms)
50% 1092
66% 1126
75% 1149
80% 1162
90% 1203
95% 1242
98% 1280
99% 1309
100% 1414 (longest request)
此时的达到了 90qps,性能是未开启时的 10 倍以上!。
在项目目录下运行composer命令安装;在nginx的配置文件中配置,将请求转发到swoole监听的端口。
用 ab 压测 : ab -n 1000 -c 100 http://test.laravel.com/api/test
Server Software: nginx
Server Hostname: test.laravel.com
Server Port: 80
Document Path: /api/test
Document Length: 4 bytes
Concurrency Level: 100
Time taken for tests: 1.158 seconds
Complete requests: 1000
Failed requests: 0
Total transferred: 225000 bytes
HTML transferred: 4000 bytes
Requests per second: 863.46 [#/sec] (mean)
Time per request: 115.813 [ms] (mean)
Time per request: 1.158 [ms] (mean, across all concurrent requests)
Transfer rate: 189.72 [Kbytes/sec] received
速度起飞!达到了800qps!
也就是一百多倍?
当然这只是一个比较简单的测试,但是总的来说opcache扩展和swoole扩展对php脚本性能的提升还是很明显的。
PHP 互联网架构师 50K 成长指南+行业问题解决总纲(持续更新)
面试10家公司,收获9个offer,2020年PHP 面试问题
★如果喜欢我的文章,想与更多资深开发者一起交流学习的话,获取更多大厂面试相关技术咨询和指导,欢迎加入我们的群啊,暗号:phpzh
内容不错的话希望大家支持鼓励下点个赞/喜欢,欢迎一起来交流;另外如果有什么问题 建议 想看的内容可以在评论提出
原文链接:https://www.cnblogs.com/phpyu/p/13712673.html
作者:sdjsdh
链接:http://www.phpheidong.com/blog/article/3095/3fedc0c521fb30f4ac81/
来源:php黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 php黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-4
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!