18

Yaf 3.1.0 - 10%性能提升版

 4 years ago
source link: https://www.laruence.com/2020/03/15/5563.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Yaf(Yet Another Framework) 是我的第一个发布的PECL扩展,也是我走上PHP内核维护的开始,我一直对它比较有感情,Yaf在过去的8年多时间里,也得到了不少朋友的喜爱,当然Yaf还是有很多不足,但毕竟Yaf主要还是针对性能场景,不能满足所有的需求。

FrMfEjR.jpg!web

即然Yaf == 性能,借着疫情的在家时间,又花了一些时间对Yaf做了一轮优化,希望能对性能有进一步的提升。

主要涉及到的有:

1. 重构了Yaf_Loader::autoload
2. 重写了Yaf_Route_Static/Rewrite的核心逻辑
3. is_localnamespace采用Hash匹配来取代之前的字符串匹配
4. 重写了大部分的逻辑以避免内存分配

因为改动比较大,所以也跳跃了版本,从3.0.9到3.1.0. 下载地址: Yaf At PECL

那么,还是老规矩,我们来测试下性能变化,首先采用yaf代码中的tools/cg/yaf_cg来生成一个测试:

tools/cg/yaf_cg benchmark /var/www/html/yaf

测试服务器配置:

32GB RAM 
Intel(R) Xeon(R) CPU E5-2620 v2 @ 2.10GHz * 24
Nginx 12 processes
PHP-FPM 24 processes

我们首先用yaf-3.0.9版本来测试(200并发):

ab -n 100000 -c 200 http://10.33.1.23:8000/yaf/

Server Software:        nginx/1.9.7
Server Hostname:        10.33.1.23
Server Port:            8000

Document Path:          /yaf/
Document Length:        26 bytes

Concurrency Level:      200
Time taken for tests:   17.950 seconds
Complete requests:      100000
Failed requests:        0
Write errors:           0
Total transferred:      19100000 bytes
HTML transferred:       2600000 bytes
Requests per second:    5571.17 [#/sec] (mean)
Time per request:       35.899 [ms] (mean)
Time per request:       0.179 [ms] (mean, across all concurrent requests)
Transfer rate:          1039.15 [Kbytes/sec] received

然后我们用yaf-3.1.0来测试(200并发):

ab -n 100000 -c 200 http://10.33.1.23:8000/yaf/

Server Software:        nginx/1.9.7
Server Hostname:        10.33.1.23
Server Port:            8000

Document Path:          /yaf/
Document Length:        26 bytes

Concurrency Level:      200
Time taken for tests:   16.046 seconds
Complete requests:      100000
Failed requests:        0
Write errors:           0
Total transferred:      19100000 bytes
HTML transferred:       2600000 bytes
Requests per second:    6232.05 [#/sec] (mean)
Time per request:       32.092 [ms] (mean)
Time per request:       0.160 [ms] (mean, across all concurrent requests)
Transfer rate:          1162.42 [Kbytes/sec] received

可以看到,QPS从5571飙升到了6232, 11%的QPS提升。

当然了,这点提升对于一个实际项目来说,可能并不明显,我只是在这里说明下版本升级对比:)

另外毕竟这次改动变动的代码比较大, 虽然说已经尽力测试, 但可能还是会有问题, 如果大家在实际使用的时候遇到任何问题, 可以在 Github上提Issue ,或者直接发邮件给我laruence at php dot net, 注意邮件不要带附件,php的mail系统过滤有附件的邮件:<Enjoy!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK