MixPHP、Yii与CodeIgniter并发性能对比背景
前言部分简要介绍了本次对比的对象。
MixPHP是一款依托Swoole构建的高性能框架,CodeIgniter作为元老级的轻量级代表,而Yii则是目前极为流行的开发框架,三者各有特点。
鉴于Yii和CodeIgniter均基于传统的Apache或PHP-FPM架构,若直接采用MixPHP默认的Swoole部署方式进行对比并不公平。由于MixPHP同样支持在Apache/PHP-FPM环境中运行,因此本次测试将MixPHP也部署于Apache之下以确保条件一致。
测试环境配置如下。
服务器为虚拟机配置,具备4个CPU核心及1GB内存。
采用ab工具执行压力测试,具体命令参数为:ab -n 5000 -c 100 URL,即总请求数5000,并发数100。
针对Yii框架进行测试。
Yii配置中关闭了调试模式,并将环境设置为pro(生产环境)。
其默认控制器代码逻辑简单,仅输出字符串“Hello World”。
<?php public function actionIndex() { return 'Hello World'; }
接下来是CodeIgniter框架的测试环节。
该框架的默认控制器代码同样被简化为输出“Hello World”。
<?php public function index() { echo 'Hello World'; }
随后对MixPHP框架进行测试。
MixPHP的默认控制器代码也保持一致,输出“Hello World”。
public function actionIndex() { return 'Hello World'; }
正式进入测试阶段。
首先测试Yii框架,测得QPS(每秒查询率)为56.40。
C:/Server/apache24vc11/bin>ab -n 5000 -c 100 http://www.b.com/ This is ApacheBench, Version 2.3 <$Revision: 1757674 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking www.b.com (be patient) Completed 500 requests Completed 1000 requests Completed 1500 requests Completed 2000 requests Completed 2500 requests Completed 3000 requests Completed 3500 requests Completed 4000 requests Completed 4500 requests Completed 5000 requests Finished 5000 requests Server Software: Apache/2.2.32 Server Hostname: www.b.com Server Port: 80 Document Path: / Document Length: 9 bytes Concurrency Level: 100 Time taken for tests: 88.659 seconds Complete requests: 5000 Failed requests: 0 Total transferred: 1080000 bytes HTML transferred: 45000 bytes Requests per second: 56.40 [#/sec] (mean) Time per request: 1773.170 [ms] (mean) Time per request: 17.732 [ms] (mean, across all concurrent requests) Transfer rate: 11.90 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 0 1.0 1 29 Processing: 14 1765 4200.5 688 48084 Waiting: 14 1610 3741.9 682 46690 Total: 14 1766 4200.5 688 48084 WARNING: The median and mean for the initial connection time are not within a normal deviation These results are probably not that reliable. Percentage of the requests served within a certain time (ms) 50% 688 66% 1087 75% 1400 80% 1657 90% 3029 95% 7665 98% 14760 99% 24300 100% 48084 (longest request)
其次测试CodeIgniter框架,测得QPS为144.42。
C:/Server/apache24vc11/bin>ab -n 5000 -c 100 http://www.c.com/ This is ApacheBench, Version 2.3 <$Revision: 1757674 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking www.c.com (be patient) Completed 500 requests Completed 1000 requests Completed 1500 requests Completed 2000 requests Completed 2500 requests Completed 3000 requests Completed 3500 requests Completed 4000 requests Completed 4500 requests Completed 5000 requests Finished 5000 requests Server Software: Apache/2.2.32 Server Hostname: www.c.com Server Port: 80 Document Path: / Document Length: 8 bytes Concurrency Level: 100 Time taken for tests: 34.621 seconds Complete requests: 5000 Failed requests: 0 Total transferred: 1075000 bytes HTML transferred: 40000 bytes Requests per second: 144.42 [#/sec] (mean) Time per request: 692.419 [ms] (mean) Time per request: 6.924 [ms] (mean, across all concurrent requests) Transfer rate: 30.32 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 0 0.7 1 22 Processing: 8 689 1014.3 480 13324 Waiting: 8 686 1012.2 478 13324 Total: 9 689 1014.3 480 13325 WARNING: The median and mean for the initial connection time are not within a normal deviation These results are probably not that reliable. Percentage of the requests served within a certain time (ms) 50% 480 66% 673 75% 810 80% 902 90% 1238 95% 1808 98% 3336 99% 6037 100% 13325 (longest request)
最后测试MixPHP框架,测得QPS高达440.48。
C:/Server/apache24vc11/bin>ab -n 5000 -c 100 http://www.a.com/ This is ApacheBench, Version 2.3 <$Revision: 1757674 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking www.a.com (be patient) Completed 500 requests Completed 1000 requests Completed 1500 requests Completed 2000 requests Completed 2500 requests Completed 3000 requests Completed 3500 requests Completed 4000 requests Completed 4500 requests Completed 5000 requests Finished 5000 requests Server Software: Apache/2.2.32 Server Hostname: www.a.com Server Port: 80 Document Path: / Document Length: 12 bytes Concurrency Level: 100 Time taken for tests: 11.351 seconds Complete requests: 5000 Failed requests: 0 Total transferred: 1025000 bytes HTML transferred: 60000 bytes Requests per second: 440.48 [#/sec] (mean) Time per request: 227.026 [ms] (mean) Time per request: 2.270 [ms] (mean, across all concurrent requests) Transfer rate: 88.18 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 1 0.9 1 25 Processing: 4 222 822.4 15 10376 Waiting: 4 221 822.4 14 10376 Total: 4 222 822.4 16 10376 Percentage of the requests served within a certain time (ms) 50% 16 66% 69 75% 214 80% 364 90% 561 95% 762 98% 978 99% 1829 100% 10376 (longest request)
根据测试结果得出结论。
由于虚拟机文件IO性能较差,当Yii尝试处理300并发时直接失去响应,故被迫降低至100并发。即便在同等架构限制下,MixPHP的性能表现依然领先。
欢迎读者自行验证,项目地址为:https://github.com/mixstart/mixPHP。
| ITEM | DESC |
|---|---|
| Yii | 56.40 QPS |
| CodeIgniter | 144.42 QPS |
| MixPHP | 440.48 QPS |
总结部分回顾全文。
以上内容即为本次对比的全部细节,希望能为相关学习或工作提供参考。如有任何疑问,欢迎留言交流,感谢支持。