MixPHP 与 Phalcon 性能对比测试

2017-09-19 21:10:13 +08:00
 onanying

今天完成了 MixPHP 的 Mysql/Redis 进程级长连接处理,想着做一下压测,试试性能,于是就拿公司在使用的 Phalcon C 扩展框架对比一下,对比后的结果吓我一跳,感慨 MixPHP 极简架构 + 基于 Swoole 的 MixHttpd + 进程级长连接 带来了如此强大的性能。

测试统计在最后面。

硬件

虚拟机:4 核 1G

环境

[ Phalcon ]

Apache worker 模式,mpm 配置如下:

这个配置基本上不会有连接上的瓶颈了,这样测试才公平。

<IfModule worker.c>
ServerLimit  50
ThreadLimit  200
StartServers  5
MaxClients  5000
MinSpareThreads  25
MaxSpareThreads  500
ThreadsPerChild  100
MaxRequestsPerChild 0
</IfModule>

[ MixPHP ]

MixHttpd 配置如下:

4 个线程处理连接,8 个进程处理 PHP 代码。

reactor_num = 4
worker_num = 8

源代码

请求数据表的第一行,并输出 json。

[ Phalcon ]

public function actionIndex()
{
    $this->view->disable();
    $query = $this->modelsManager->executeQuery("SELECT * FROM Test")->getFirst();
    $data  = $query->toArray();
    header("Content-Type:application/json;charset=utf-8");
    echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
}

[ MixPHP ]

function actionIndex()
{
    $rows = \Mix::app()->rdb->createCommand("SELECT * FROM `test`")->queryOne();
    return $rows;
}

普通测试

工具为 Apache 的 ab 命令。

[ Phalcon ]

100 并发,请求全部成功,QPS 为 174.39

C:\Server\apache24vc11\bin>ab -n 2000 -c 100 http://www.t.com/index/test
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.t.com (be patient)
Completed 200 requests
Completed 400 requests
Completed 600 requests
Completed 800 requests
Completed 1000 requests
Completed 1200 requests
Completed 1400 requests
Completed 1600 requests
Completed 1800 requests
Completed 2000 requests
Finished 2000 requests


Server Software:        Apache/2.2.32
Server Hostname:        www.t.com
Server Port:            80

Document Path:          /index/test
Document Length:        631 bytes

Concurrency Level:      100
Time taken for tests:   11.468 seconds
Complete requests:      2000
Failed requests:        0
Total transferred:      1692000 bytes
HTML transferred:       1262000 bytes
Requests per second:    174.39 [#/sec] (mean)
Time per request:       573.422 [ms] (mean)
Time per request:       5.734 [ms] (mean, across all concurrent requests)
Transfer rate:          144.08 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    2   3.6      1      55
Processing:    27  559 452.9    426    2604
Waiting:       26  555 453.2    422    2603
Total:         28  560 452.8    428    2604

Percentage of the requests served within a certain time (ms)
  50%    428
  66%    598
  75%    717
  80%    819
  90%   1238
  95%   1538
  98%   1868
  99%   2103
 100%   2604 (longest request)

[ MixPHP ]

100 并发,请求全部成功,QPS 为 637.81

C:\Server\apache24vc11\bin>ab -n 2000 -c 100 http://www.ve.com:9501/
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.ve.com (be patient)
Completed 200 requests
Completed 400 requests
Completed 600 requests
Completed 800 requests
Completed 1000 requests
Completed 1200 requests
Completed 1400 requests
Completed 1600 requests
Completed 1800 requests
Completed 2000 requests
Finished 2000 requests


Server Software:        swoole-http-server
Server Hostname:        www.ve.com
Server Port:            9501

Document Path:          /
Document Length:        613 bytes

Concurrency Level:      100
Time taken for tests:   3.136 seconds
Complete requests:      2000
Failed requests:        0
Total transferred:      1566000 bytes
HTML transferred:       1226000 bytes
Requests per second:    637.81 [#/sec] (mean)
Time per request:       156.787 [ms] (mean)
Time per request:       1.568 [ms] (mean, across all concurrent requests)
Transfer rate:          487.70 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   0.7      1      10
Processing:    88  149  22.2    151     209
Waiting:       26  146  26.9    150     207
Total:         89  150  22.1    152     209

Percentage of the requests served within a certain time (ms)
  50%    152
  66%    160
  75%    165
  80%    167
  90%    175
  95%    182
  98%    192
  99%    198
 100%    209 (longest request)

暴力测试

[ Phalcon ]

1000 并发,由于响应太慢,服务器卡死了很久,不得不手动终止,执行成功 5139,失败 8,QPS 为 3.77


C:\Server\apache24vc11\bin>ab -n 10000 -c 1000 http://www.t.com/index/test
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.t.com (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests


Server Software:        Apache/2.2.32
Server Hostname:        www.t.com
Server Port:            80

Document Path:          /index/test
Document Length:        631 bytes

Concurrency Level:      1000
Time taken for tests:   1364.772 seconds
Complete requests:      5139
Failed requests:        8
   (Connect: 0, Receive: 0, Length: 8, Exceptions: 0)
Total transferred:      4388782 bytes
HTML transferred:       3275767 bytes
Requests per second:    3.77 [#/sec] (mean)
Time per request:       265571.594 [ms] (mean)
Time per request:       265.572 [ms] (mean, across all concurrent requests)
Transfer rate:          3.14 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    6 118.5      1    3044
Processing:    14 76341 202529.3  16190 1363728
Waiting:       12 75306 202698.3  15302 1363726
Total:         14 76346 202529.0  16191 1363729

Percentage of the requests served within a certain time (ms)
  50%  16190
  66%  26254
  75%  45141
  80%  58735
  90%  150683
  95%  399760
  98%  946039
  99%  1294335
 100%  1363729 (longest request)

[ MixPHP ]

1000 并发,请求全部成功,QPS 任然高达 561.51

C:\Server\apache24vc11\bin>ab -n 10000 -c 1000 http://www.ve.com:9501/
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.ve.com (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests


Server Software:        swoole-http-server
Server Hostname:        www.ve.com
Server Port:            9501

Document Path:          /
Document Length:        613 bytes

Concurrency Level:      1000
Time taken for tests:   17.809 seconds
Complete requests:      10000
Failed requests:        0
Total transferred:      7830000 bytes
HTML transferred:       6130000 bytes
Requests per second:    561.51 [#/sec] (mean)
Time per request:       1780.914 [ms] (mean)
Time per request:       1.781 [ms] (mean, across all concurrent requests)
Transfer rate:          429.36 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   0.9      1      12
Processing:  1211 1642  98.5   1676    1833
Waiting:      368 1343 306.3   1448    1774
Total:       1211 1644  98.6   1677    1836

Percentage of the requests served within a certain time (ms)
  50%   1677
  66%   1694
  75%   1702
  80%   1705
  90%   1718
  95%   1729
  98%   1748
  99%   1769
 100%   1836 (longest request)

结论

MixPHP 以压倒性的性能优势 KO Phalcon。

| 框架     | 并发数   | 失败数 | QPS      |
| MixPHP  | 100 并发  | 0      | 637.81   |
| Phalcon | 100 并发  | 0      | 174.39   |

| 框架     | 并发数    | 失败数 | QPS     |
| MixPHP  | 1000 并发  | 0      | 561.51  |
| Phalcon | 1000 并发  | 8      | 3.77    |
4690 次点击
所在节点    PHP
8 条回复
sagaxu
2017-09-19 23:54:56 +08:00
Phalcon 数据库开长连接了吗?
gouchaoer
2017-09-20 01:38:17 +08:00
phalcon 只能在没有 io 的 hello-world 里性能爆表,在正常的有 session/sql 之类的 io 的业务中和纯 php 性能差别很小,有人做了实验提给 phalcon 社区然后社区也很吃惊。。。

所以我的理解是主要瓶颈还是 io,把 io 弄成异步也好协程也好就能大幅度提高性能。。。。至于 mysql 的连接池问题,我记得有 2 倍不到的 qps 差距,并不大
abccccabc
2017-09-21 09:54:47 +08:00
@gouchaoer 你说的 phalcon 这块 session/sql 在那个贴子上呀,我也看看。
onanying
2017-09-22 14:11:48 +08:00
@sagaxu Phalcon 并没有开长连接,框架本身没有提供长连接,不过我认为 mysql 连接数在这个测试中,应该不是瓶颈。
onanying
2017-09-22 14:13:19 +08:00
@gouchaoer 你说到点子上了,哈哈
sagaxu
2017-09-22 14:26:51 +08:00
@onanying
https://docs.phalconphp.com/en/latest/db-layer#connecting-to-databases
https://github.com/phalcon/cphalcon/blob/master/phalcon/db/adapter/pdo.zep

Phalcon 本身没有长连接,但 PDO 有,建议还是测试一下有无长连接时的性能差距
mingyun
2017-09-23 10:37:28 +08:00
看安装要求里 https://www.kancloud.cn/onanying/mixphp1/403489,不装 Swoole 也可以吧
onanying
2017-09-24 12:00:26 +08:00
@mingyun 在 LNMP/LAMP 下不需要 swoole 的

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/392064

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX